diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index d1db86701..542de2d4c 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -887,7 +887,7 @@ public function it_can_get(): void { $this::fromIterable(range('A', 'E')) ->get(4) - ->shouldIterateAs(['E']); + ->shouldIterateAs([4 => 'E']); $this::fromIterable(range('A', 'E')) ->get('unexistent key', 'default') diff --git a/src/Operation/FoldLeft.php b/src/Operation/FoldLeft.php index 60f467c7b..f802fc585 100644 --- a/src/Operation/FoldLeft.php +++ b/src/Operation/FoldLeft.php @@ -37,23 +37,14 @@ static function (callable $callback): Closure { * @psalm-return Closure(Iterator): Generator */ static function ($initial = null) use ($callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - /** @psalm-var Generator $iterator */ - $iterator = Last::of()(ScanLeft::of()($callback)($initial)($iterator)); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + ScanLeft::of()($callback)($initial), + Last::of() + ); - /** @psalm-var Generator $key */ - $key = (Key::of()(0)($iterator)); - /** @psalm-var Generator $current */ - $current = (Current::of()(0)($iterator)); - - return yield $key->current() => $current->current(); - }; + // Point free style. + return $pipe; }; }; } diff --git a/src/Operation/FoldLeft1.php b/src/Operation/FoldLeft1.php index cec251bce..587dd3540 100644 --- a/src/Operation/FoldLeft1.php +++ b/src/Operation/FoldLeft1.php @@ -29,23 +29,14 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback): Generator { - /** @psalm-var Generator $iterator */ - $iterator = Last::of()(ScanLeft1::of()($callback)($iterator)); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + ScanLeft1::of()($callback), + Last::of() + ); - /** @psalm-var Generator $key */ - $key = (Key::of()(0)($iterator)); - /** @psalm-var Generator $current */ - $current = (Current::of()(0)($iterator)); - - return yield $key->current() => $current->current(); - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/FoldRight.php b/src/Operation/FoldRight.php index 2896af90b..7b4138512 100644 --- a/src/Operation/FoldRight.php +++ b/src/Operation/FoldRight.php @@ -37,23 +37,14 @@ static function (callable $callback): Closure { * @psalm-return Closure(Iterator): Generator */ static function ($initial = null) use ($callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - /** @psalm-var Generator $iterator */ - $iterator = ScanRight::of()($callback)($initial)($iterator); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + ScanRight::of()($callback)($initial), + Head::of() + ); - /** @psalm-var Generator $key */ - $key = (Key::of()(0)($iterator)); - /** @psalm-var Generator $current */ - $current = (Current::of()(0)($iterator)); - - return yield $key->current() => $current->current(); - }; + // Point free style. + return $pipe; }; }; } diff --git a/src/Operation/FoldRight1.php b/src/Operation/FoldRight1.php index 57d7ade01..ad3d8c598 100644 --- a/src/Operation/FoldRight1.php +++ b/src/Operation/FoldRight1.php @@ -18,7 +18,7 @@ final class FoldRight1 extends AbstractOperation { /** - * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @psalm-return Closure(callable(T|null, T, TKey, Iterator):(T|null)): Closure(Iterator): Generator */ public function __invoke(): Closure { @@ -26,26 +26,18 @@ public function __invoke(): Closure /** * @psalm-param callable(T|null, T, TKey, Iterator):(T|null) $callback * - * @psalm-return Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback): Generator { - /** @psalm-var Generator $iterator */ - $iterator = ScanRight1::of()($callback)(Reverse::of()($iterator)); + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Reverse::of(), + ScanRight1::of()($callback), + Head::of() + ); - /** @psalm-var Generator $key */ - $key = (Key::of()(0)($iterator)); - /** @psalm-var Generator $current */ - $current = (Current::of()(0)($iterator)); - - return yield $key->current() => $current->current(); - }; + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/Get.php b/src/Operation/Get.php index d3a9696e0..a7c0d5693 100644 --- a/src/Operation/Get.php +++ b/src/Operation/Get.php @@ -15,37 +15,48 @@ */ final class Get extends AbstractOperation { + /** + * @psalm-return Closure(T|TKey): Closure(T): Closure(Iterator): Generator + */ public function __invoke(): Closure { return /** - * @psalm-param array-key $keyToGet - * * @param mixed $keyToGet + * @psalm-param T|TKey $keyToGet + * + * @psalm-return Closure(T): Closure(Iterator): Generator */ static function ($keyToGet): Closure { return /** + * @param mixed $default * @psalm-param T $default * - * @param mixed $default + * @psalm-return Closure(Iterator): Generator */ static function ($default) use ($keyToGet): Closure { - return + $filterCallback = /** - * @psalm-param Iterator $iterator + * @param mixed $value + * @psalm-param T $value * - * @psalm-return Generator + * @param mixed $key + * @psalm-param TKey $key */ - static function (Iterator $iterator) use ($keyToGet, $default): Generator { - foreach ($iterator as $key => $value) { - if ($key === $keyToGet) { - return yield $value; - } - } - - return yield $default; + static function ($value, $key) use ($keyToGet): bool { + return $key === $keyToGet; }; + + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Filter::of()($filterCallback), + Append::of()($default), + Head::of() + ); + + // Point free style. + return $pipe; }; }; } diff --git a/src/Operation/Has.php b/src/Operation/Has.php index 76bfc3bfa..fc22b9beb 100644 --- a/src/Operation/Has.php +++ b/src/Operation/Has.php @@ -27,21 +27,27 @@ public function __invoke(): Closure * @psalm-return Closure(Iterator): Generator */ static function (callable $callback): Closure { - return + $mapCallback = /** - * @psalm-param Iterator $iterator + * @param mixed $value + * @psalm-param T $value * - * @psalm-return Generator + * @param mixed $key + * @psalm-param TKey $key */ - static function (Iterator $iterator) use ($callback): Generator { - foreach ($iterator as $key => $value) { - if ($callback($key, $value) === $value) { - return yield true; - } - } - - return yield false; + static function ($value, $key) use ($callback): bool { + return $callback($key, $value) === $value; }; + + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Map::of()($mapCallback), + Append::of()(false), + Head::of() + ); + + // Point free style. + return $pipe; }; } } diff --git a/src/Operation/IfThenElse.php b/src/Operation/IfThenElse.php index d4fde7fad..fbd4b86f8 100644 --- a/src/Operation/IfThenElse.php +++ b/src/Operation/IfThenElse.php @@ -24,38 +24,45 @@ public function __invoke(): Closure { return /** - * @psalm-param callable(T, TKey): bool $condition + * @psalm-param callable(T, TKey):bool $condition * * @psalm-return Closure(callable(T, TKey): (T)): Closure(callable(T, TKey): (T)): Closure(Iterator): Generator */ static function (callable $condition): Closure { return /** - * @psalm-param callable(T, TKey): (T) $then + * @psalm-param callable(T, TKey):T $then * * @psalm-return Closure(callable(T, TKey): (T)): Closure(Iterator): Generator */ static function (callable $then) use ($condition): Closure { return /** - * @psalm-param callable(T, TKey): (T) $else + * @psalm-param callable(T, TKey):T $else * * @psalm-return Closure(Iterator): Generator */ static function (callable $else) use ($condition, $then): Closure { - return + /** @psalm-var Closure(Iterator): Generator $map */ + $map = Map::of()( /** - * @psalm-param Iterator $iterator + * @param mixed $value + * @psalm-param T $value * - * @psalm-return Generator + * @param mixed $key + * @psalm-param TKey $key + * + * @psalm-return T */ - static function (Iterator $iterator) use ($condition, $then, $else): Generator { - foreach ($iterator as $key => $value) { - yield $key => $condition($value, $key) ? - $then($value, $key) : - $else($value, $key); - } - }; + static function ($value, $key) use ($condition, $then, $else) { + return $condition($value, $key) ? + $then($value, $key) : + $else($value, $key); + } + ); + + // Point free style. + return $map; }; }; };