diff --git a/README.md b/README.md index 83c6e4b..cf103d0 100644 --- a/README.md +++ b/README.md @@ -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. 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. +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. 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. diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index d8e2b32..45089b1 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -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, fn (Seq $a) => $a::new(array_reverse($a->getData())) ));