-
-
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.
feat: introduce search_with_keys and search_with_keys_opt
Currently, there's no way to search iterable using also a key
- Loading branch information
Showing
6 changed files
with
92 additions
and
8 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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Iter; | ||
|
||
use Closure; | ||
|
||
/** | ||
* Searches an iterable until a predicate returns true, then returns | ||
* the value of the matching element. | ||
* | ||
* Examples: | ||
* | ||
* Iter\search_with_keys(['foo', 'bar', 'baz'], fn($k, $v) => 'baz' === $v) | ||
* => 'baz' | ||
* | ||
* Iter\search_with_keys(['foo', 'bar', 'baz'], fn($k, $v) => 'qux' === $v) | ||
* => null | ||
* | ||
* @template TKey | ||
* @template TValue | ||
* | ||
* @param iterable<TKey, TValue> $iterable The iterable to search | ||
* @param (Closure(TKey, TValue): bool) $predicate | ||
* | ||
* @return TValue|null | ||
*/ | ||
function search_with_keys(iterable $iterable, Closure $predicate): mixed | ||
{ | ||
foreach ($iterable as $key => $value) { | ||
if ($predicate($key, $value)) { | ||
return $value; | ||
} | ||
} | ||
|
||
return null; | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Iter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Iter; | ||
|
||
final class SearchWithKeysOptTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideDataSome | ||
*/ | ||
public function testSearchSome($expected, iterable $iterable, callable $predicate): void | ||
{ | ||
static::assertSame($expected, Iter\search_with_keys_opt($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_with_keys_opt($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]; | ||
} | ||
} |
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