Skip to content

Commit 69684b2

Browse files
committed
Add filterMapKV function
1 parent ecf306d commit 69684b2

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Fp/Functions/Collection/Filter.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,25 @@ function filterNotNull(iterable $collection): array
112112
*/
113113
function filterMap(iterable $collection, callable $predicate): array
114114
{
115-
$gen = FilterMapOperation::of($collection)(dropFirstArg($predicate));
115+
return filterMapKV($collection, dropFirstArg($predicate));
116+
}
117+
118+
/**
119+
* Same as {@see filterMap()} but passing also the key to the $predicate function.
120+
*
121+
* @template TK of array-key
122+
* @template TV
123+
* @template TVO
124+
*
125+
* @param iterable<TK, TV> $collection
126+
* @param callable(TK, TV): Option<TVO> $predicate
127+
* @return array<TK, TVO>
128+
*
129+
* @psalm-return ($collection is list<TV> ? list<TVO> : array<TK, TVO>)
130+
*/
131+
function filterMapKV(iterable $collection, callable $predicate): array
132+
{
133+
$gen = FilterMapOperation::of($collection)($predicate);
116134

117135
return is_array($collection) && array_is_list($collection)
118136
? asList($gen)

0 commit comments

Comments
 (0)