From 626c02362416230e2014b5e8b1976e4f909594ae Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 23 Jan 2022 11:51:50 +0100 Subject: [PATCH] Replace `Iterator` with `iterable` almost everywhere. --- src/Collection.php | 4 +- src/Contract/Operation/Applyable.php | 3 +- src/Contract/Operation/Associateable.php | 10 ++-- src/Contract/Operation/Duplicateable.php | 6 +-- src/Contract/Operation/Filterable.php | 3 +- src/Contract/Operation/Findable.php | 4 +- src/Contract/Operation/FlatMapable.php | 3 +- src/Contract/Operation/FoldLeftable.php | 3 +- src/Contract/Operation/FoldRightable.php | 3 +- src/Contract/Operation/Hasable.php | 4 +- src/Contract/Operation/MapNable.php | 3 +- src/Contract/Operation/Mapable.php | 3 +- src/Contract/Operation/Matchable.php | 4 +- src/Contract/Operation/Partitionable.php | 3 +- src/Contract/Operation/Reduceable.php | 3 +- src/Contract/Operation/Reductionable.php | 3 +- src/Contract/Operation/Rejectable.php | 3 +- src/Contract/Operation/ScanLeftable.php | 3 +- src/Contract/Operation/ScanRightable.php | 3 +- src/Contract/Operation/Spanable.php | 3 +- src/Contract/Operation/TakeWhileable.php | 3 +- src/Contract/Operation/Whenable.php | 7 ++- src/Iterator/MultipleIterableIterator.php | 4 +- src/Operation/All.php | 11 ++--- src/Operation/Append.php | 8 +-- src/Operation/Apply.php | 15 +++--- src/Operation/Associate.php | 19 ++++---- src/Operation/Chunk.php | 13 +++-- src/Operation/Coalesce.php | 5 +- src/Operation/Collapse.php | 5 +- src/Operation/Column.php | 10 ++-- src/Operation/Combinate.php | 7 ++- src/Operation/Combine.php | 23 +++++---- src/Operation/Compact.php | 5 +- src/Operation/Contains.php | 7 ++- src/Operation/Current.php | 9 ++-- src/Operation/Cycle.php | 12 +++-- src/Operation/Diff.php | 5 +- src/Operation/DiffKeys.php | 5 +- src/Operation/Distinct.php | 16 +++--- src/Operation/Drop.php | 14 +++--- src/Operation/DropWhile.php | 29 ++++++----- src/Operation/Dump.php | 13 +++-- src/Operation/Duplicate.php | 16 +++--- src/Operation/Every.php | 33 ++++++------- src/Operation/Explode.php | 4 +- src/Operation/Falsy.php | 5 +- src/Operation/Filter.php | 15 +++--- src/Operation/Find.php | 11 ++--- src/Operation/First.php | 5 +- src/Operation/FlatMap.php | 7 ++- src/Operation/Flatten.php | 14 +++--- src/Operation/Flip.php | 5 +- src/Operation/FoldLeft.php | 11 ++--- src/Operation/FoldRight.php | 9 ++-- src/Operation/Forget.php | 5 +- src/Operation/Frequency.php | 5 +- src/Operation/Get.php | 9 ++-- src/Operation/Group.php | 9 ++-- src/Operation/GroupBy.php | 7 ++- src/Operation/Has.php | 13 +++-- src/Operation/Head.php | 9 ++-- src/Operation/IfThenElse.php | 21 ++++---- src/Operation/Implode.php | 4 +- src/Operation/Init.php | 10 ++-- src/Operation/Inits.php | 5 +- src/Operation/Intersect.php | 5 +- src/Operation/IntersectKeys.php | 5 +- src/Operation/Intersperse.php | 12 ++--- src/Operation/Key.php | 7 ++- src/Operation/Keys.php | 5 +- src/Operation/Last.php | 9 ++-- src/Operation/Limit.php | 14 +++--- src/Operation/Lines.php | 5 +- src/Operation/Map.php | 13 +++-- src/Operation/MapN.php | 17 +++---- src/Operation/MatchOne.php | 17 +++---- src/Operation/Matching.php | 7 ++- src/Operation/Merge.php | 12 +++-- src/Operation/Nth.php | 9 ++-- src/Operation/Nullsy.php | 5 +- src/Operation/Pack.php | 13 +++-- src/Operation/Pad.php | 13 +++-- src/Operation/Pair.php | 5 +- src/Operation/Partition.php | 5 +- src/Operation/Pipe.php | 21 ++++---- src/Operation/Prepend.php | 11 ++--- src/Operation/Product.php | 59 ++++++++++++----------- src/Operation/RSample.php | 11 ++--- src/Operation/Random.php | 9 ++-- src/Operation/Reduce.php | 11 ++--- src/Operation/Reduction.php | 17 +++---- src/Operation/Reject.php | 11 ++--- src/Operation/Reverse.php | 5 +- src/Operation/Scale.php | 15 +++--- src/Operation/ScanLeft.php | 15 +++--- src/Operation/ScanRight.php | 11 ++--- src/Operation/Shuffle.php | 11 ++--- src/Operation/Since.php | 29 ++++++----- src/Operation/Slice.php | 11 ++--- src/Operation/Span.php | 5 +- src/Operation/Split.php | 15 +++--- src/Operation/Tail.php | 5 +- src/Operation/Tails.php | 11 ++--- src/Operation/TakeWhile.php | 33 ++++++------- src/Operation/Transpose.php | 9 ++-- src/Operation/Truthy.php | 5 +- src/Operation/Unlines.php | 5 +- src/Operation/Unpack.php | 10 ++-- src/Operation/Unpair.php | 9 ++-- src/Operation/Until.php | 15 +++--- src/Operation/Unwindow.php | 5 +- src/Operation/Unwords.php | 5 +- src/Operation/Unwrap.php | 5 +- src/Operation/Unzip.php | 5 +- src/Operation/When.php | 19 ++++---- src/Operation/Window.php | 9 ++-- src/Operation/Words.php | 5 +- src/Operation/Wrap.php | 5 +- src/Operation/Zip.php | 40 +++++++-------- src/Utils/CallbacksArrayReducer.php | 13 +++-- 121 files changed, 554 insertions(+), 655 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 7eb4c65cf..f11b89633 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -133,7 +133,7 @@ use loophp\collection\Operation\Zip; use loophp\iterators\CachingIteratorAggregate; use loophp\iterators\ClosureIterator; -use loophp\iterators\IterableIterator; +use loophp\iterators\IterableIteratorAggregate; use loophp\iterators\ResourceIteratorAggregate; use loophp\iterators\StringIteratorAggregate; use NoRewindIterator; @@ -503,7 +503,7 @@ public static function fromGenerator(Generator $generator): self */ public static function fromIterable(iterable $iterable): self { - return new self(static fn (): Iterator => new IterableIterator($iterable)); + return new self(static fn (): IteratorAggregate => new IterableIteratorAggregate($iterable)); } /** diff --git a/src/Contract/Operation/Applyable.php b/src/Contract/Operation/Applyable.php index 259b57ae6..55b26e495 100644 --- a/src/Contract/Operation/Applyable.php +++ b/src/Contract/Operation/Applyable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -25,7 +24,7 @@ interface Applyable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#apply * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection */ diff --git a/src/Contract/Operation/Associateable.php b/src/Contract/Operation/Associateable.php index d54a4a3e3..1cef0082d 100644 --- a/src/Contract/Operation/Associateable.php +++ b/src/Contract/Operation/Associateable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -23,13 +22,10 @@ interface Associateable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#associate * - * @template NewT - * @template NewTKey + * @param callable(TKey=, T=, iterable=): mixed $callbackForKeys + * @param callable(T=, TKey=, iterable=): mixed $callbackForValues * - * @param callable(TKey=, T=, Iterator=): NewTKey $callbackForKeys - * @param callable(T=, TKey=, Iterator=): NewT $callbackForValues - * - * @return Collection + * @return Collection */ public function associate(?callable $callbackForKeys = null, ?callable $callbackForValues = null): Collection; } diff --git a/src/Contract/Operation/Duplicateable.php b/src/Contract/Operation/Duplicateable.php index 394e45538..5d6c7a319 100644 --- a/src/Contract/Operation/Duplicateable.php +++ b/src/Contract/Operation/Duplicateable.php @@ -32,10 +32,8 @@ interface Duplicateable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#duplicate * - * @template U - * - * @param null|callable(U): (Closure(U): bool) $comparatorCallback - * @param null|callable(T, TKey): U $accessorCallback + * @param (callable(mixed): (Closure(mixed): bool)) $comparatorCallback + * @param callable(T, TKey): mixed $accessorCallback * * @return Collection */ diff --git a/src/Contract/Operation/Filterable.php b/src/Contract/Operation/Filterable.php index be36479b7..14823598b 100644 --- a/src/Contract/Operation/Filterable.php +++ b/src/Contract/Operation/Filterable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -23,7 +22,7 @@ interface Filterable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#filter * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection */ diff --git a/src/Contract/Operation/Findable.php b/src/Contract/Operation/Findable.php index e703227a1..b72121236 100644 --- a/src/Contract/Operation/Findable.php +++ b/src/Contract/Operation/Findable.php @@ -9,8 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; - /** * @template TKey * @template T @@ -26,7 +24,7 @@ interface Findable * @template V * * @param V $default - * @param (callable(T=, TKey=, Iterator=): bool) ...$callbacks + * @param (callable(T=, TKey=, iterable=): bool) ...$callbacks * * @return T|V */ diff --git a/src/Contract/Operation/FlatMapable.php b/src/Contract/Operation/FlatMapable.php index d566f0342..773e5ab29 100644 --- a/src/Contract/Operation/FlatMapable.php +++ b/src/Contract/Operation/FlatMapable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -27,7 +26,7 @@ interface FlatMapable * @template IKey * @template IValue * - * @param callable(T=, TKey=, Iterator=): iterable $callback + * @param callable(T=, TKey=, iterable=): iterable $callback * * @return Collection */ diff --git a/src/Contract/Operation/FoldLeftable.php b/src/Contract/Operation/FoldLeftable.php index 8f6ecc1fb..d2078693c 100644 --- a/src/Contract/Operation/FoldLeftable.php +++ b/src/Contract/Operation/FoldLeftable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -24,7 +23,7 @@ interface FoldLeftable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#foldleft * - * @param callable(T, T, TKey, Iterator): T $callback + * @param callable(T, T, TKey, iterable): T $callback * @param T|null $initial * * @return Collection diff --git a/src/Contract/Operation/FoldRightable.php b/src/Contract/Operation/FoldRightable.php index 869b9bab1..3c7a99345 100644 --- a/src/Contract/Operation/FoldRightable.php +++ b/src/Contract/Operation/FoldRightable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -24,7 +23,7 @@ interface FoldRightable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#foldright * - * @param callable(T, T, TKey, Iterator): T $callback + * @param callable(T, T, TKey, iterable): T $callback * @param T|null $initial * * @return Collection diff --git a/src/Contract/Operation/Hasable.php b/src/Contract/Operation/Hasable.php index e336cd472..40a4b767a 100644 --- a/src/Contract/Operation/Hasable.php +++ b/src/Contract/Operation/Hasable.php @@ -9,8 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; - /** * @template TKey * @template T @@ -22,7 +20,7 @@ interface Hasable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#has * - * @param callable(T=, TKey=, Iterator=): T ...$callbacks + * @param callable(T=, TKey=, iterable=): T ...$callbacks */ public function has(callable ...$callbacks): bool; } diff --git a/src/Contract/Operation/MapNable.php b/src/Contract/Operation/MapNable.php index 9caacf2ba..e304708ce 100644 --- a/src/Contract/Operation/MapNable.php +++ b/src/Contract/Operation/MapNable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -23,7 +22,7 @@ interface MapNable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#mapn * - * @param callable(mixed, mixed, Iterator): mixed ...$callbacks + * @param callable(mixed, mixed, iterable): mixed ...$callbacks * * @return Collection */ diff --git a/src/Contract/Operation/Mapable.php b/src/Contract/Operation/Mapable.php index 1c9b49e81..6eeca586a 100644 --- a/src/Contract/Operation/Mapable.php +++ b/src/Contract/Operation/Mapable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -25,7 +24,7 @@ interface Mapable * * @template V * - * @param callable(T=, TKey=, Iterator=): V $callback + * @param callable(T=, TKey=, iterable=): V $callback * * @return Collection */ diff --git a/src/Contract/Operation/Matchable.php b/src/Contract/Operation/Matchable.php index be30a55f4..abd6d02a4 100644 --- a/src/Contract/Operation/Matchable.php +++ b/src/Contract/Operation/Matchable.php @@ -30,8 +30,8 @@ interface Matchable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#match * - * @param callable(T=, TKey=, Iterator=): bool $callback - * @param null|callable(T=, TKey=, Iterator=): bool $matcher + * @param callable(T=, TKey=, iterable=): bool $callback + * @param null|callable(T=, TKey=, iterable=): bool $matcher */ public function match(callable $callback, ?callable $matcher = null): bool; } diff --git a/src/Contract/Operation/Partitionable.php b/src/Contract/Operation/Partitionable.php index b4a6d184b..032b81b54 100644 --- a/src/Contract/Operation/Partitionable.php +++ b/src/Contract/Operation/Partitionable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -26,7 +25,7 @@ interface Partitionable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#partition * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection> */ diff --git a/src/Contract/Operation/Reduceable.php b/src/Contract/Operation/Reduceable.php index 71b8c49c7..f39d58adb 100644 --- a/src/Contract/Operation/Reduceable.php +++ b/src/Contract/Operation/Reduceable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -26,7 +25,7 @@ interface Reduceable * @template V * @template W * - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * @param V $initial * * @return Collection diff --git a/src/Contract/Operation/Reductionable.php b/src/Contract/Operation/Reductionable.php index 4e3fa1825..3c0971f66 100644 --- a/src/Contract/Operation/Reductionable.php +++ b/src/Contract/Operation/Reductionable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -26,7 +25,7 @@ interface Reductionable * @template V * @template W * - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * @param V $initial * * @return Collection diff --git a/src/Contract/Operation/Rejectable.php b/src/Contract/Operation/Rejectable.php index 6991dff4f..545a56fc6 100644 --- a/src/Contract/Operation/Rejectable.php +++ b/src/Contract/Operation/Rejectable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -23,7 +22,7 @@ interface Rejectable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#reject * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection */ diff --git a/src/Contract/Operation/ScanLeftable.php b/src/Contract/Operation/ScanLeftable.php index 8e581ccb8..3c624edd4 100644 --- a/src/Contract/Operation/ScanLeftable.php +++ b/src/Contract/Operation/ScanLeftable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -28,7 +27,7 @@ interface ScanLeftable * @template V * @template W * - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * @param V $initial * * @return Collection diff --git a/src/Contract/Operation/ScanRightable.php b/src/Contract/Operation/ScanRightable.php index 911c3517d..54f237d2e 100644 --- a/src/Contract/Operation/ScanRightable.php +++ b/src/Contract/Operation/ScanRightable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -28,7 +27,7 @@ interface ScanRightable * @template V * @template W * - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * @param V $initial * * @return Collection diff --git a/src/Contract/Operation/Spanable.php b/src/Contract/Operation/Spanable.php index cad99124e..97c986f8a 100644 --- a/src/Contract/Operation/Spanable.php +++ b/src/Contract/Operation/Spanable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -27,7 +26,7 @@ interface Spanable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#span * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection> */ diff --git a/src/Contract/Operation/TakeWhileable.php b/src/Contract/Operation/TakeWhileable.php index 3c8b1c46f..11b3a9bc5 100644 --- a/src/Contract/Operation/TakeWhileable.php +++ b/src/Contract/Operation/TakeWhileable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -24,7 +23,7 @@ interface TakeWhileable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#takewhile * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Collection */ diff --git a/src/Contract/Operation/Whenable.php b/src/Contract/Operation/Whenable.php index 430bbca14..2c945ae9b 100644 --- a/src/Contract/Operation/Whenable.php +++ b/src/Contract/Operation/Whenable.php @@ -9,7 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; use loophp\collection\Contract\Collection; /** @@ -27,9 +26,9 @@ interface Whenable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#when * - * @param callable(Iterator): bool $predicate - * @param callable(Iterator): iterable $whenTrue - * @param callable(Iterator): iterable $whenFalse + * @param callable(iterable): bool $predicate + * @param callable(iterable): iterable $whenTrue + * @param callable(iterable): iterable $whenFalse * * @return Collection */ diff --git a/src/Iterator/MultipleIterableIterator.php b/src/Iterator/MultipleIterableIterator.php index 5f29aa195..9480c2a2e 100644 --- a/src/Iterator/MultipleIterableIterator.php +++ b/src/Iterator/MultipleIterableIterator.php @@ -11,7 +11,7 @@ use AppendIterator; use Iterator; -use loophp\iterators\IterableIterator; +use loophp\iterators\IterableIteratorAggregate; use NoRewindIterator; use ReturnTypeWillChange; @@ -38,7 +38,7 @@ public function __construct(iterable ...$iterables) $appendIterator = new AppendIterator(); foreach ($iterables as $iterable) { - $appendIterator->append(new NoRewindIterator(new IterableIterator($iterable))); + $appendIterator->append(new NoRewindIterator((new IterableIteratorAggregate($iterable))->getIterator())); } $this->iterator = $appendIterator; diff --git a/src/Operation/All.php b/src/Operation/All.php index 7a99d6da5..d0b6f7a1e 100644 --- a/src/Operation/All.php +++ b/src/Operation/All.php @@ -10,7 +10,6 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; /** * @immutable @@ -23,20 +22,20 @@ final class All extends AbstractOperation /** * @pure * - * @return Closure(bool): Closure(Iterator): Iterator|Iterator + * @return Closure(bool): Closure(iterable): iterable|iterable */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Iterator|Iterator + * @return Closure(iterable): iterable|iterable */ static fn (bool $normalize): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator|Iterator + * @return iterable|iterable */ - static fn (Iterator $iterator): Iterator => $normalize ? (new Normalize())()($iterator) : $iterator; + static fn (iterable $iterable): iterable => $normalize ? (new Normalize())()($iterable) : $iterable; } } diff --git a/src/Operation/Append.php b/src/Operation/Append.php index 91525db39..b5d52214b 100644 --- a/src/Operation/Append.php +++ b/src/Operation/Append.php @@ -24,7 +24,7 @@ final class Append extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Iterator + * @return Closure(T...): Closure(iterable): Iterator */ public function __invoke(): Closure { @@ -32,14 +32,14 @@ public function __invoke(): Closure /** * @param T ...$items * - * @return Closure(Iterator): Iterator + * @return Closure(iterable): Iterator */ static fn (...$items): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Iterator */ - static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($iterator, $items); + static fn (iterable $iterable): Iterator => new MultipleIterableIterator($iterable, $items); } } diff --git a/src/Operation/Apply.php b/src/Operation/Apply.php index 05adfef21..5846fd91d 100644 --- a/src/Operation/Apply.php +++ b/src/Operation/Apply.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,26 +25,26 @@ final class Apply extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=):bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=):bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { - foreach ($iterator as $key => $value) { + static function (iterable $iterable) use ($callbacks): Generator { + foreach ($iterable as $key => $value) { foreach ($callbacks as $cKey => $callback) { - $result = $callback($value, $key, $iterator); + $result = $callback($value, $key, $iterable); if (false === $result) { unset($callbacks[$cKey]); diff --git a/src/Operation/Associate.php b/src/Operation/Associate.php index 53ab94d41..788281cb5 100644 --- a/src/Operation/Associate.php +++ b/src/Operation/Associate.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,31 +28,31 @@ final class Associate extends AbstractOperation * @template NewTKey * @template NewT * - * @return Closure(callable(mixed=, mixed=, Iterator=): mixed): Closure(callable(mixed=, mixed=, Iterator=): mixed): Closure(Iterator): Generator + * @return Closure(callable(mixed=, mixed=, iterable=): mixed): Closure(callable(mixed=, mixed=, iterable=): mixed): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(TKey=, T=, Iterator=): NewTKey $callbackForKeys + * @param callable(TKey=, T=, iterable=): NewTKey $callbackForKeys * - * @return Closure((callable(T=, TKey=, Iterator=): NewT)): Closure(Iterator): Generator + * @return Closure((callable(T=, TKey=, iterable=): NewT)): Closure(iterable): Generator */ static fn (callable $callbackForKeys): Closure => /** - * @param callable(T=, TKey=, Iterator=): NewT $callbackForValues + * @param callable(T=, TKey=, iterable=): NewT $callbackForValues * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable $callbackForValues): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues): Generator { - foreach ($iterator as $key => $value) { - yield $callbackForKeys($key, $value, $iterator) => $callbackForValues($value, $key, $iterator); + static function (iterable $iterable) use ($callbackForKeys, $callbackForValues): Generator { + foreach ($iterable as $key => $value) { + yield $callbackForKeys($key, $value, $iterable) => $callbackForValues($value, $key, $iterable); } }; } diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index 738fa3ffb..5e24a5a0b 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -9,7 +9,6 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; use Iterator; @@ -27,27 +26,27 @@ final class Chunk extends AbstractOperation /** * @pure * - * @return Closure(int...): Closure(Iterator): Generator> + * @return Closure(int...): Closure(iterable): Generator> */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static fn (int ...$sizes): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator> */ - static function (Iterator $iterator) use ($sizes): Generator { + static function (iterable $iterable) use ($sizes): Generator { /** @var Iterator $sizesIterator */ - $sizesIterator = Cycle::of()(new ArrayIterator($sizes)); + $sizesIterator = Cycle::of()($sizes); $values = []; - foreach ($iterator as $value) { + foreach ($iterable as $value) { $size = $sizesIterator->current(); if (0 >= $size) { diff --git a/src/Operation/Coalesce.php b/src/Operation/Coalesce.php index e3a51952d..3731ccd96 100644 --- a/src/Operation/Coalesce.php +++ b/src/Operation/Coalesce.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Coalesce extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Compact::of()(), Head::of(), diff --git a/src/Operation/Collapse.php b/src/Operation/Collapse.php index eb7ceb6e2..f990a9fa2 100644 --- a/src/Operation/Collapse.php +++ b/src/Operation/Collapse.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,7 +25,7 @@ final class Collapse extends AbstractOperation * * @psalm-suppress ImpureFunctionCall - using Filter and Flatten as an internal tools, not returned. * - * @return Closure(Iterator)>): Generator + * @return Closure(iterable)>): Generator */ public function __invoke(): Closure { @@ -36,7 +35,7 @@ public function __invoke(): Closure */ static fn ($value): bool => is_iterable($value); - /** @var Closure(Iterator)>): Generator $pipe */ + /** @var Closure(iterable)>): Generator $pipe */ $pipe = Pipe::of()( (new Filter())()($filterCallback), (new Flatten())()(1), diff --git a/src/Operation/Column.php b/src/Operation/Column.php index 3dd519780..f3fd6f91f 100644 --- a/src/Operation/Column.php +++ b/src/Operation/Column.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Column extends AbstractOperation /** * @pure * - * @return Closure(mixed): Closure(Iterator): Generator + * @return Closure(mixed): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -32,7 +31,7 @@ public function __invoke(): Closure /** * @param mixed $column * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($column): Closure { $filterCallbackBuilder = @@ -43,11 +42,10 @@ static function ($column): Closure { /** * @param T $value * @param TKey $key - * @param Iterator $iterator */ - static fn ($value, $key, Iterator $iterator): bool => $key === $column; + static fn ($value, $key): bool => $key === $column; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Transpose::of(), (new Filter())()($filterCallbackBuilder($column)), diff --git a/src/Operation/Combinate.php b/src/Operation/Combinate.php index 90b22b597..8a73e5f82 100644 --- a/src/Operation/Combinate.php +++ b/src/Operation/Combinate.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function array_slice; use function count; @@ -55,12 +54,12 @@ static function (array $dataset, int $length) use (&$getCombinations): Generator return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator> */ - static function (Iterator $iterator) use ($length, $getCombinations): Generator { - $dataset = [...$iterator]; + static function (iterable $iterable) use ($length, $getCombinations): Generator { + $dataset = [...$iterable]; if (0 < $length) { return yield from $getCombinations($dataset, $length); diff --git a/src/Operation/Combine.php b/src/Operation/Combine.php index 8b87549c2..aaba98c58 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -9,10 +9,9 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; use MultipleIterator; /** @@ -28,7 +27,7 @@ final class Combine extends AbstractOperation * * @template U * - * @return Closure(U...): Closure(Iterator): Generator + * @return Closure(U...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -36,33 +35,33 @@ public function __invoke(): Closure /** * @param U ...$keys * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$keys): Closure { $buildMultipleIterator = /** - * @param Iterator $keyIterator + * @param iterable $keyIterable */ - static function (Iterator $keyIterator): Closure { + static function (iterable $keyIterable): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return MultipleIterator */ - static function (Iterator $iterator) use ($keyIterator): MultipleIterator { + static function (iterable $iterable) use ($keyIterable): MultipleIterator { $mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY); - $mit->attachIterator($keyIterator); - $mit->attachIterator($iterator); + $mit->attachIterator((new IterableIteratorAggregate($keyIterable))->getIterator()); + $mit->attachIterator((new IterableIteratorAggregate($iterable))->getIterator()); return $mit; }; }; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( - $buildMultipleIterator(new ArrayIterator($keys)), + $buildMultipleIterator($keys), Flatten::of()(1), Pair::of(), ); diff --git a/src/Operation/Compact.php b/src/Operation/Compact.php index 1ab421431..e8c918551 100644 --- a/src/Operation/Compact.php +++ b/src/Operation/Compact.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -28,7 +27,7 @@ final class Compact extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Generator + * @return Closure(T...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -36,7 +35,7 @@ public function __invoke(): Closure /** * @param T ...$values * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$values): Closure { $filterCallback = diff --git a/src/Operation/Contains.php b/src/Operation/Contains.php index 1cf1d7a6e..7a1fff43f 100644 --- a/src/Operation/Contains.php +++ b/src/Operation/Contains.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Contains extends AbstractOperation /** * @pure * - * @return Closure(T ...$values): Closure(Iterator): Generator + * @return Closure(T ...$values): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -32,7 +31,7 @@ public function __invoke(): Closure /** * @param T ...$values * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$values): Closure { $callback = @@ -45,7 +44,7 @@ static function (...$values): Closure { */ static fn ($right): bool => $left === $right; - /** @var Closure(Iterator): Generator $matchOne */ + /** @var Closure(iterable): Generator $matchOne */ $matchOne = MatchOne::of()(static fn (): bool => true)(...array_map($callback, $values)); // Point free style. diff --git a/src/Operation/Current.php b/src/Operation/Current.php index 614590f99..5aabc84e5 100644 --- a/src/Operation/Current.php +++ b/src/Operation/Current.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,7 +25,7 @@ final class Current extends AbstractOperation * * @template V * - * @return Closure(TKey): Closure(V): Closure(Iterator): Generator + * @return Closure(TKey): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,16 +33,16 @@ public function __invoke(): Closure /** * @param TKey $index * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (int $index): Closure => /** * @param V $default * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($default) use ($index): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( (new Normalize())(), Get::of()($index)($default) diff --git a/src/Operation/Cycle.php b/src/Operation/Cycle.php index cc5f18364..b2be36ca5 100644 --- a/src/Operation/Cycle.php +++ b/src/Operation/Cycle.php @@ -10,8 +10,8 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; use loophp\iterators\InfiniteIteratorAggregate; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -24,16 +24,18 @@ final class Cycle extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => yield from new InfiniteIteratorAggregate($iterator); + static function (iterable $iterable): iterable { + yield from new InfiniteIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator()); + }; } } diff --git a/src/Operation/Diff.php b/src/Operation/Diff.php index cfae897cb..5c5104a4d 100644 --- a/src/Operation/Diff.php +++ b/src/Operation/Diff.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -26,7 +25,7 @@ final class Diff extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Generator + * @return Closure(T...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure /** * @param T ...$values * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$values): Closure { $filterCallbackFactory = diff --git a/src/Operation/DiffKeys.php b/src/Operation/DiffKeys.php index 480ce6741..5d8a5fe2c 100644 --- a/src/Operation/DiffKeys.php +++ b/src/Operation/DiffKeys.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -26,7 +25,7 @@ final class DiffKeys extends AbstractOperation /** * @pure * - * @return Closure(TKey...): Closure(Iterator): Generator + * @return Closure(TKey...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure /** * @param TKey ...$keys * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$keys): Closure { $filterCallbackFactory = diff --git a/src/Operation/Distinct.php b/src/Operation/Distinct.php index 1814d102a..a8f30b671 100644 --- a/src/Operation/Distinct.php +++ b/src/Operation/Distinct.php @@ -9,10 +9,8 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,7 +27,7 @@ final class Distinct extends AbstractOperation * * @template U * - * @return Closure(callable(U): Closure(U): bool): Closure(callable(T, TKey): U): Closure(Iterator): Generator + * @return Closure(callable(U): Closure(U): bool): Closure(callable(T, TKey): U): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -37,24 +35,24 @@ public function __invoke(): Closure /** * @param callable(U): (Closure(U): bool) $comparatorCallback * - * @return Closure(callable(T, TKey): U): Closure(Iterator): Generator + * @return Closure(callable(T, TKey): U): Closure(iterable): Generator */ static fn (callable $comparatorCallback): Closure => /** * @param callable(T, TKey): U $accessorCallback * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $accessorCallback) use ($comparatorCallback): Closure { - /** @var ArrayIterator $stack */ - $stack = new ArrayIterator(); + /** @var array $stack */ + $stack = []; $filter = (new Filter())()( /** * @param T $value * @param TKey $key */ - static function ($value, $key) use ($comparatorCallback, $accessorCallback, $stack): bool { + static function ($value, $key) use ($comparatorCallback, $accessorCallback, &$stack): bool { $matchWhenNot = static fn (): bool => true; $matcher = /** @@ -68,7 +66,7 @@ static function ($value, $key) use ($comparatorCallback, $accessorCallback, $sta return false; } - $stack->append([$key, $value]); + $stack[] = [$key, $value]; return true; } diff --git a/src/Operation/Drop.php b/src/Operation/Drop.php index 506183207..49853ffc7 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -10,34 +10,36 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; use LimitIterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Drop extends AbstractOperation { /** * @pure * - * @return Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(iterable): iterable */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (int $count): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => new LimitIterator($iterator, $count); + static fn (iterable $iterable): iterable => new LimitIterator((new IterableIteratorAggregate($iterable))->getIterator(), $count); } } diff --git a/src/Operation/DropWhile.php b/src/Operation/DropWhile.php index 222c27ac4..86c4883df 100644 --- a/src/Operation/DropWhile.php +++ b/src/Operation/DropWhile.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,33 +26,37 @@ final class DropWhile extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=):bool ...$callbacks + * @param callable(T=, TKey=, iterable=):bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { - foreach ($iterator as $key => $current) { - if (CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + static function (iterable $iterable) use ($callbacks): Generator { + $skip = false; + + foreach ($iterable as $key => $current) { + if (false === $skip) { + if (false === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { + $skip = true; + + yield $key => $current; + } + continue; } - break; - } - - for (; $iterator->valid(); $iterator->next()) { - yield $iterator->key() => $iterator->current(); + yield $key => $current; } }; } diff --git a/src/Operation/Dump.php b/src/Operation/Dump.php index d3c44017b..ecbdc32a3 100644 --- a/src/Operation/Dump.php +++ b/src/Operation/Dump.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use Symfony\Component\VarDumper\VarDumper; /** @@ -27,27 +26,27 @@ final class Dump extends AbstractOperation /** * @pure * - * @return Closure(string): Closure(int): Closure(?Closure): Closure(Iterator): Generator + * @return Closure(string): Closure(int): Closure(?Closure): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(int): Closure(?Closure): Closure(Iterator): Generator + * @return Closure(int): Closure(?Closure): Closure(iterable): Generator */ static fn (string $name = ''): Closure => /** - * @return Closure(?Closure): Closure(Iterator): Generator + * @return Closure(?Closure): Closure(iterable): Generator */ static fn (int $size = -1): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (?Closure $callback = null): Closure => /** * @return Generator */ - static function (Iterator $iterator) use ($name, $size, $callback): Generator { + static function (iterable $iterable) use ($name, $size, $callback): Generator { $j = 0; /** @var callable $debugFunction */ @@ -62,7 +61,7 @@ static function (Iterator $iterator) use ($name, $size, $callback): Generator { */ static fn (string $name, $key, $value) => $debugFunction(['name' => $name, 'key' => $key, 'value' => $value]); - foreach ($iterator as $key => $value) { + foreach ($iterable as $key => $value) { yield $key => $value; if (-1 === $size) { diff --git a/src/Operation/Duplicate.php b/src/Operation/Duplicate.php index ee93d1663..08e6f3719 100644 --- a/src/Operation/Duplicate.php +++ b/src/Operation/Duplicate.php @@ -9,10 +9,8 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,7 +27,7 @@ final class Duplicate extends AbstractOperation * * @template U * - * @return Closure(callable(U): Closure(U): bool): Closure(callable(T, TKey): U): Closure(Iterator): Generator + * @return Closure(callable(U): Closure(U): bool): Closure(callable(T, TKey): U): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -37,24 +35,24 @@ public function __invoke(): Closure /** * @param callable(U): (Closure(U): bool) $comparatorCallback * - * @return Closure(callable(T, TKey): U): Closure(Iterator): Generator + * @return Closure(callable(T, TKey): U): Closure(iterable): Generator */ static fn (callable $comparatorCallback): Closure => /** * @param callable(T, TKey): U $accessorCallback * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $accessorCallback) use ($comparatorCallback): Closure { - /** @var ArrayIterator $stack */ - $stack = new ArrayIterator(); + /** @var array $stack */ + $stack = []; $filter = (new Filter())()( /** * @param T $value * @param TKey $key */ - static function ($value, $key) use ($comparatorCallback, $accessorCallback, $stack): bool { + static function ($value, $key) use ($comparatorCallback, $accessorCallback, &$stack): bool { $matchWhenNot = static fn (): bool => true; $matcher = /** @@ -68,7 +66,7 @@ static function ($value, $key) use ($comparatorCallback, $accessorCallback, $sta return true; } - $stack->append([$key, $value]); + $stack[] = [$key, $value]; return false; } diff --git a/src/Operation/Every.php b/src/Operation/Every.php index bcf82de9d..0046509cd 100644 --- a/src/Operation/Every.php +++ b/src/Operation/Every.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,59 +26,59 @@ final class Every extends AbstractOperation /** * @pure * - * @return Closure(callable(T, TKey, Iterator...): bool): Closure(callable(T, TKey, Iterator...): bool): Closure(Iterator): Generator + * @return Closure(callable(T, TKey, iterable...): bool): Closure(callable(T, TKey, iterable...): bool): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$matchers + * @param callable(T=, TKey=, iterable=): bool ...$matchers * - * @return Closure(...callable(T=, TKey=, Iterator=): bool): Closure(Iterator): Generator + * @return Closure(...callable(T=, TKey=, iterable=): bool): Closure(iterable): Generator */ static function (callable ...$matchers): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable ...$callbacks) use ($matchers): Closure { $callbackReducer = /** - * @param list=): bool> $callbacks + * @param list=): bool> $callbacks * - * @return Closure(T, TKey, Iterator): bool + * @return Closure(T, TKey, iterable): bool */ static fn (array $callbacks): Closure => /** * @param T $current * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn ($current, $key, Iterator $iterator): bool => CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator); + static fn ($current, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable); $mapCallback = /** - * @param callable(T=, TKey=, Iterator=): mixed $reducer1 + * @param callable(T=, TKey=, iterable=): mixed $reducer1 * - * @return Closure(callable(T=, TKey=, Iterator=)): Closure(T, TKey, Iterator): bool + * @return Closure(callable(T=, TKey=, iterable=)): Closure(T, TKey, iterable): bool */ static fn (callable $reducer1): Closure => /** - * @param callable(T=, TKey=, Iterator=): mixed $reducer2 + * @param callable(T=, TKey=, iterable=): mixed $reducer2 * - * @return Closure(T, TKey, Iterator): bool + * @return Closure(T, TKey, iterable): bool */ static fn (callable $reducer2): Closure => /** * @param T $value * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn ($value, $key, Iterator $iterator): bool => $reducer1($value, $key, $iterator) !== $reducer2($value, $key, $iterator); + static fn ($value, $key, iterable $iterable): bool => $reducer1($value, $key, $iterable) !== $reducer2($value, $key, $iterable); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Map::of()($mapCallback($callbackReducer($callbacks))($callbackReducer($matchers))), DropWhile::of()(static fn (bool $value): bool => true === $value), diff --git a/src/Operation/Explode.php b/src/Operation/Explode.php index 27a3d1979..2146fe163 100644 --- a/src/Operation/Explode.php +++ b/src/Operation/Explode.php @@ -33,10 +33,10 @@ public function __invoke(): Closure /** * @param T ...$explodes * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static function (...$explodes): Closure { - /** @var Closure(Iterator): Generator> $split */ + /** @var Closure(iterable): Generator> $split */ $split = Split::of()(Splitable::REMOVE)( ...array_map( /** diff --git a/src/Operation/Falsy.php b/src/Operation/Falsy.php index 13a63cf8d..d708a33e4 100644 --- a/src/Operation/Falsy.php +++ b/src/Operation/Falsy.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Falsy extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -35,7 +34,7 @@ public function __invoke(): Closure */ static fn ($value): bool => (bool) $value; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( MatchOne::of()($matchWhenNot)($matcher), Map::of()( diff --git a/src/Operation/Filter.php b/src/Operation/Filter.php index a4be11dea..ba2c95489 100644 --- a/src/Operation/Filter.php +++ b/src/Operation/Filter.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,23 +26,23 @@ final class Filter extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { + static function (iterable $iterable) use ($callbacks): Generator { $defaultCallback = /** * @param T $value @@ -54,8 +53,8 @@ static function (Iterator $iterator) use ($callbacks): Generator { [$defaultCallback] : $callbacks; - foreach ($iterator as $key => $current) { - if (CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + foreach ($iterable as $key => $current) { + if (CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { yield $key => $current; } } diff --git a/src/Operation/Find.php b/src/Operation/Find.php index b316aee42..7c89e50d3 100644 --- a/src/Operation/Find.php +++ b/src/Operation/Find.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -28,7 +27,7 @@ final class Find extends AbstractOperation * * @template V * - * @return Closure(V): Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(V): Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -36,16 +35,16 @@ public function __invoke(): Closure /** * @param V $default * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ static fn ($default): Closure => /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable ...$callbacks) use ($default): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( (new Filter())()(...$callbacks), Append::of()($default), diff --git a/src/Operation/First.php b/src/Operation/First.php index d03c6270c..593da9456 100644 --- a/src/Operation/First.php +++ b/src/Operation/First.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class First extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $head */ + /** @var Closure(iterable): Generator $head */ $head = Head::of(); // Point free style. diff --git a/src/Operation/FlatMap.php b/src/Operation/FlatMap.php index f8df891d2..a410c4700 100644 --- a/src/Operation/FlatMap.php +++ b/src/Operation/FlatMap.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,16 +28,16 @@ final class FlatMap extends AbstractOperation * @template IKey * @template IValue * - * @return Closure(callable(T=, TKey=, Iterator=): iterable): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): iterable): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): iterable $callback + * @param callable(T=, TKey=, iterable=): iterable $callback */ static function (callable $callback): Closure { - /** @var Closure(Iterator): Generator $flatMap */ + /** @var Closure(iterable): Generator $flatMap */ $flatMap = Pipe::of()( Map::of()($callback), Flatten::of()(1) diff --git a/src/Operation/Flatten.php b/src/Operation/Flatten.php index 0cbce22f9..fc012c2ce 100644 --- a/src/Operation/Flatten.php +++ b/src/Operation/Flatten.php @@ -11,8 +11,6 @@ use Closure; use Generator; -use Iterator; -use loophp\iterators\IterableIterator; /** * @immutable @@ -25,20 +23,20 @@ final class Flatten extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (int $depth): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable */ - static function (Iterator $iterator) use ($depth): Generator { - foreach ($iterator as $key => $value) { + static function (iterable $iterable) use ($depth): Generator { + foreach ($iterable as $key => $value) { if (false === is_iterable($value)) { yield $key => $value; @@ -46,7 +44,7 @@ static function (Iterator $iterator) use ($depth): Generator { } yield from (1 !== $depth) - ? (new Flatten())()($depth - 1)(new IterableIterator($value)) + ? (new Flatten())()($depth - 1)($value) : $value; } }; diff --git a/src/Operation/Flip.php b/src/Operation/Flip.php index 098fcb9be..9d115b50c 100644 --- a/src/Operation/Flip.php +++ b/src/Operation/Flip.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Flip extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -46,7 +45,7 @@ public function __invoke(): Closure */ static fn ($value, $key) => $key; - /** @var Closure(Iterator): Generator $associate */ + /** @var Closure(iterable): Generator $associate */ $associate = Associate::of()($callbackForKeys)($callbackForValues); // Point free style. diff --git a/src/Operation/FoldLeft.php b/src/Operation/FoldLeft.php index 34238a764..fa79d647d 100644 --- a/src/Operation/FoldLeft.php +++ b/src/Operation/FoldLeft.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,24 +25,24 @@ final class FoldLeft extends AbstractOperation /** * @pure * - * @return Closure(callable((T|null), T, TKey, Iterator):(T|null)): Closure(T): Closure(Iterator): Generator + * @return Closure(callable((T|null), T, TKey, iterable):(T|null)): Closure(T): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T|null, T, TKey, Iterator):(T|null) $callback + * @param callable(T|null, T, TKey, iterable):(T|null) $callback * - * @return Closure(T): Closure(Iterator): Generator + * @return Closure(T): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param T|null $initial * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($initial = null) use ($callback): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( ScanLeft::of()($callback)($initial), Last::of() diff --git a/src/Operation/FoldRight.php b/src/Operation/FoldRight.php index 1c66c68a6..099cd450b 100644 --- a/src/Operation/FoldRight.php +++ b/src/Operation/FoldRight.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,18 +25,18 @@ final class FoldRight extends AbstractOperation /** * @pure * - * @return Closure(callable((T|null), T, TKey, Iterator):(T|null)): Closure(T): Closure(Iterator): Generator + * @return Closure(callable((T|null), T, TKey, iterable):(T|null)): Closure(T): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T|null, T, TKey, Iterator):(T|null) $callback + * @param callable(T|null, T, TKey, iterable):(T|null) $callback * - * @return Closure(T): Closure(Iterator): Generator + * @return Closure(T): Closure(iterable): Generator */ static fn (callable $callback): Closure => static function ($initial = null) use ($callback): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( ScanRight::of()($callback)($initial), Head::of() diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index dde0c7097..6387f46a7 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -26,7 +25,7 @@ final class Forget extends AbstractOperation /** * @pure * - * @return Closure(TKey...): Closure(Iterator): Generator + * @return Closure(TKey...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure /** * @param TKey ...$keys * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$keys): Closure { $filterCallbackFactory = diff --git a/src/Operation/Frequency.php b/src/Operation/Frequency.php index bab9bdfd4..0334544a8 100644 --- a/src/Operation/Frequency.php +++ b/src/Operation/Frequency.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Frequency extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -56,7 +55,7 @@ static function (array $storage, $value): array { return $storage; }; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Reduce::of()($reduceCallback)([]), Flatten::of()(1), diff --git a/src/Operation/Get.php b/src/Operation/Get.php index 8108d9919..06627354e 100644 --- a/src/Operation/Get.php +++ b/src/Operation/Get.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,7 +25,7 @@ final class Get extends AbstractOperation * * @template V * - * @return Closure(TKey): Closure(V): Closure(Iterator): Generator + * @return Closure(TKey): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,13 +33,13 @@ public function __invoke(): Closure /** * @param TKey $keyToGet * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn ($keyToGet): Closure => /** * @param V $default * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($default) use ($keyToGet): Closure { $filterCallback = @@ -50,7 +49,7 @@ static function ($default) use ($keyToGet): Closure { */ static fn ($value, $key): bool => $key === $keyToGet; - /** @var Closure(Iterator): (Generator) $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( (new Filter())()($filterCallback), Append::of()($default), diff --git a/src/Operation/Group.php b/src/Operation/Group.php index b0906d4d9..d94fc4367 100644 --- a/src/Operation/Group.php +++ b/src/Operation/Group.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,20 +23,20 @@ final class Group extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator> */ - static function (Iterator $iterator): Generator { + static function (iterable $iterable): Generator { $last = []; - foreach ($iterator as $current) { + foreach ($iterable as $current) { if ([] === $last) { $last = [$current]; diff --git a/src/Operation/GroupBy.php b/src/Operation/GroupBy.php index 4dbf63a86..b61b54a94 100644 --- a/src/Operation/GroupBy.php +++ b/src/Operation/GroupBy.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -28,7 +27,7 @@ final class GroupBy extends AbstractOperation * * @template NewTKey of array-key * - * @return Closure(callable(T=, TKey=): NewTKey):Closure(Iterator): Generator> + * @return Closure(callable(T=, TKey=): NewTKey): Closure(iterable): Generator> */ public function __invoke(): Closure { @@ -36,7 +35,7 @@ public function __invoke(): Closure /** * @param callable(T=, TKey=): NewTKey $callable * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static function (callable $callable): Closure { $reducerFactory = @@ -59,7 +58,7 @@ static function (array $collect, $value, $key) use ($callback): array { return $collect; }; - /** @var Closure(Iterator): Generator> $pipe */ + /** @var Closure(iterable): Generator> $pipe */ $pipe = Pipe::of()( Reduce::of()($reducerFactory($callable))([]), Flatten::of()(1) diff --git a/src/Operation/Has.php b/src/Operation/Has.php index 056bfa649..769344821 100644 --- a/src/Operation/Has.php +++ b/src/Operation/Has.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,27 +25,27 @@ final class Has extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): T ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): T ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): T ...$callbacks + * @param callable(T=, TKey=, iterable=): T ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable ...$callbacks): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = MatchOne::of()(static fn (): bool => true)( ...array_map( static fn (callable $callback): callable => /** * @param T $value * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn ($value, $key, Iterator $iterator): bool => $callback($value, $key, $iterator) === $value, + static fn ($value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) === $value, $callbacks ) ); diff --git a/src/Operation/Head.php b/src/Operation/Head.php index 99c847403..3dc22e373 100644 --- a/src/Operation/Head.php +++ b/src/Operation/Head.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,22 +23,22 @@ final class Head extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator): Generator { + static function (iterable $iterable): Generator { $isEmpty = true; $key = $current = null; - foreach ($iterator as $key => $current) { + foreach ($iterable as $key => $current) { $isEmpty = false; break; diff --git a/src/Operation/IfThenElse.php b/src/Operation/IfThenElse.php index 5828b9ad1..1e2a5e52a 100644 --- a/src/Operation/IfThenElse.php +++ b/src/Operation/IfThenElse.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,39 +25,39 @@ final class IfThenElse extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool): Closure(callable(T=, TKey=, Iterator=): T): Closure(callable(T=, TKey=, Iterator=): T): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool): Closure(callable(T=, TKey=, iterable=): T): Closure(callable(T=, TKey=, iterable=): T): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool $condition + * @param callable(T=, TKey=, iterable=): bool $condition * - * @return Closure(callable(T=, TKey=, Iterator=): T): Closure(callable(T=, TKey=, Iterator=): T): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): T): Closure(callable(T=, TKey=, iterable=): T): Closure(iterable): Generator */ static fn (callable $condition): Closure => /** - * @param callable(T=, TKey=, Iterator=): T $then + * @param callable(T=, TKey=, iterable=): T $then * - * @return Closure(callable(T=, TKey=, Iterator=): T): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): T): Closure(iterable): Generator */ static fn (callable $then): Closure => /** - * @param callable(T=, TKey=, Iterator=):T $else + * @param callable(T=, TKey=, iterable=):T $else * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $else) use ($condition, $then): Closure { - /** @var Closure(Iterator): Generator $map */ + /** @var Closure(iterable): Generator $map */ $map = Map::of()( /** * @param T $value * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable * * @return T */ - static fn ($value, $key, Iterator $iterator) => $condition($value, $key, $iterator) ? $then($value, $key, $iterator) : $else($value, $key, $iterator) + static fn ($value, $key, iterable $iterable) => $condition($value, $key, $iterable) ? $then($value, $key, $iterable) : $else($value, $key, $iterable) ); // Point free style. diff --git a/src/Operation/Implode.php b/src/Operation/Implode.php index eacf6732f..e15c1f302 100644 --- a/src/Operation/Implode.php +++ b/src/Operation/Implode.php @@ -32,7 +32,7 @@ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (string $glue): Closure { $reducer = @@ -41,7 +41,7 @@ static function (string $glue): Closure { */ static fn (string $carry, $item): string => $carry .= $item; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Intersperse::of()($glue)(1)(0), Drop::of()(1), diff --git a/src/Operation/Init.php b/src/Operation/Init.php index bc4c334f6..8edc2526f 100644 --- a/src/Operation/Init.php +++ b/src/Operation/Init.php @@ -12,7 +12,7 @@ use CachingIterator; use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -27,7 +27,7 @@ final class Init extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -41,13 +41,13 @@ public function __invoke(): Closure $buildCachingIterator = /** - * @param Iterator $iterator + * @param iterable $iterable * * @return CachingIterator */ - static fn (Iterator $iterator): CachingIterator => new CachingIterator($iterator, CachingIterator::FULL_CACHE); + static fn (iterable $iterator): CachingIterator => new CachingIterator((new IterableIteratorAggregate($iterator))->getIterator(), CachingIterator::FULL_CACHE); - /** @var Closure(Iterator): Generator $takeWhile */ + /** @var Closure(iterable): Generator $takeWhile */ $takeWhile = Pipe::of()( $buildCachingIterator, TakeWhile::of()($callback) diff --git a/src/Operation/Inits.php b/src/Operation/Inits.php index b612b3794..67cbfecdd 100644 --- a/src/Operation/Inits.php +++ b/src/Operation/Inits.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Inits extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ public function __invoke(): Closure { @@ -41,7 +40,7 @@ static function (array $carry, array $value): array { return $carry; }; - /** @var Closure(Iterator): Generator> $inits */ + /** @var Closure(iterable): Generator> $inits */ $inits = Pipe::of()( Pack::of(), ScanLeft::of()($scanLeftCallback)([]), diff --git a/src/Operation/Intersect.php b/src/Operation/Intersect.php index 5797f53a1..a93800091 100644 --- a/src/Operation/Intersect.php +++ b/src/Operation/Intersect.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -26,7 +25,7 @@ final class Intersect extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Generator + * @return Closure(T...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure /** * @param T ...$values * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$values): Closure { $filterCallbackFactory = diff --git a/src/Operation/IntersectKeys.php b/src/Operation/IntersectKeys.php index 2b4123181..7b573df85 100644 --- a/src/Operation/IntersectKeys.php +++ b/src/Operation/IntersectKeys.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -26,7 +25,7 @@ final class IntersectKeys extends AbstractOperation /** * @pure * - * @return Closure(TKey...): Closure(Iterator): Generator + * @return Closure(TKey...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure /** * @param TKey ...$keys * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$keys): Closure { $filterCallbackFactory = diff --git a/src/Operation/Intersperse.php b/src/Operation/Intersperse.php index dae43258d..1dbc7b5fa 100644 --- a/src/Operation/Intersperse.php +++ b/src/Operation/Intersperse.php @@ -27,7 +27,7 @@ final class Intersperse extends AbstractOperation /** * @pure * - * @return Closure(T): Closure(int): Closure(int): Closure(Iterator): Generator + * @return Closure(T): Closure(int): Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -39,19 +39,19 @@ public function __invoke(): Closure */ static fn ($element): Closure => /** - * @return Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(iterable): Generator */ static fn (int $atEvery): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (int $startAt): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($element, $atEvery, $startAt): Generator { + static function (iterable $iterable) use ($element, $atEvery, $startAt): Generator { $intersperse = new AppendIterator(); $intersperse->append( new ArrayIterator(array_fill(0, $startAt, 1)) @@ -61,7 +61,7 @@ static function (Iterator $iterator) use ($element, $atEvery, $startAt): Generat ); $intersperse->rewind(); - foreach ($iterator as $key => $value) { + foreach ($iterable as $key => $value) { if (0 === $intersperse->current()) { yield $element; } diff --git a/src/Operation/Key.php b/src/Operation/Key.php index 7289e274f..421beb922 100644 --- a/src/Operation/Key.php +++ b/src/Operation/Key.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,16 +23,16 @@ final class Key extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (int $index): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Limit::of()(1)($index), Flip::of() diff --git a/src/Operation/Keys.php b/src/Operation/Keys.php index 2cb26d785..dcd60bfcc 100644 --- a/src/Operation/Keys.php +++ b/src/Operation/Keys.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Keys extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Flip::of(), (new Normalize())() diff --git a/src/Operation/Last.php b/src/Operation/Last.php index 7dde462f0..dca40c826 100644 --- a/src/Operation/Last.php +++ b/src/Operation/Last.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,22 +23,22 @@ final class Last extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator): Generator { + static function (iterable $iterable): Generator { $isEmpty = true; $key = $current = null; - foreach ($iterator as $key => $current) { + foreach ($iterable as $key => $current) { $isEmpty = false; } diff --git a/src/Operation/Limit.php b/src/Operation/Limit.php index 1f326a7ee..d563a4383 100644 --- a/src/Operation/Limit.php +++ b/src/Operation/Limit.php @@ -10,8 +10,8 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; use LimitIterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -26,24 +26,24 @@ final class Limit extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(int): Closure(iterable): iterable */ public function __invoke(): Closure { return /** - * @return Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(iterable): iterable */ static fn (int $count = -1): Closure => /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (int $offset = 0): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => new LimitIterator($iterator, $offset, $count); + static fn (iterable $iterable): iterable => new LimitIterator((new IterableIteratorAggregate($iterable))->getIterator(), $offset, $count); } } diff --git a/src/Operation/Lines.php b/src/Operation/Lines.php index 0499d9956..4acb112af 100644 --- a/src/Operation/Lines.php +++ b/src/Operation/Lines.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use const PHP_EOL; @@ -26,7 +25,7 @@ final class Lines extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -36,7 +35,7 @@ public function __invoke(): Closure */ static fn (array $value): string => implode('', $value); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Explode::of()(PHP_EOL, "\n", "\r\n"), Map::of()($mapCallback) diff --git a/src/Operation/Map.php b/src/Operation/Map.php index ab10965df..246fa6ee4 100644 --- a/src/Operation/Map.php +++ b/src/Operation/Map.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -28,23 +27,23 @@ final class Map extends AbstractOperation * * @template V * - * @return Closure(callable(T=, TKey=, Iterator=): V): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): V $callback + * @param callable(T=, TKey=, iterable=): V $callback */ static fn (callable $callback): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callback): Generator { - foreach ($iterator as $key => $value) { - yield $key => $callback($value, $key, $iterator); + static function (iterable $iterable) use ($callback): Generator { + foreach ($iterable as $key => $value) { + yield $key => $callback($value, $key, $iterable); } }; } diff --git a/src/Operation/MapN.php b/src/Operation/MapN.php index 6779f01a5..3e6d0f09e 100644 --- a/src/Operation/MapN.php +++ b/src/Operation/MapN.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,37 +25,37 @@ final class MapN extends AbstractOperation /** * @pure * - * @return Closure(callable(mixed, mixed, Iterator): mixed ...): Closure(Iterator): Generator + * @return Closure(callable(mixed, mixed, iterable): mixed ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(mixed, mixed, Iterator): mixed ...$callbacks + * @param callable(mixed, mixed, iterable): mixed ...$callbacks */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { + static function (iterable $iterable) use ($callbacks): Generator { $callbackFactory = /** * @param mixed $key * - * @return Closure(mixed, callable(mixed, mixed, Iterator): mixed): mixed + * @return Closure(mixed, callable(mixed, mixed, iterable): mixed): mixed */ static fn ($key): Closure => /** * @param mixed $carry - * @param callable(mixed, mixed, Iterator): mixed $callback + * @param callable(mixed, mixed, iterable): mixed $callback * * @return mixed */ - static fn ($carry, callable $callback) => $callback($carry, $key, $iterator); + static fn ($carry, callable $callback) => $callback($carry, $key, $iterable); - foreach ($iterator as $key => $value) { + foreach ($iterable as $key => $value) { yield $key => array_reduce($callbacks, $callbackFactory($key), $value); } }; diff --git a/src/Operation/MatchOne.php b/src/Operation/MatchOne.php index 620a88c51..d9a4c0c77 100644 --- a/src/Operation/MatchOne.php +++ b/src/Operation/MatchOne.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,33 +26,33 @@ final class MatchOne extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$matchers + * @param callable(T=, TKey=, iterable=): bool ...$matchers * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ static function (callable ...$matchers): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable ...$callbacks) use ($matchers): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Map::of()( /** * @param T $value * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn ($value, $key, Iterator $iterator): bool => CallbacksArrayReducer::or()($callbacks, $value, $key, $iterator) === CallbacksArrayReducer::or()($matchers, $value, $key, $iterator) + static fn ($value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks, $value, $key, $iterable) === CallbacksArrayReducer::or()($matchers, $value, $key, $iterable) ), DropWhile::of()(static fn (bool $value): bool => false === $value), Append::of()(false), diff --git a/src/Operation/Matching.php b/src/Operation/Matching.php index a01444a92..4d03db518 100644 --- a/src/Operation/Matching.php +++ b/src/Operation/Matching.php @@ -13,7 +13,6 @@ use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; use Generator; -use Iterator; use loophp\collection\Contract\Operation\Sortable; /** @@ -29,13 +28,13 @@ final class Matching extends AbstractOperation /** * @pure * - * @return Closure(Criteria): Closure(Iterator): Generator + * @return Closure(Criteria): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (Criteria $criteria): Closure { $expr = $criteria->getWhereExpression(); @@ -64,7 +63,7 @@ static function (Criteria $criteria): Closure { $pipes[] = Limit::of()($length)((int) $offset); } - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()(...$pipes); // Point free style. diff --git a/src/Operation/Merge.php b/src/Operation/Merge.php index 2ddc50ce7..6b6ca67b4 100644 --- a/src/Operation/Merge.php +++ b/src/Operation/Merge.php @@ -10,7 +10,7 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; +use Generator; use loophp\collection\Iterator\MultipleIterableIterator; /** @@ -18,13 +18,15 @@ * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Merge extends AbstractOperation { /** * @pure * - * @return Closure(iterable...): Closure(Iterator): Iterator + * @return Closure(iterable...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,10 +36,10 @@ public function __invoke(): Closure */ static fn (iterable ...$sources): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return Generator */ - static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($iterator, ...$sources); + static fn (iterable $iterable): Generator => yield from new MultipleIterableIterator($iterable, ...$sources); } } diff --git a/src/Operation/Nth.php b/src/Operation/Nth.php index 899b66540..a808ffb2c 100644 --- a/src/Operation/Nth.php +++ b/src/Operation/Nth.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,17 +23,17 @@ final class Nth extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(iterable): Generator */ static fn (int $step): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (int $offset) use ($step): Closure { $filterCallback = @@ -43,7 +42,7 @@ static function (int $offset) use ($step): Closure { */ static fn (array $value, int $key): bool => (($key % $step) === $offset); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Pack::of(), (new Filter())()($filterCallback), diff --git a/src/Operation/Nullsy.php b/src/Operation/Nullsy.php index 45383d2ca..ba0cfc717 100644 --- a/src/Operation/Nullsy.php +++ b/src/Operation/Nullsy.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function in_array; @@ -33,7 +32,7 @@ final class Nullsy extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -44,7 +43,7 @@ public function __invoke(): Closure */ static fn ($value): bool => in_array($value, self::VALUES, true); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( MatchOne::of()($matchWhenNot)($matcher), Map::of()( diff --git a/src/Operation/Pack.php b/src/Operation/Pack.php index f73183500..d1dbb4916 100644 --- a/src/Operation/Pack.php +++ b/src/Operation/Pack.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\iterators\PackIterableAggregate; /** @@ -25,21 +24,21 @@ final class Pack extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator): Generator { - /** @var PackIterableAggregate $packIterableAggregate */ - $packIterableAggregate = new PackIterableAggregate($iterator); + static function (iterable $iterable): Generator { + /** @var PackIterableAggregate $iterable */ + $iterable = new PackIterableAggregate($iterable); - yield from $packIterableAggregate->getIterator(); + yield from $iterable->getIterator(); }; } } diff --git a/src/Operation/Pad.php b/src/Operation/Pad.php index 7f305e7b4..9e36abc8e 100644 --- a/src/Operation/Pad.php +++ b/src/Operation/Pad.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,30 +23,30 @@ final class Pad extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(T): Closure(Iterator): Generator + * @return Closure(int): Closure(T): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(T): Closure(Iterator): Generator + * @return Closure(T): Closure(iterable): Generator */ static fn (int $size): Closure => /** * @param T $padValue * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn ($padValue): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($size, $padValue): Generator { + static function (iterable $iterable) use ($size, $padValue): Generator { $y = 0; - foreach ($iterator as $key => $value) { + foreach ($iterable as $key => $value) { ++$y; yield $key => $value; diff --git a/src/Operation/Pair.php b/src/Operation/Pair.php index d664f943d..345e088d6 100644 --- a/src/Operation/Pair.php +++ b/src/Operation/Pair.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Pair extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -45,7 +44,7 @@ public function __invoke(): Closure */ static fn (array $value) => $value[1] ?? null; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Chunk::of()(2), Map::of()(static fn (array $value): array => array_values($value)), diff --git a/src/Operation/Partition.php b/src/Operation/Partition.php index a005e1cc7..ebb7689f8 100644 --- a/src/Operation/Partition.php +++ b/src/Operation/Partition.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use IteratorAggregate; use loophp\iterators\ClosureIteratorAggregate; @@ -28,12 +27,12 @@ final class Partition extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(IteratorAggregate): Generator> + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(IteratorAggregate): Generator> */ public function __invoke(): Closure { /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Closure(IteratorAggregate): Generator> */ diff --git a/src/Operation/Pipe.php b/src/Operation/Pipe.php index f7fad120d..9d96f3089 100644 --- a/src/Operation/Pipe.php +++ b/src/Operation/Pipe.php @@ -10,7 +10,6 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; /** * @immutable @@ -25,32 +24,32 @@ final class Pipe extends AbstractOperation /** * @pure * - * @return Closure(callable(Iterator): Iterator ...): Closure(Iterator): Iterator + * @return Closure(callable(iterable): iterable ...): Closure(iterable): iterable */ public function __invoke(): Closure { return /** - * @param callable(Iterator): Iterator ...$operations + * @param callable(iterable): iterable ...$operations * - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (callable ...$operations): Closure => array_reduce( $operations, /** - * @param callable(Iterator): Iterator $f - * @param callable(Iterator): Iterator $g + * @param callable(iterable): iterable $f + * @param callable(iterable): iterable $g * - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (callable $f, callable $g): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => $g($f($iterator)), - static fn (Iterator $iterator): Iterator => $iterator + static fn (iterable $iterable): iterable => $g($f($iterable)), + static fn (iterable $iterable): iterable => $iterable ); } } diff --git a/src/Operation/Prepend.php b/src/Operation/Prepend.php index a2fe58633..a23cbdbe8 100644 --- a/src/Operation/Prepend.php +++ b/src/Operation/Prepend.php @@ -10,7 +10,6 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; use loophp\collection\Iterator\MultipleIterableIterator; /** @@ -24,7 +23,7 @@ final class Prepend extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Iterator + * @return Closure(T...): Closure(iterable): iterable */ public function __invoke(): Closure { @@ -32,14 +31,14 @@ public function __invoke(): Closure /** * @param T ...$items * - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (...$items): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($items, $iterator); + static fn (iterable $iterable): iterable => new MultipleIterableIterator($items, $iterable); } } diff --git a/src/Operation/Product.php b/src/Operation/Product.php index d020ee1b9..6f8620046 100644 --- a/src/Operation/Product.php +++ b/src/Operation/Product.php @@ -9,17 +9,18 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; -use Iterator; -use loophp\iterators\IterableIterator; +use loophp\iterators\IterableIteratorAggregate; +use Traversable; /** * @immutable * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Product extends AbstractOperation { @@ -35,60 +36,60 @@ public function __invoke(): Closure /** * @param iterable ...$iterables * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static function (iterable ...$iterables): Closure { - /** @var Closure(Iterator): Generator> $pipe */ + /** @var Closure(iterable): Generator> $pipe */ $pipe = Pipe::of()( ( /** - * @param list> $iterables + * @param array> $iterables */ static fn (array $iterables): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable */ - static fn (Iterator $iterator): Generator => ( + static fn (iterable $iterable): Generator => ( /** - * @param Closure(Iterator): (Closure(Iterator): Generator>) $f + * @param Closure(iterable): (Closure(iterable): Generator>) $f */ static fn (Closure $f): Closure => (new FoldLeft())()( /** - * @param Iterator $a - * @param Iterator $x + * @param iterable $a + * @param iterable $x */ - static fn (Iterator $a, Iterator $x): Generator => $f($x)($a) + static fn (iterable $a, iterable $x): Generator => $f($x)($a) ) )( /** - * @param (Iterator|Iterator) $xs - */ - static fn (Iterator $xs): Closure => - /** - * @param Iterator> $as + * @param (iterable|iterable) $xs */ - static fn (Iterator $as): Generator => (new FlatMap())()( + static fn (iterable $xs): Closure => /** - * @param list $a + * @param iterable> $as */ - static fn (array $a): Generator => (new FlatMap())()( + static fn (iterable $as): Generator => (new FlatMap())()( /** - * @param T|U $x - * - * @return Generator> + * @param array $a */ - static fn ($x): Generator => yield [...$a, $x] - )($xs) - )($as) - )(new ArrayIterator([[]]))(new ArrayIterator([$iterator, ...$iterables])) + static fn (array $a): Generator => (new FlatMap())()( + /** + * @param T|U $x + * + * @return Generator> + */ + static fn ($x): Generator => yield [...$a, $x] + )($xs) + )($as) + )([[]])([$iterable, ...$iterables]) )( array_map( /** * @param iterable $iterable * - * @return Iterator + * @return IterableIteratorAggregate */ - static fn (iterable $iterable): Iterator => new IterableIterator($iterable), + static fn (iterable $iterable): IterableIteratorAggregate => new IterableIteratorAggregate($iterable), $iterables ) ), diff --git a/src/Operation/RSample.php b/src/Operation/RSample.php index 464a08f02..383a9b7ea 100644 --- a/src/Operation/RSample.php +++ b/src/Operation/RSample.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,18 +25,18 @@ final class RSample extends AbstractOperation /** * @pure * - * @return Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (float $probability): Closure { - $callback = static fn (): bool => (mt_rand() / mt_getrandmax()) < $probability; - - $filter = (new Filter())()($callback); + $filter = (new Filter())()( + static fn (): bool => (mt_rand() / mt_getrandmax()) < $probability + ); // Point free style. return $filter; diff --git a/src/Operation/Random.php b/src/Operation/Random.php index 2c620968d..c4caabff9 100644 --- a/src/Operation/Random.php +++ b/src/Operation/Random.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,21 +23,21 @@ final class Random extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(iterable): Generator */ static function (int $seed): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (int $size) use ($seed): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Shuffle::of()($seed), Limit::of()($size)(0) diff --git a/src/Operation/Reduce.php b/src/Operation/Reduce.php index f93364f9c..345189acf 100644 --- a/src/Operation/Reduce.php +++ b/src/Operation/Reduce.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,24 +28,24 @@ final class Reduce extends AbstractOperation * @template V * @template W * - * @return Closure(callable(mixed=, mixed=, mixed=, Iterator=): mixed): Closure(mixed): Closure(Iterator): Generator + * @return Closure(callable(mixed=, mixed=, mixed=, iterable=): mixed): Closure(mixed): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param V $initial * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($initial) use ($callback): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( (new Reduction())()($callback)($initial), Last::of(), diff --git a/src/Operation/Reduction.php b/src/Operation/Reduction.php index b8da47bb8..753e0f9cf 100644 --- a/src/Operation/Reduction.php +++ b/src/Operation/Reduction.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,31 +28,31 @@ final class Reduction extends AbstractOperation * @template V * @template W * - * @return Closure(callable(mixed=, mixed=, mixed=, Iterator=): mixed): Closure(mixed): Closure(Iterator): Generator + * @return Closure(callable(mixed=, mixed=, mixed=, iterable=): mixed): Closure(mixed): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param V $initial * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn ($initial): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - foreach ($iterator as $key => $value) { - yield $key => ($initial = $callback($initial, $value, $key, $iterator)); + static function (iterable $iterable) use ($callback, $initial): Generator { + foreach ($iterable as $key => $value) { + yield $key => ($initial = $callback($initial, $value, $key, $iterable)); } }; } diff --git a/src/Operation/Reject.php b/src/Operation/Reject.php index 4aa855220..6351771b8 100644 --- a/src/Operation/Reject.php +++ b/src/Operation/Reject.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,15 +26,15 @@ final class Reject extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable ...$callbacks): Closure { $defaultCallback = @@ -52,9 +51,9 @@ static function (callable ...$callbacks): Closure { /** * @param T $current * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn ($current, $key, Iterator $iterator): bool => !CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator) + static fn ($current, $key, iterable $iterable): bool => !CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable) ); // Point free style. diff --git a/src/Operation/Reverse.php b/src/Operation/Reverse.php index cb895bf29..82b8c46cc 100644 --- a/src/Operation/Reverse.php +++ b/src/Operation/Reverse.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Reverse extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -37,7 +36,7 @@ public function __invoke(): Closure */ static fn (array $carry, array $value): array => [...$value, ...$carry]; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( (new Pack())(), Reduce::of()($callback)([]), diff --git a/src/Operation/Scale.php b/src/Operation/Scale.php index 6d3928cf6..3f6110385 100644 --- a/src/Operation/Scale.php +++ b/src/Operation/Scale.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use const INF; @@ -28,29 +27,29 @@ final class Scale extends AbstractOperation /** * @pure * - * @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(float): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(float): Closure(float): Closure(float): Closure(iterable): Generator */ static fn (float $lowerBound): Closure => /** - * @return Closure(float): Closure(float): Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(float): Closure(float): Closure(iterable): Generator */ static fn (float $upperBound): Closure => /** - * @return Closure(float): Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(float): Closure(iterable): Generator */ static fn (float $wantedLowerBound = 0.0): Closure => /** - * @return Closure(float): Closure(Iterator): Generator + * @return Closure(float): Closure(iterable): Generator */ static fn (float $wantedUpperBound = 1.0): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { $wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; @@ -82,7 +81,7 @@ static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUp static fn ($item): bool => $item <= $upperBound ); - /** @var Closure(Iterator):(Generator) $pipe */ + /** @var Closure(iterable):(Generator) $pipe */ $pipe = Pipe::of()($filter, $mapper); // Point free style. diff --git a/src/Operation/ScanLeft.php b/src/Operation/ScanLeft.php index 0e4bce0ca..8fd40f66f 100644 --- a/src/Operation/ScanLeft.php +++ b/src/Operation/ScanLeft.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,27 +28,27 @@ final class ScanLeft extends AbstractOperation * @template V * @template W * - * @return Closure(callable((V|W)=, T=, TKey=, Iterator=): W): Closure(V): Closure(Iterator): Generator + * @return Closure(callable((V|W)=, T=, TKey=, iterable=): W): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param V $initial * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($initial) use ($callback): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( - Reduction::of()($callback)($initial), - Prepend::of()($initial) + (new Reduction())()($callback)($initial), + (new Prepend())()($initial) ); return $pipe; diff --git a/src/Operation/ScanRight.php b/src/Operation/ScanRight.php index f08eeeafd..a2481b265 100644 --- a/src/Operation/ScanRight.php +++ b/src/Operation/ScanRight.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -29,24 +28,24 @@ final class ScanRight extends AbstractOperation * @template V * @template W * - * @return Closure(callable((V|W)=, T=, TKey=, Iterator=): W): Closure(V): Closure(Iterator): Generator + * @return Closure(callable((V|W)=, T=, TKey=, iterable=): W): Closure(V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable((V|W)=, T=, TKey=, Iterator=): W $callback + * @param callable((V|W)=, T=, TKey=, iterable=): W $callback * - * @return Closure(V): Closure(Iterator): Generator + * @return Closure(V): Closure(iterable): Generator */ static fn (callable $callback): Closure => /** * @param V $initial * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function ($initial) use ($callback): Closure { - /** @var Closure(Iterator):(Generator) $pipe */ + /** @var Closure(iterable):(Generator) $pipe */ $pipe = Pipe::of()( Reverse::of(), Reduction::of()($callback)($initial), diff --git a/src/Operation/Shuffle.php b/src/Operation/Shuffle.php index 8cf65a364..53e604c12 100644 --- a/src/Operation/Shuffle.php +++ b/src/Operation/Shuffle.php @@ -10,7 +10,6 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; use loophp\iterators\RandomIterableAggregate; /** @@ -26,20 +25,20 @@ final class Shuffle extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(iterable): iterable */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): iterable */ static fn (int $seed): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return iterable */ - static fn (Iterator $iterator): Iterator => (new RandomIterableAggregate($iterator, $seed))->getIterator(); + static fn (iterable $iterable): iterable => new RandomIterableAggregate($iterable, $seed); } } diff --git a/src/Operation/Since.php b/src/Operation/Since.php index 3a18f24b0..f659d9ac5 100644 --- a/src/Operation/Since.php +++ b/src/Operation/Since.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,33 +26,37 @@ final class Since extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=):bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=):bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=):bool ...$callbacks + * @param callable(T=, TKey=, iterable=):bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { - foreach ($iterator as $key => $current) { - if (!CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + static function (iterable $iterable) use ($callbacks): Generator { + $skip = false; + + foreach ($iterable as $key => $current) { + if (false === $skip) { + if (true === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { + $skip = true; + + yield $key => $current; + } + continue; } - break; - } - - for (; $iterator->valid(); $iterator->next()) { - yield $iterator->key() => $iterator->current(); + yield $key => $current; } }; } diff --git a/src/Operation/Slice.php b/src/Operation/Slice.php index 32b48fb12..edb75f14e 100644 --- a/src/Operation/Slice.php +++ b/src/Operation/Slice.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,27 +23,27 @@ final class Slice extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(int=): Closure(Iterator): Generator + * @return Closure(int): Closure(int=): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(int=): Closure(Iterator): Generator + * @return Closure(int=): Closure(iterable): Generator */ static fn (int $offset): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (int $length = -1) use ($offset): Closure { - /** @var Closure(Iterator): Generator $skip */ + /** @var Closure(iterable): Generator $skip */ $skip = Drop::of()($offset); if (-1 === $length) { return $skip; } - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( $skip, Limit::of()($length)(0) diff --git a/src/Operation/Span.php b/src/Operation/Span.php index e25ee678d..2d1da7409 100644 --- a/src/Operation/Span.php +++ b/src/Operation/Span.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use IteratorAggregate; use loophp\iterators\ClosureIteratorAggregate; @@ -28,12 +27,12 @@ final class Span extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(IteratorAggregate): Generator> + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(IteratorAggregate): Generator> */ public function __invoke(): Closure { /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * * @return Closure(IteratorAggregate): Generator> */ diff --git a/src/Operation/Split.php b/src/Operation/Split.php index 297767c82..b82596677 100644 --- a/src/Operation/Split.php +++ b/src/Operation/Split.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Contract\Operation\Splitable; use loophp\collection\Utils\CallbacksArrayReducer; @@ -28,31 +27,31 @@ final class Split extends AbstractOperation /** * @pure * - * @return Closure(int): Closure((callable(T, TKey): bool)...): Closure(Iterator): Generator> + * @return Closure(int): Closure((callable(T, TKey): bool)...): Closure(iterable): Generator> */ public function __invoke(): Closure { return /** - * @return Closure((callable(T, TKey): bool)...): Closure(Iterator): Generator> + * @return Closure((callable(T, TKey): bool)...): Closure(iterable): Generator> */ static fn (int $type = Splitable::BEFORE): Closure => /** * @param callable(T, TKey): bool ...$callbacks * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator> */ - static function (Iterator $iterator) use ($type, $callbacks): Generator { + static function (iterable $iterable) use ($type, $callbacks): Generator { $carry = []; - foreach ($iterator as $key => $current) { - $callbackReturn = CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator); + foreach ($iterable as $key => $current) { + $callbackReturn = CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable); if (Splitable::AFTER === $type) { $carry[] = $current; diff --git a/src/Operation/Tail.php b/src/Operation/Tail.php index cdb768fda..bb3ef8c95 100644 --- a/src/Operation/Tail.php +++ b/src/Operation/Tail.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Tail extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $drop */ + /** @var Closure(iterable): Generator $drop */ $drop = Drop::of()(1); // Point free style. diff --git a/src/Operation/Tails.php b/src/Operation/Tails.php index a5075e5df..0c61fd548 100644 --- a/src/Operation/Tails.php +++ b/src/Operation/Tails.php @@ -9,7 +9,6 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; use Iterator; @@ -25,24 +24,24 @@ final class Tails extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator, mixed, void> + * @return Closure(iterable): Generator, mixed, void> */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator, mixed, void> */ - static function (Iterator $iterator): Generator { + static function (iterable $iterable): Generator { /** @var Iterator $iterator */ - $iterator = Pack::of()($iterator); + $iterator = Pack::of()($iterable); $data = [...$iterator]; while ([] !== $data) { /** @var Iterator $unpack */ - $unpack = Unpack::of()(new ArrayIterator($data)); + $unpack = Unpack::of()($data); yield [...$unpack]; diff --git a/src/Operation/TakeWhile.php b/src/Operation/TakeWhile.php index 75d5a8783..309b6aef6 100644 --- a/src/Operation/TakeWhile.php +++ b/src/Operation/TakeWhile.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,30 +26,30 @@ final class TakeWhile extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => - /** - * @param Iterator $iterator - * - * @return Generator - */ - static function (Iterator $iterator) use ($callbacks): Generator { - foreach ($iterator as $key => $current) { - if (!CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { - break; - } + /** + * @param iterable $iterable + * + * @return Generator + */ + static function (iterable $iterable) use ($callbacks): Generator { + foreach ($iterable as $key => $current) { + if (!CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { + break; + } - yield $key => $current; - } - }; + yield $key => $current; + } + }; } } diff --git a/src/Operation/Transpose.php b/src/Operation/Transpose.php index b504f1782..6505e449e 100644 --- a/src/Operation/Transpose.php +++ b/src/Operation/Transpose.php @@ -11,8 +11,7 @@ use Closure; use Generator; -use Iterator; -use loophp\iterators\IterableIterator; +use loophp\iterators\IterableIteratorAggregate; use MultipleIterator; /** @@ -28,7 +27,7 @@ final class Transpose extends AbstractOperation * * @psalm-suppress ImpureMethodCall - using MultipleIterator as an internal tool which is not returned * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ public function __invoke(): Closure { @@ -48,11 +47,11 @@ public function __invoke(): Closure */ static fn (array $value): array => $value; - /** @var Closure(Iterator): Generator> $pipe */ + /** @var Closure(iterable): Generator> $pipe */ $pipe = Pipe::of()( Reduce::of()( static function (MultipleIterator $acc, iterable $iterable): MultipleIterator { - $acc->attachIterator(new IterableIterator($iterable)); + $acc->attachIterator((new IterableIteratorAggregate($iterable))->getIterator()); return $acc; } diff --git a/src/Operation/Truthy.php b/src/Operation/Truthy.php index 1c277b941..e049cc097 100644 --- a/src/Operation/Truthy.php +++ b/src/Operation/Truthy.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Truthy extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -35,7 +34,7 @@ public function __invoke(): Closure */ static fn ($value): bool => !(bool) $value; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( MatchOne::of()($matchWhenNot)($matcher), Map::of()($matcher), diff --git a/src/Operation/Unlines.php b/src/Operation/Unlines.php index 4c10f7d67..9239ca11b 100644 --- a/src/Operation/Unlines.php +++ b/src/Operation/Unlines.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use const PHP_EOL; @@ -26,11 +25,11 @@ final class Unlines extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator):Generator $implode */ + /** @var Closure(iterable):Generator $implode */ $implode = Implode::of()(PHP_EOL); // Point free style. diff --git a/src/Operation/Unpack.php b/src/Operation/Unpack.php index 376c3bba2..b4555e00c 100644 --- a/src/Operation/Unpack.php +++ b/src/Operation/Unpack.php @@ -14,7 +14,6 @@ use Iterator; use loophp\iterators\IterableIteratorAggregate; use loophp\iterators\UnpackIterableAggregate; -use Traversable; // phpcs:disable Generic.Files.LineLength.TooLong @@ -32,16 +31,17 @@ final class Unpack extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( - Map::of()(static fn (iterable $iterable): Iterator => (new IterableIteratorAggregate($iterable))->getIterator()), + Map::of()(static fn (iterable $iterable): iterable => new IterableIteratorAggregate($iterable)), Map::of()(Chunk::of()(2)), Flatten::of()(1), - static fn (Iterator $iterator): Traversable => (new UnpackIterableAggregate($iterator))->getIterator() + // TODO: See why we cannot return an iterator aggregate directly. + static fn (iterable $iterable): Iterator => (new UnpackIterableAggregate($iterable))->getIterator() ); // Point free style. diff --git a/src/Operation/Unpair.php b/src/Operation/Unpair.php index b0f71ed56..924e4fc6b 100644 --- a/src/Operation/Unpair.php +++ b/src/Operation/Unpair.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,18 +23,18 @@ final class Unpair extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator): Generator { - foreach ($iterator as $key => $value) { + static function (iterable $iterable): Generator { + foreach ($iterable as $key => $value) { yield $key; yield $value; diff --git a/src/Operation/Until.php b/src/Operation/Until.php index c0592cec7..e6dc6f1f6 100644 --- a/src/Operation/Until.php +++ b/src/Operation/Until.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; /** @@ -27,27 +26,27 @@ final class Until extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(Iterator): Generator + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable ...$callbacks): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($callbacks): Generator { - foreach ($iterator as $key => $current) { + static function (iterable $iterable) use ($callbacks): Generator { + foreach ($iterable as $key => $current) { yield $key => $current; - if (CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + if (CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { break; } } diff --git a/src/Operation/Unwindow.php b/src/Operation/Unwindow.php index 129fc9687..ef3726158 100644 --- a/src/Operation/Unwindow.php +++ b/src/Operation/Unwindow.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Unwindow extends AbstractOperation /** * @pure * - * @return Closure(Iterator>): Generator + * @return Closure(iterable>): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator>): Generator $unwindow */ + /** @var Closure(iterable>): Generator $unwindow */ $unwindow = Map::of()( /** * @param iterable> $iterable diff --git a/src/Operation/Unwords.php b/src/Operation/Unwords.php index 41a056960..de613f312 100644 --- a/src/Operation/Unwords.php +++ b/src/Operation/Unwords.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Unwords extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $implode */ + /** @var Closure(iterable): Generator $implode */ $implode = Implode::of()(' '); // Point free style. diff --git a/src/Operation/Unwrap.php b/src/Operation/Unwrap.php index 354c161ef..19b772434 100644 --- a/src/Operation/Unwrap.php +++ b/src/Operation/Unwrap.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,11 +23,11 @@ final class Unwrap extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - /** @var Closure(Iterator): Generator $flatten */ + /** @var Closure(iterable): Generator $flatten */ $flatten = Flatten::of()(1); // Point free style. diff --git a/src/Operation/Unzip.php b/src/Operation/Unzip.php index 1bdfa9fd2..43f448131 100644 --- a/src/Operation/Unzip.php +++ b/src/Operation/Unzip.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Unzip extends AbstractOperation /** * @pure * - * @return Closure(Iterator>): Generator> + * @return Closure(iterable>): Generator> */ public function __invoke(): Closure { @@ -45,7 +44,7 @@ static function (array $carry, iterable $value): array { return $carry; }; - /** @var Closure(Iterator>): Generator> $pipe */ + /** @var Closure(iterable>): Generator> $pipe */ $pipe = Pipe::of()( Reduce::of()($reduceCallback)([]), Flatten::of()(1) diff --git a/src/Operation/When.php b/src/Operation/When.php index ed585d270..3b047e649 100644 --- a/src/Operation/When.php +++ b/src/Operation/When.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,34 +25,34 @@ final class When extends AbstractOperation /** * @pure * - * @return Closure(callable(Iterator): bool): Closure(callable(Iterator): iterable): Closure(callable(Iterator): iterable): Closure(Iterator): Generator + * @return Closure(callable(iterable): bool): Closure(callable(iterable): iterable): Closure(callable(iterable): iterable): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(Iterator): bool $predicate + * @param callable(iterable): bool $predicate * - * @return Closure(callable(Iterator): iterable): Closure(callable(Iterator): iterable): Closure(Iterator): Generator + * @return Closure(callable(iterable): iterable): Closure(callable(iterable): iterable): Closure(iterable): Generator */ static fn (callable $predicate): Closure => /** - * @param callable(Iterator): iterable $whenTrue + * @param callable(iterable): iterable $whenTrue * - * @return Closure(callable(Iterator): iterable): Closure(Iterator): Generator + * @return Closure(callable(iterable): iterable): Closure(iterable): Generator */ static fn (callable $whenTrue): Closure => /** - * @param callable(Iterator): iterable $whenFalse + * @param callable(iterable): iterable $whenFalse * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable $whenFalse): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static fn (Iterator $iterator): Generator => yield from (true === $predicate($iterator)) ? $whenTrue($iterator) : $whenFalse($iterator); + static fn (iterable $iterable): Generator => yield from (true === $predicate($iterable)) ? $whenTrue($iterable) : $whenFalse($iterable); } } diff --git a/src/Operation/Window.php b/src/Operation/Window.php index 335c3513b..72c13712a 100644 --- a/src/Operation/Window.php +++ b/src/Operation/Window.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use function array_slice; @@ -26,17 +25,17 @@ final class Window extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(Iterator): Generator> + * @return Closure(int): Closure(iterable): Generator> */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ static function (int $size): Closure { - /** @var Closure(Iterator): Generator> $reduction */ - $reduction = Reduction::of()( + /** @var Closure(iterable): Generator> $reduction */ + $reduction = (new Reduction())()( /** * @param list $stack * @param T $current diff --git a/src/Operation/Words.php b/src/Operation/Words.php index 1b35d3608..c13fd016a 100644 --- a/src/Operation/Words.php +++ b/src/Operation/Words.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Words extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { @@ -34,7 +33,7 @@ public function __invoke(): Closure */ static fn (array $value): string => implode('', $value); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Explode::of()("\t", "\n", ' '), Map::of()($mapCallback), diff --git a/src/Operation/Wrap.php b/src/Operation/Wrap.php index cf1b0e6b4..e4bcab516 100644 --- a/src/Operation/Wrap.php +++ b/src/Operation/Wrap.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -24,7 +23,7 @@ final class Wrap extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Generator> + * @return Closure(iterable): Generator> */ public function __invoke(): Closure { @@ -37,7 +36,7 @@ public function __invoke(): Closure */ static fn ($value, $key): array => [$key => $value]; - /** @var Closure(Iterator): Generator> $pipe */ + /** @var Closure(iterable): Generator> $pipe */ $pipe = Pipe::of()( Map::of()($mapCallback), (new Normalize())() diff --git a/src/Operation/Zip.php b/src/Operation/Zip.php index f84e62107..e98113b1f 100644 --- a/src/Operation/Zip.php +++ b/src/Operation/Zip.php @@ -9,11 +9,11 @@ namespace loophp\collection\Operation; -use ArrayIterator; use Closure; use Generator; use Iterator; use loophp\iterators\IterableIterator; +use loophp\iterators\IterableIteratorAggregate; use MultipleIterator; /** @@ -48,27 +48,27 @@ static function (iterable ...$iterables): Closure { * @param list> $iterables */ static fn (array $iterables): Closure => - /** - * @param Iterator $iterator - * - * @return ArrayIterator|IterableIterator)> - */ - static fn (Iterator $iterator): Iterator => new ArrayIterator([ - $iterator, - ...array_map( - /** - * @param iterable $iterable - * - * @return IterableIterator - */ - static fn (iterable $iterable): IterableIterator => new IterableIterator($iterable), - $iterables - ), - ]); + /** + * @param Iterator $iterable + * + * @return array|Iterator)> + */ + static fn (iterable $iterable): array => [ + $iterable, + ...array_map( + /** + * @param iterable $iterable + * + * @return Iterator + */ + static fn (iterable $iterable): Iterator => (new IterableIteratorAggregate($iterable))->getIterator(), + $iterables + ), + ]; $buildMultipleIterator = /** - * @return Closure(ArrayIterator|IterableIterator)>): MultipleIterator + * @return Closure(array|IterableIterator)>): MultipleIterator */ (new Reduce())()( /** @@ -81,7 +81,7 @@ static function (MultipleIterator $acc, Iterator $iterator): MultipleIterator { } )(new MultipleIterator(MultipleIterator::MIT_NEED_ANY)); - /** @var Closure(Iterator): Generator, list> $pipe */ + /** @var Closure(iterable): Generator, list> $pipe */ $pipe = Pipe::of()( $buildArrayIterator($iterables), $buildMultipleIterator, diff --git a/src/Utils/CallbacksArrayReducer.php b/src/Utils/CallbacksArrayReducer.php index 69bb7635b..15dc02c26 100644 --- a/src/Utils/CallbacksArrayReducer.php +++ b/src/Utils/CallbacksArrayReducer.php @@ -10,7 +10,6 @@ namespace loophp\collection\Utils; use Closure; -use Iterator; /** * @immutable @@ -25,24 +24,24 @@ final class CallbacksArrayReducer /** * @pure * - * @return Closure(array=): bool>, T, TKey, Iterator): bool + * @return Closure(array=): bool>, T, TKey, iterable): bool */ public static function or(): Closure { return /** - * @param array=): bool> $callbacks + * @param array=): bool> $callbacks * @param T $current * @param TKey $key - * @param Iterator $iterator + * @param iterable $iterable */ - static fn (array $callbacks, $current, $key, Iterator $iterator): bool => array_reduce( + static fn (array $callbacks, $current, $key, iterable $iterable): bool => array_reduce( $callbacks, /** * @param bool $carry - * @param callable(T=, TKey=, Iterator=): bool $callable + * @param callable(T=, TKey=, iterable=): bool $callable */ - static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator), + static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterable), false ); }