add function: filterh

This commit is contained in:
Pekka Laiho 2020-10-17 17:08:53 +07:00
parent 6475c5a253
commit c3d99d60ef
2 changed files with 7 additions and 0 deletions

View File

@ -173,6 +173,7 @@ map | `(map (fn (a) (* a 2)) [1 2 3])` | `[2 4 6]` | Create new sequence by
map2 | `(map2 + [1 2 3] [4 5 6])` | `[5 7 9]` | Create new sequence by calling a function for each item from both sequences. map2 | `(map2 + [1 2 3] [4 5 6])` | `[5 7 9]` | Create new sequence by calling a function for each item from both sequences.
reduce | `(reduce + [2 3 4] 1)` | `10` | Reduce a sequence to a single value by calling a function sequentially of all arguments. Optional third argument is used to give the initial value for first iteration. Uses [array_reduce](https://www.php.net/manual/en/function.array-reduce.php) internally. reduce | `(reduce + [2 3 4] 1)` | `10` | Reduce a sequence to a single value by calling a function sequentially of all arguments. Optional third argument is used to give the initial value for first iteration. Uses [array_reduce](https://www.php.net/manual/en/function.array-reduce.php) internally.
filter | `(filter odd? [1 2 3 4 5])` | `[1 3 5]` | Create a new sequence by using the given function as a filter. Uses [array_filter](https://www.php.net/manual/en/function.array-filter.php) internally. filter | `(filter odd? [1 2 3 4 5])` | `[1 3 5]` | Create a new sequence by using the given function as a filter. Uses [array_filter](https://www.php.net/manual/en/function.array-filter.php) internally.
filterh | `(filterh (fn (v k) (prefix? k "a")) {"aa":1 "ab":2 "bb": 3})` | `{"aa":1 "ab":2}` | Same as filter but for hash-maps. First argument passed to the callback is the value and second is the key.
reverse | `(reverse [1 2 3])` | `[3 2 1]` | Reverse the order of a sequence. Uses [array_reverse](https://www.php.net/manual/en/function.array-reverse.php) internally. reverse | `(reverse [1 2 3])` | `[3 2 1]` | Reverse the order of a sequence. Uses [array_reverse](https://www.php.net/manual/en/function.array-reverse.php) internally.
key? | `(key? {"a" "b"} "a")` | `true` | Return true if the hash-map contains the given key. key? | `(key? {"a" "b"} "a")` | `true` | Return true if the hash-map contains the given key.
set | `(set {"a" 1} "b" 2)` | `{"a":1 "b":2}` | Create new hash-map which contains the given key-value pair. set | `(set {"a" 1} "b" 2)` | `{"a":1 "b":2}` | Create new hash-map which contains the given key-value pair.

View File

@ -193,6 +193,12 @@ class Collections implements ILib
} }
)); ));
$env->set('filterh', new CoreFunc('filterh', 'Same as filter but for hash maps. First argument passed to the callback is the value and second is the key.', 2, 2,
function (Func $f, Hash $a) {
return new Hash(array_filter($a->getData(), $f->getClosure(), ARRAY_FILTER_USE_BOTH));
}
));
$env->set('reverse', new CoreFunc('reverse', 'Create new sequence with reversed order.', 1, 1, $env->set('reverse', new CoreFunc('reverse', 'Create new sequence with reversed order.', 1, 1,
fn (Seq $a) => $a::new(array_reverse($a->getData())) fn (Seq $a) => $a::new(array_reverse($a->getData()))
)); ));