-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
include __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Closure; | ||
use loophp\collection\Collection; | ||
|
||
$isGreaterThan = static fn (int $left): Closure => static fn (int $right): bool => $left < $right; | ||
|
||
$input = array_combine(range('a', 'l'), [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]); | ||
|
||
$collection = Collection::fromIterable($input) | ||
->partition( | ||
$isGreaterThan(5), | ||
$isGreaterThan(3) | ||
); | ||
|
||
// Result | ||
/* | ||
[ | ||
[ | ||
['d', 4], | ||
['e', 5], | ||
['f', 6], | ||
['g', 7], | ||
['h', 8], | ||
['i', 9], | ||
], | ||
[ | ||
['a', 1], | ||
['b', 2], | ||
['c', 3], | ||
['j', 1], | ||
['k', 2], | ||
['l', 3], | ||
], | ||
] | ||
*/ |
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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
interface Partitionable | ||
{ | ||
/** | ||
* @param callable ...$callbacks | ||
* @psalm-param callable(T, TKey):bool ...$callbacks | ||
* | ||
* @psalm-return \loophp\collection\Collection<int, array{int, array<0: TKey, 1: T>}> | ||
*/ | ||
public function partition(callable ...$callbacks): Collection; | ||
} |
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
* | ||
* phpcs:disable Generic.Files.LineLength.TooLong | ||
*/ | ||
final class Partition extends AbstractOperation | ||
{ | ||
/** | ||
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>):bool...): Closure(Iterator<TKey, T>): Generator<int, list<array{0: TKey, 1: T}>> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
return | ||
/** | ||
* @psalm-param callable(T, TKey, Iterator<TKey, T>):bool ...$callbacks | ||
* | ||
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, list<array{0: TKey, 1: T}>> | ||
*/ | ||
static fn (callable ...$callbacks): Closure => | ||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
* | ||
* @psalm-return Generator<int, list<array{0: TKey, 1: T}>> | ||
*/ | ||
static function (Iterator $iterator) use ($callbacks): Generator { | ||
$reducerCallback = | ||
/** | ||
* @param mixed $key | ||
* @psalm-param TKey $key | ||
* | ||
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool | ||
*/ | ||
static fn ($key): Closure => | ||
/** | ||
* @param mixed $current | ||
* @psalm-param T $current | ||
* | ||
* @psalm-return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool | ||
*/ | ||
static fn ($current): Closure => | ||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
* | ||
* @psalm-return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool | ||
*/ | ||
static fn (Iterator $iterator): Closure => | ||
/** | ||
* @psalm-param bool $carry | ||
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable | ||
*/ | ||
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator); | ||
|
||
$true = $false = []; | ||
|
||
foreach ($iterator as $key => $current) { | ||
$result = array_reduce( | ||
$callbacks, | ||
$reducerCallback($key)($current)($iterator), | ||
false | ||
); | ||
|
||
$result ? | ||
$true[] = [$key, $current] : | ||
$false[] = [$key, $current]; | ||
} | ||
|
||
yield $true; | ||
|
||
return yield $false; | ||
}; | ||
} | ||
} |