Skip to content

Commit

Permalink
Refactor reducer callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 7, 2020
1 parent 1bb45f2 commit 9d1e176
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
53 changes: 31 additions & 22 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
final class Group extends AbstractOperation
{
/**
* @psalm-return Closure(null|callable(TKey, T):(TKey)): Closure(Iterator<TKey, T>): Generator<int, list<T>>
* @psalm-return Closure(null|callable(TKey, T):(TKey|null)): Closure(Iterator<TKey, T>): Generator<int, T|list<T>>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param null|callable(TKey, T):(T|TKey) $callable
* @psalm-param null|callable(TKey, T):(TKey|null) $callable
*/
static function (?callable $callable = null): Closure {
return
Expand All @@ -32,45 +32,54 @@ static function (?callable $callable = null): Closure {
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($callable): Generator {
/** @psalm-var callable(T, TKey): (TKey|null) $callable */
$callable = $callable ??
/**
* @param mixed $key
* @psalm-param TKey $key
*
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
*
* @return mixed
* @psalm-return TKey
*/
static function ($value, $key) {
return $key;
};

$callback =
$reducerFactory =
/**
* @psalm-param array<TKey, list<T>> $collect
*
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
* @psalm-param callable(T, TKey): (TKey|null) $callback
*
* @psalm-return array<TKey, list<T>>
* @psalm-return Closure(array<TKey, T|list<T>>, T, TKey): array<TKey, T|list<T>>
*/
static function (array $collect, $value, $key) use ($callable): array {
if (null !== $groupKey = $callable($value, $key)) {
$collect[$groupKey][] = $value;
} else {
$collect[$key] = $value;
}
static function (callable $callback): Closure {
return
/**
* @psalm-param array<TKey, list<T>> $collect
*
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
*
* @psalm-return non-empty-array<TKey, T|list<T>>
*/
static function (array $collect, $value, $key) use ($callback): array {
if (null !== $groupKey = $callback($value, $key)) {
$collect[$groupKey][] = $value;
} else {
$collect[$key] = $value;
}

return $collect;
return $collect;
};
};

/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $foldLeft */
$foldLeft = FoldLeft::of()($callback)([]);
$foldLeft = FoldLeft::of()($reducerFactory($callable))([]);

return yield from ($foldLeft($iterator))->current();
};
Expand Down
34 changes: 18 additions & 16 deletions src/Operation/Implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,34 @@
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Implode extends AbstractOperation
{
public function __invoke(): Closure
{
return static function (string $glue): Closure {
return static function (Iterator $iterator) use ($glue): Generator {
$callback =
/**
* @psalm-param TKey $key
* @psalm-param CachingIterator $iterator
*
* @param mixed $key
* @param mixed $iterator
*/
static function (string $carry, string $item, $key, CachingIterator $iterator) use ($glue): string {
$carry .= $item;
$reducerFactory = static function (string $glue): Closure {
return
/**
* @psalm-param TKey $key
*
* @param mixed $key
*/
static function (string $carry, string $item, $key, CachingIterator $iterator) use ($glue): string {
$carry .= $item;

if ($iterator->hasNext()) {
$carry .= $glue;
}
if ($iterator->hasNext()) {
$carry .= $glue;
}

return $carry;
};
return $carry;
};
};

return yield from FoldLeft::of()($callback)('')(new CachingIterator($iterator));
return yield from FoldLeft::of()($reducerFactory($glue))('')(new CachingIterator($iterator));
};
};
}
Expand Down

0 comments on commit 9d1e176

Please sign in to comment.