-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, there's no way to search iterable using also a key
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Iter; | ||
|
||
use Closure; | ||
use Psl\Option\Option; | ||
|
||
/** | ||
* Searches an iterable until a predicate returns true, then returns | ||
* the value of the matching element wrapped in {@see Option::some}. | ||
* If a predicate never returns true, {@see Option::none} will be returned. | ||
* | ||
* Examples: | ||
* | ||
* Iter\search_opt_k_v(['foo', 'bar', 'baz'], fn($k, $v) => 'baz' === $v) | ||
* => Option::some('baz') | ||
* | ||
* Iter\search_opt_k_v(['foo', 'bar', 'baz'], fn($k, $v) => 'qux' === $v) | ||
* => Option::none() | ||
* | ||
* @template TKey | ||
* @template TValue | ||
* | ||
* @param iterable<TKey, TValue> $iterable The iterable to search | ||
* @param (Closure(TKey, TValue): bool) $predicate | ||
* | ||
* @return Option<TValue> | ||
*/ | ||
function search_opt_k_v(iterable $iterable, Closure $predicate): Option | ||
{ | ||
foreach ($iterable as $key => $value) { | ||
if ($predicate($key, $value)) { | ||
return Option::some($value); | ||
} | ||
} | ||
|
||
return Option::none(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Iter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Iter; | ||
|
||
final class SearchOptKVTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideDataSome | ||
*/ | ||
public function testSearchSome($expected, iterable $iterable, callable $predicate): void | ||
{ | ||
static::assertSame($expected, Iter\search_opt_k_v($iterable, $predicate)->unwrap()); | ||
} | ||
|
||
public function provideDataSome(): iterable | ||
{ | ||
yield ['baz', ['foo', 'bar', 'baz'], static fn (int $k, string $v): bool => 2 === $k && 'baz' === $v]; | ||
|
||
yield [ | ||
'baz', | ||
Iter\to_iterator(['foo', 'bar', 'baz']), static fn (int $k, string $v): bool => 2 === $k && 'baz' === $v | ||
]; | ||
} | ||
/** | ||
* @dataProvider provideDataNone | ||
*/ | ||
public function testSearchNone(iterable $iterable, callable $predicate): void | ||
{ | ||
static::assertTrue(Iter\search_opt_k_v($iterable, $predicate)->isNone()); | ||
} | ||
public function provideDataNone(): iterable | ||
{ | ||
yield [[], static fn (int $k, string $v): bool => 'qux' === $v]; | ||
yield [Iter\to_iterator([]), static fn (int $k, string $v): bool => 'qux' === $v]; | ||
yield [Iter\to_iterator(['foo', 'bar', 'baz']), static fn (int $k, string $v): bool => 'qux' === $v]; | ||
} | ||
} |