Skip to content

Commit

Permalink
refactor: Update Collapse operation in point free style. (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol authored Aug 18, 2021
1 parent 0fe81c7 commit 02c61a1
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,19 @@ final class Collapse extends AbstractOperation
*/
public function __invoke(): Closure
{
return
$filterCallback =
/**
* @param Iterator<TKey, iterable<TKey, T>|T> $iterator
*
* @return Generator<TKey, T>
* @param T $value
*/
static function (Iterator $iterator): Generator {
/** @var Closure(Iterator<TKey, T|iterable<TKey, T>>): Generator<TKey, iterable<TKey, T>> $filter */
$filter = Filter::of()(
/**
* @param T $value
*/
static fn ($value): bool => is_iterable($value)
);
static fn ($value): bool => is_iterable($value);

foreach ($filter($iterator) as $value) {
yield from $value;
}
};
/** @var Closure(Iterator<TKey, (T|iterable<TKey, T>)>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Filter::of()($filterCallback),
Flatten::of()(1),
);

// Point free style.
return $pipe;
}
}

0 comments on commit 02c61a1

Please sign in to comment.