Skip to content

Commit b843c77

Browse files
committed
Iterables: $else [WIP]
1 parent 3e0344c commit b843c77

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/Utils/Iterables.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,38 @@ public static function containsKey(iterable $iterable, mixed $key): bool
4848

4949

5050
/**
51-
* Returns the first item (matching the specified predicate if given) or null if there is no such item.
52-
* The callback has the signature `function ($value, $key, $iterable): bool`.
51+
* Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
52+
* The $predicate has the signature `function ($value, $key, $iterable): bool`.
5353
* @template T
5454
* @param iterable<T> $iterable
5555
* @return ?T
5656
*/
57-
public static function first(iterable $iterable, ?callable $predicate = null): mixed
57+
public static function first(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed
5858
{
5959
foreach ($iterable as $k => $v) {
6060
if (!$predicate || $predicate($v, $k, $iterable)) {
6161
return $v;
6262
}
6363
}
64-
return null;
64+
return $else ? $else() : null;
6565
}
6666

6767

6868
/**
69-
* Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
70-
* The callback has the signature `function ($value, $key, $iterable): bool`.
69+
* Returns the key of first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
70+
* The $predicate has the signature `function ($value, $key, $iterable): bool`.
7171
* @template T
7272
* @param iterable<T, mixed> $iterable
7373
* @return ?T
7474
*/
75-
public static function firstKey(iterable $iterable, ?callable $predicate = null): mixed
75+
public static function firstKey(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed
7676
{
7777
foreach ($iterable as $k => $v) {
7878
if (!$predicate || $predicate($v, $k, $iterable)) {
7979
return $k;
8080
}
8181
}
82-
return null;
82+
return $else ? $else() : null;
8383
}
8484

8585

tests/Utils/Iterables.first().phpt

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ test('with predicate', function () {
3737
test('predicate arguments', function () {
3838
Iterables::first([2 => 'x'], fn() => Assert::same(['x', 2, [2 => 'x']], func_get_args()));
3939
});
40+
41+
test('else', function () {
42+
Assert::same(123, Iterables::first(new ArrayIterator([]), else: fn() => 123));
43+
});

tests/Utils/Iterables.firstKey().phpt

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ test('with predicate', function () {
3737
test('predicate arguments', function () {
3838
Iterables::firstKey([2 => 'x'], fn() => Assert::same(['x', 2, [2 => 'x']], func_get_args()));
3939
});
40+
41+
test('else', function () {
42+
Assert::same(123, Iterables::firstKey(new ArrayIterator([]), else: fn() => 123));
43+
});

0 commit comments

Comments
 (0)