From d775074f598700f4cc46f2482280ee784d91eddd Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 30 Jan 2022 16:16:07 +0100 Subject: [PATCH] Replace `Iterator` with `iterable`. --- composer.json | 2 +- src/Collection.php | 305 ++++++++---------- src/Contract/Collection.php | 6 +- src/Contract/Operation/Applyable.php | 3 +- src/Contract/Operation/Associateable.php | 10 +- src/Contract/Operation/Duplicateable.php | 6 +- src/Contract/Operation/Equalsable.php | 4 +- src/Contract/Operation/Everyable.php | 4 +- 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/Sameable.php | 4 +- 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/Operation/All.php | 11 +- src/Operation/Append.php | 2 +- src/Operation/Apply.php | 15 +- src/Operation/Associate.php | 19 +- src/Operation/AsyncMap.php | 9 +- src/Operation/AsyncMapN.php | 9 +- src/Operation/Cache.php | 13 +- 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 | 13 +- 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 | 7 +- src/Operation/DropWhile.php | 15 +- src/Operation/Dump.php | 13 +- src/Operation/Duplicate.php | 16 +- src/Operation/Equals.php | 26 +- src/Operation/Every.php | 33 +- src/Operation/Explode.php | 7 +- 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/FoldLeft1.php | 9 +- src/Operation/FoldRight.php | 9 +- src/Operation/FoldRight1.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 | 7 +- src/Operation/Init.php | 10 +- src/Operation/Inits.php | 5 +- src/Operation/Intersect.php | 5 +- src/Operation/IntersectKeys.php | 5 +- src/Operation/Intersperse.php | 15 +- src/Operation/IsEmpty.php | 8 +- src/Operation/Key.php | 7 +- src/Operation/Keys.php | 5 +- src/Operation/Last.php | 9 +- src/Operation/Limit.php | 16 +- 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 | 2 +- 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 | 22 +- src/Operation/Permutate.php | 8 +- src/Operation/Pipe.php | 21 +- src/Operation/Pluck.php | 30 +- src/Operation/Prepend.php | 9 +- src/Operation/Product.php | 59 ++-- src/Operation/RSample.php | 5 +- src/Operation/Random.php | 9 +- src/Operation/Range.php | 13 +- src/Operation/Reduce.php | 11 +- src/Operation/Reduction.php | 20 +- src/Operation/Reject.php | 11 +- src/Operation/Reverse.php | 5 +- src/Operation/Same.php | 22 +- src/Operation/Scale.php | 15 +- src/Operation/ScanLeft.php | 15 +- src/Operation/ScanLeft1.php | 18 +- src/Operation/ScanRight.php | 11 +- src/Operation/ScanRight1.php | 9 +- src/Operation/Shuffle.php | 12 +- src/Operation/Since.php | 15 +- src/Operation/Slice.php | 11 +- src/Operation/Sort.php | 17 +- src/Operation/Span.php | 22 +- src/Operation/Split.php | 15 +- src/Operation/Strict.php | 16 +- src/Operation/Tail.php | 5 +- src/Operation/Tails.php | 13 +- src/Operation/TakeWhile.php | 33 +- src/Operation/Times.php | 8 +- src/Operation/Transpose.php | 9 +- src/Operation/Truthy.php | 5 +- src/Operation/Unlines.php | 5 +- src/Operation/Unpack.php | 11 +- 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 | 5 +- src/Utils/CallbacksArrayReducer.php | 13 +- .../unit/CollectionSpecificOperationTest.php | 11 +- .../Traits/GenericCollectionProviders.php | 9 +- .../unit/Utils/CallbacksArrayReducerTest.php | 6 +- 144 files changed, 797 insertions(+), 927 deletions(-) diff --git a/composer.json b/composer.json index e04b2ffbb..7e809fd28 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ ], "require": { "php": ">= 7.4", - "loophp/iterators": "^1.5.13" + "loophp/iterators": "^1.5.14" }, "require-dev": { "amphp/parallel-functions": "^1", diff --git a/src/Collection.php b/src/Collection.php index 7eb4c65cf..1587618e9 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -12,7 +12,6 @@ use Closure; use Doctrine\Common\Collections\Criteria; use Generator; -use Iterator; use IteratorAggregate; use loophp\collection\Contract\Collection as CollectionInterface; use loophp\collection\Contract\Operation; @@ -131,14 +130,14 @@ use loophp\collection\Operation\Words; use loophp\collection\Operation\Wrap; use loophp\collection\Operation\Zip; -use loophp\iterators\CachingIteratorAggregate; -use loophp\iterators\ClosureIterator; -use loophp\iterators\IterableIterator; +use loophp\iterators\ClosureIteratorAggregate; +use loophp\iterators\IterableIteratorAggregate; use loophp\iterators\ResourceIteratorAggregate; use loophp\iterators\StringIteratorAggregate; use NoRewindIterator; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\Adapter\ArrayAdapter; +use Traversable; use const INF; use const PHP_INT_MAX; @@ -156,40 +155,32 @@ final class Collection implements CollectionInterface { /** - * @var iterable + * @var IteratorAggregate */ - private iterable $parameters; + private IteratorAggregate $innerIterator; /** - * @var callable(mixed ...$parameters): iterable - */ - private $source; - - /** - * @psalm-external-mutation-free - * * @param callable(mixed ...$parameters): iterable $callable * @param iterable $parameters */ private function __construct(callable $callable, iterable $parameters = []) { - $this->source = $callable; - $this->parameters = $parameters; + $this->innerIterator = new ClosureIteratorAggregate($callable, $parameters); } public function all(bool $normalize = true): array { - return iterator_to_array(All::of()($normalize)($this->getIterator())); + return iterator_to_array(All::of()($normalize)($this)); } public function append(...$items): CollectionInterface { - return new self(Append::of()(...$items), [$this->getIterator()]); + return new self(Append::of()(...$items), [$this]); } public function apply(callable ...$callbacks): CollectionInterface { - return new self(Apply::of()(...$callbacks), [$this->getIterator()]); + return new self(Apply::of()(...$callbacks), [$this]); } public function associate( @@ -204,87 +195,87 @@ public function associate( */ static fn ($carry) => $carry; - return new self(Associate::of()($callbackForKeys ?? $defaultCallback)($callbackForValues ?? $defaultCallback), [$this->getIterator()]); + return new self(Associate::of()($callbackForKeys ?? $defaultCallback)($callbackForValues ?? $defaultCallback), [$this]); } public function asyncMap(callable $callback): CollectionInterface { - return new self(AsyncMap::of()($callback), [$this->getIterator()]); + return new self(AsyncMap::of()($callback), [$this]); } public function asyncMapN(callable ...$callbacks): CollectionInterface { - return new self(AsyncMapN::of()(...$callbacks), [$this->getIterator()]); + return new self(AsyncMapN::of()(...$callbacks), [$this]); } public function cache(?CacheItemPoolInterface $cache = null): CollectionInterface { - return new self(Cache::of()($cache ?? new ArrayAdapter()), [$this->getIterator()]); + return new self(Cache::of()($cache ?? new ArrayAdapter()), [$this]); } public function chunk(int ...$sizes): CollectionInterface { - return new self(Chunk::of()(...$sizes), [$this->getIterator()]); + return new self(Chunk::of()(...$sizes), [$this]); } public function coalesce(): CollectionInterface { - return new self(Coalesce::of(), [$this->getIterator()]); + return new self(Coalesce::of(), [$this]); } public function collapse(): CollectionInterface { - return new self(Collapse::of(), [$this->getIterator()]); + return new self(Collapse::of(), [$this]); } public function column($column): CollectionInterface { - return new self(Column::of()($column), [$this->getIterator()]); + return new self(Column::of()($column), [$this]); } public function combinate(?int $length = null): CollectionInterface { - return new self(Combinate::of()($length), [$this->getIterator()]); + return new self(Combinate::of()($length), [$this]); } public function combine(...$keys): CollectionInterface { - return new self(Combine::of()(...$keys), [$this->getIterator()]); + return new self(Combine::of()(...$keys), [$this]); } public function compact(...$values): CollectionInterface { - return new self(Compact::of()(...$values), [$this->getIterator()]); + return new self(Compact::of()(...$values), [$this]); } public function contains(...$values): bool { - return (new self(Contains::of()(...$values), [$this->getIterator()]))->getIterator()->current(); + return Contains::of()(...$values)($this)->current(); } public function count(): int { - return iterator_count($this->getIterator()); + return iterator_count($this); } public function current(int $index = 0, $default = null) { - return (new self(Current::of()($index)($default), [$this->getIterator()]))->getIterator()->current(); + return Current::of()($index)($default)($this)->current(); } public function cycle(): CollectionInterface { - return new self(Cycle::of(), [$this->getIterator()]); + return new self(Cycle::of(), [$this]); } public function diff(...$values): CollectionInterface { - return new self(Diff::of()(...$values), [$this->getIterator()]); + return new self(Diff::of()(...$values), [$this]); } public function diffKeys(...$keys): CollectionInterface { - return new self(DiffKeys::of()(...$keys), [$this->getIterator()]); + return new self(DiffKeys::of()(...$keys), [$this]); } public function distinct(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface @@ -310,22 +301,22 @@ public function distinct(?callable $comparatorCallback = null, ?callable $access */ static fn ($right): bool => $left === $right; - return new self(Distinct::of()($comparatorCallback)($accessorCallback), [$this->getIterator()]); + return new self(Distinct::of()($comparatorCallback)($accessorCallback), [$this]); } public function drop(int $count): CollectionInterface { - return new self(Drop::of()($count), [$this->getIterator()]); + return new self(Drop::of()($count), [$this]); } public function dropWhile(callable ...$callbacks): CollectionInterface { - return new self(DropWhile::of()(...$callbacks), [$this->getIterator()]); + return new self(DropWhile::of()(...$callbacks), [$this]); } public function dump(string $name = '', int $size = 1, ?Closure $closure = null): CollectionInterface { - return new self(Dump::of()($name)($size)($closure), [$this->getIterator()]); + return new self(Dump::of()($name)($size)($closure), [$this]); } public function duplicate(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface @@ -351,104 +342,98 @@ public function duplicate(?callable $comparatorCallback = null, ?callable $acces */ static fn ($right): bool => $left === $right; - return new self(Duplicate::of()($comparatorCallback)($accessorCallback), [$this->getIterator()]); + return new self(Duplicate::of()($comparatorCallback)($accessorCallback), [$this]); } /** - * @pure - * - * @return self + * @return self */ public static function empty(): CollectionInterface { - /** @var array $emptyArray */ - $emptyArray = []; - - return self::fromIterable($emptyArray); + return new self(static fn (): Generator => yield from []); } - public function equals(CollectionInterface $other): bool + public function equals(iterable $other): bool { - return (new self(Equals::of()($other->getIterator()), [$this->getIterator()]))->getIterator()->current(); + return Equals::of()($other)($this)->current(); } public function every(callable ...$callbacks): bool { - return (new self(Every::of()(static fn (): bool => false)(...$callbacks), [$this->getIterator()]))->getIterator()->current(); + return Every::of()(static fn (): bool => false)(...$callbacks)($this)->current(); } public function explode(...$explodes): CollectionInterface { - return new self(Explode::of()(...$explodes), [$this->getIterator()]); + return new self(Explode::of()(...$explodes), [$this]); } public function falsy(): bool { - return (new self(Falsy::of(), [$this->getIterator()]))->getIterator()->current(); + return Falsy::of()($this)->current(); } public function filter(callable ...$callbacks): CollectionInterface { - return new self(Filter::of()(...$callbacks), [$this->getIterator()]); + return new self(Filter::of()(...$callbacks), [$this]); } public function find($default = null, callable ...$callbacks) { - return (new self(Find::of()($default)(...$callbacks), [$this->getIterator()]))->getIterator()->current(); + return Find::of()($default)(...$callbacks)($this)->current(); } public function first(): CollectionInterface { - return new self(First::of(), [$this->getIterator()]); + return new self(First::of(), [$this]); } public function flatMap(callable $callback): CollectionInterface { - return new self(FlatMap::of()($callback), [$this->getIterator()]); + return new self(FlatMap::of()($callback), [$this]); } public function flatten(int $depth = PHP_INT_MAX): CollectionInterface { - return new self(Flatten::of()($depth), [$this->getIterator()]); + return new self(Flatten::of()($depth), [$this]); } public function flip(): CollectionInterface { - return new self(Flip::of(), [$this->getIterator()]); + return new self(Flip::of(), [$this]); } public function foldLeft(callable $callback, $initial = null): CollectionInterface { - return new self(FoldLeft::of()($callback)($initial), [$this->getIterator()]); + return new self(FoldLeft::of()($callback)($initial), [$this]); } public function foldLeft1(callable $callback): CollectionInterface { - return new self(FoldLeft1::of()($callback), [$this->getIterator()]); + return new self(FoldLeft1::of()($callback), [$this]); } public function foldRight(callable $callback, $initial = null): CollectionInterface { - return new self(Foldright::of()($callback)($initial), [$this->getIterator()]); + return new self(Foldright::of()($callback)($initial), [$this]); } public function foldRight1(callable $callback): CollectionInterface { - return new self(FoldRight1::of()($callback), [$this->getIterator()]); + return new self(FoldRight1::of()($callback), [$this]); } public function forget(...$keys): CollectionInterface { - return new self(Forget::of()(...$keys), [$this->getIterator()]); + return new self(Forget::of()(...$keys), [$this]); } public function frequency(): CollectionInterface { - return new self(Frequency::of(), [$this->getIterator()]); + return new self(Frequency::of(), [$this]); } /** - * @pure * @template NewTKey * @template NewT * @@ -463,14 +448,12 @@ public static function fromCallable(callable $callable, iterable $parameters = [ } /** - * @pure - * * @return self */ public static function fromFile(string $filepath): self { return new self( - static fn (): IteratorAggregate => new ResourceIteratorAggregate(fopen($filepath, 'rb'), true), + static fn (): Generator => yield from new ResourceIteratorAggregate(fopen($filepath, 'rb'), true), ); } @@ -484,16 +467,10 @@ public static function fromFile(string $filepath): self */ public static function fromGenerator(Generator $generator): self { - return self::fromIterable( - new CachingIteratorAggregate( - new NoRewindIterator($generator) - ) - ); + return new self(static fn (): Generator => yield from new NoRewindIterator($generator)); } /** - * @pure - * * @template NewTKey * @template NewT * @@ -503,62 +480,58 @@ 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 (): Generator => yield from new IterableIteratorAggregate($iterable)); } /** - * @pure - * * @param resource $resource * * @return self */ public static function fromResource($resource): self { - return new self(static fn (): IteratorAggregate => new ResourceIteratorAggregate($resource)); + return new self(static fn (): Generator => yield from new ResourceIteratorAggregate($resource)); } /** - * @pure - * * @return self */ public static function fromString(string $string, string $delimiter = ''): self { - return new self(static fn (): IteratorAggregate => new StringIteratorAggregate($string, $delimiter)); + return new self(static fn (): Generator => yield from new StringIteratorAggregate($string, $delimiter)); } public function get($key, $default = null): CollectionInterface { - return new self(Get::of()($key)($default), [$this->getIterator()]); + return new self(Get::of()($key)($default), [$this]); } /** - * @return Iterator + * @return Traversable */ - public function getIterator(): Iterator + public function getIterator(): Traversable { - return new ClosureIterator($this->source, $this->parameters); + yield from $this->innerIterator->getIterator(); } public function group(): CollectionInterface { - return new self(Group::of(), [$this->getIterator()]); + return new self(Group::of(), [$this]); } public function groupBy(callable $callable): CollectionInterface { - return new self(GroupBy::of()($callable), [$this->getIterator()]); + return new self(GroupBy::of()($callable), [$this]); } public function has(callable ...$callbacks): bool { - return (new self(Has::of()(...$callbacks), [$this->getIterator()]))->getIterator()->current(); + return Has::of()(...$callbacks)($this)->current(); } public function head(): CollectionInterface { - return new self(Head::of(), [$this->getIterator()]); + return new self(Head::of(), [$this]); } public function ifThenElse(callable $condition, callable $then, ?callable $else = null): CollectionInterface @@ -571,42 +544,42 @@ public function ifThenElse(callable $condition, callable $then, ?callable $else */ static fn ($value) => $value; - return new self(IfThenElse::of()($condition)($then)($else ?? $identity), [$this->getIterator()]); + return new self(IfThenElse::of()($condition)($then)($else ?? $identity), [$this]); } public function implode(string $glue = ''): CollectionInterface { - return new self(Implode::of()($glue), [$this->getIterator()]); + return new self(Implode::of()($glue), [$this]); } public function init(): CollectionInterface { - return new self(Init::of(), [$this->getIterator()]); + return new self(Init::of(), [$this]); } public function inits(): CollectionInterface { - return new self(Inits::of(), [$this->getIterator()]); + return new self(Inits::of(), [$this]); } public function intersect(...$values): CollectionInterface { - return new self(Intersect::of()(...$values), [$this->getIterator()]); + return new self(Intersect::of()(...$values), [$this]); } public function intersectKeys(...$keys): CollectionInterface { - return new self(IntersectKeys::of()(...$keys), [$this->getIterator()]); + return new self(IntersectKeys::of()(...$keys), [$this]); } public function intersperse($element, int $every = 1, int $startAt = 0): CollectionInterface { - return new self(Intersperse::of()($element)($every)($startAt), [$this->getIterator()]); + return new self(Intersperse::of()($element)($every)($startAt), [$this]); } public function isEmpty(): bool { - return (new self(IsEmpty::of(), [$this->getIterator()]))->getIterator()->current(); + return IsEmpty::of()($this)->current(); } /** @@ -619,84 +592,82 @@ public function jsonSerialize(): array public function key(int $index = 0) { - return (new self(Key::of()($index), [$this->getIterator()]))->getIterator()->current(); + return Key::of()($index)($this)->current(); } public function keys(): CollectionInterface { - return new self(Keys::of(), [$this->getIterator()]); + return new self(Keys::of(), [$this]); } public function last(): CollectionInterface { - return new self(Last::of(), [$this->getIterator()]); + return new self(Last::of(), [$this]); } public function limit(int $count = -1, int $offset = 0): CollectionInterface { - return new self(Limit::of()($count)($offset), [$this->getIterator()]); + return new self(Limit::of()($count)($offset), [$this]); } public function lines(): CollectionInterface { - return new self(Lines::of(), [$this->getIterator()]); + return new self(Lines::of(), [$this]); } public function map(callable $callback): CollectionInterface { - return new self(Map::of()($callback), [$this->getIterator()]); + return new self(Map::of()($callback), [$this]); } public function mapN(callable ...$callbacks): CollectionInterface { - return new self(MapN::of()(...$callbacks), [$this->getIterator()]); + return new self(MapN::of()(...$callbacks), [$this]); } public function match(callable $callback, ?callable $matcher = null): bool { - return (new self(MatchOne::of()($matcher ?? static fn (): bool => true)($callback), [$this->getIterator()])) - ->getIterator() - ->current(); + return MatchOne::of()($matcher ?? static fn (): bool => true)($callback)($this)->current(); } public function matching(Criteria $criteria): CollectionInterface { - return new self(Matching::of()($criteria), [$this->getIterator()]); + return new self(Matching::of()($criteria), [$this]); } public function merge(iterable ...$sources): CollectionInterface { - return new self(Merge::of()(...$sources), [$this->getIterator()]); + return new self(Merge::of()(...$sources), [$this]); } public function normalize(): CollectionInterface { - return new self(Normalize::of(), [$this->getIterator()]); + return new self(Normalize::of(), [$this]); } public function nth(int $step, int $offset = 0): CollectionInterface { - return new self(Nth::of()($step)($offset), [$this->getIterator()]); + return new self(Nth::of()($step)($offset), [$this]); } public function nullsy(): bool { - return (new self(Nullsy::of(), [$this->getIterator()]))->getIterator()->current(); + return Nullsy::of()($this)->current(); } public function pack(): CollectionInterface { - return new self(Pack::of(), [$this->getIterator()]); + return new self(Pack::of(), [$this]); } public function pad(int $size, $value): CollectionInterface { - return new self(Pad::of()($size)($value), [$this->getIterator()]); + return new self(Pad::of()($size)($value), [$this]); } public function pair(): CollectionInterface { - return new self(Pair::of(), [$this->getIterator()]); + return new self(Pair::of(), [$this]); } public function partition(callable ...$callbacks): CollectionInterface @@ -704,75 +675,75 @@ public function partition(callable ...$callbacks): CollectionInterface return (new self((new Partition())()(...$callbacks), [$this])) ->map( /** - * @param IteratorAggregate $iteratorAggregate + * @param iterable $iterable * * @return CollectionInterface */ - static fn (IteratorAggregate $iteratorAggregate): CollectionInterface => Collection::fromIterable($iteratorAggregate) + static fn (iterable $iterable): CollectionInterface => Collection::fromIterable($iterable) ); } public function permutate(): CollectionInterface { - return new self(Permutate::of(), [$this->getIterator()]); + return new self(Permutate::of(), [$this]); } public function pipe(callable ...$callbacks): CollectionInterface { - return new self(Pipe::of()(...$callbacks), [$this->getIterator()]); + return new self(Pipe::of()(...$callbacks), [$this]); } public function pluck($pluck, $default = null): CollectionInterface { - return new self(Pluck::of()($pluck)($default), [$this->getIterator()]); + return new self(Pluck::of()($pluck)($default), [$this]); } public function prepend(...$items): CollectionInterface { - return new self(Prepend::of()(...$items), [$this->getIterator()]); + return new self(Prepend::of()(...$items), [$this]); } public function product(iterable ...$iterables): CollectionInterface { - return new self(Product::of()(...$iterables), [$this->getIterator()]); + return new self(Product::of()(...$iterables), [$this]); } public function random(int $size = 1, ?int $seed = null): CollectionInterface { - return new self(Random::of()($seed ?? random_int(0, 1000))($size), [$this->getIterator()]); + return new self(Random::of()($seed ?? random_int(0, 1000))($size), [$this]); } public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): CollectionInterface { - return self::empty()->pipe(Range::of()($start)($end)($step)); + return new self(Range::of()($start)($end)($step)); } public function reduce(callable $callback, $initial = null): CollectionInterface { - return new self(Reduce::of()($callback)($initial), [$this->getIterator()]); + return new self(Reduce::of()($callback)($initial), [$this]); } public function reduction(callable $callback, $initial = null): CollectionInterface { - return new self(Reduction::of()($callback)($initial), [$this->getIterator()]); + return new self(Reduction::of()($callback)($initial), [$this]); } public function reject(callable ...$callbacks): CollectionInterface { - return new self(Reject::of()(...$callbacks), [$this->getIterator()]); + return new self(Reject::of()(...$callbacks), [$this]); } public function reverse(): CollectionInterface { - return new self(Reverse::of(), [$this->getIterator()]); + return new self(Reverse::of(), [$this]); } public function rsample(float $probability): CollectionInterface { - return new self(RSample::of()($probability), [$this->getIterator()]); + return new self(RSample::of()($probability), [$this]); } - public function same(CollectionInterface $other, ?callable $comparatorCallback = null): bool + public function same(iterable $other, ?callable $comparatorCallback = null): bool { $comparatorCallback ??= /** @@ -788,7 +759,7 @@ public function same(CollectionInterface $other, ?callable $comparatorCallback = */ static fn ($rightValue, $rightKey): bool => $leftValue === $rightValue && $leftKey === $rightKey; - return (new self(Same::of()($other->getIterator())($comparatorCallback), [$this->getIterator()]))->getIterator()->current(); + return Same::of()($other)($comparatorCallback)($this)->current(); } public function scale( @@ -798,47 +769,47 @@ public function scale( float $wantedUpperBound = 1.0, float $base = 0.0 ): CollectionInterface { - return new self(Scale::of()($lowerBound)($upperBound)($wantedLowerBound)($wantedUpperBound)($base), [$this->getIterator()]); + return new self(Scale::of()($lowerBound)($upperBound)($wantedLowerBound)($wantedUpperBound)($base), [$this]); } public function scanLeft(callable $callback, $initial = null): CollectionInterface { - return new self(ScanLeft::of()($callback)($initial), [$this->getIterator()]); + return new self(ScanLeft::of()($callback)($initial), [$this]); } public function scanLeft1(callable $callback): CollectionInterface { - return new self(ScanLeft1::of()($callback), [$this->getIterator()]); + return new self(ScanLeft1::of()($callback), [$this]); } public function scanRight(callable $callback, $initial = null): CollectionInterface { - return new self(ScanRight::of()($callback)($initial), [$this->getIterator()]); + return new self(ScanRight::of()($callback)($initial), [$this]); } public function scanRight1(callable $callback): CollectionInterface { - return new self(ScanRight1::of()($callback), [$this->getIterator()]); + return new self(ScanRight1::of()($callback), [$this]); } public function shuffle(?int $seed = null): CollectionInterface { - return new self(Shuffle::of()($seed ?? random_int(0, 1000)), [$this->getIterator()]); + return new self(Shuffle::of()($seed ?? random_int(0, 1000)), [$this]); } public function since(callable ...$callbacks): CollectionInterface { - return new self(Since::of()(...$callbacks), [$this->getIterator()]); + return new self(Since::of()(...$callbacks), [$this]); } public function slice(int $offset, int $length = -1): CollectionInterface { - return new self(Slice::of()($offset)($length), [$this->getIterator()]); + return new self(Slice::of()($offset)($length), [$this]); } public function sort(int $type = Operation\Sortable::BY_VALUES, ?callable $callback = null): CollectionInterface { - return new self(Sort::of()($type)($callback), [$this->getIterator()]); + return new self(Sort::of()($type)($callback), [$this]); } public function span(callable ...$callbacks): CollectionInterface @@ -846,17 +817,17 @@ public function span(callable ...$callbacks): CollectionInterface return (new self((new Span())()(...$callbacks), [$this])) ->map( /** - * @param IteratorAggregate $iteratorAggregate + * @param iterable $iterable * * @return CollectionInterface */ - static fn (IteratorAggregate $iteratorAggregate): CollectionInterface => Collection::fromIterable($iteratorAggregate) + static fn (iterable $iterable): CollectionInterface => Collection::fromIterable($iterable) ); } public function split(int $type = Operation\Splitable::BEFORE, callable ...$callbacks): CollectionInterface { - return new self(Split::of()($type)(...$callbacks), [$this->getIterator()]); + return new self(Split::of()($type)(...$callbacks), [$this]); } public function squash(): CollectionInterface @@ -866,114 +837,114 @@ public function squash(): CollectionInterface public function strict(?callable $callback = null): CollectionInterface { - return new self(Strict::of()($callback), [$this->getIterator()]); + return new self(Strict::of()($callback), [$this]); } public function tail(): CollectionInterface { - return new self(Tail::of(), [$this->getIterator()]); + return new self(Tail::of(), [$this]); } public function tails(): CollectionInterface { - return new self(Tails::of(), [$this->getIterator()]); + return new self(Tails::of(), [$this]); } public function takeWhile(callable ...$callbacks): CollectionInterface { - return new self(TakeWhile::of()(...$callbacks), [$this->getIterator()]); + return new self(TakeWhile::of()(...$callbacks), [$this]); } public static function times(int $number = 0, ?callable $callback = null): CollectionInterface { - return self::empty()->pipe(Times::of()($number)($callback)); + return new self(Times::of()($number)($callback)); } public function transpose(): CollectionInterface { - return new self(Transpose::of(), [$this->getIterator()]); + return new self(Transpose::of(), [$this]); } public function truthy(): bool { - return (new self(Truthy::of(), [$this->getIterator()]))->getIterator()->current(); + return Truthy::of()($this)->current(); } public static function unfold(callable $callback, ...$parameters): CollectionInterface { - return self::empty()->pipe(Unfold::of()(...$parameters)($callback)); + return new self(Unfold::of()(...$parameters)($callback)); } public function unlines(): CollectionInterface { - return new self(Unlines::of(), [$this->getIterator()]); + return new self(Unlines::of(), [$this]); } public function unpack(): CollectionInterface { - return new self(Unpack::of(), [$this->getIterator()]); + return new self(Unpack::of(), [$this]); } public function unpair(): CollectionInterface { - return new self(Unpair::of(), [$this->getIterator()]); + return new self(Unpair::of(), [$this]); } public function until(callable ...$callbacks): CollectionInterface { - return new self(Until::of()(...$callbacks), [$this->getIterator()]); + return new self(Until::of()(...$callbacks), [$this]); } public function unwindow(): CollectionInterface { - return new self(Unwindow::of(), [$this->getIterator()]); + return new self(Unwindow::of(), [$this]); } public function unwords(): CollectionInterface { - return new self(Unwords::of(), [$this->getIterator()]); + return new self(Unwords::of(), [$this]); } public function unwrap(): CollectionInterface { - return new self(Unwrap::of(), [$this->getIterator()]); + return new self(Unwrap::of(), [$this]); } public function unzip(): CollectionInterface { - return new self(Unzip::of(), [$this->getIterator()]); + return new self(Unzip::of(), [$this]); } public function when(callable $predicate, callable $whenTrue, ?callable $whenFalse = null): CollectionInterface { $whenFalse ??= /** - * @param Iterator $collection + * @param iterable $iterable * * @return iterable */ - static fn (Iterator $collection): iterable => $collection; + static fn (iterable $iterable): iterable => $iterable; - return new self(When::of()($predicate)($whenTrue)($whenFalse), [$this->getIterator()]); + return new self(When::of()($predicate)($whenTrue)($whenFalse), [$this]); } public function window(int $size): CollectionInterface { - return new self(Window::of()($size), [$this->getIterator()]); + return new self(Window::of()($size), [$this]); } public function words(): CollectionInterface { - return new self(Words::of(), [$this->getIterator()]); + return new self(Words::of(), [$this]); } public function wrap(): CollectionInterface { - return new self(Wrap::of(), [$this->getIterator()]); + return new self(Wrap::of(), [$this]); } public function zip(iterable ...$iterables): CollectionInterface { - return new self(Zip::of()(...$iterables), [$this->getIterator()]); + return new self(Zip::of()(...$iterables), [$this]); } } diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index 1126cbbb1..dcf732dbc 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -10,7 +10,6 @@ namespace loophp\collection\Contract; use Countable; -use Iterator; use IteratorAggregate; use JsonSerializable; use loophp\collection\Contract\Operation\Allable; @@ -129,6 +128,7 @@ use loophp\collection\Contract\Operation\Wordsable; use loophp\collection\Contract\Operation\Wrapable; use loophp\collection\Contract\Operation\Zipable; +use Traversable; /** * @immutable @@ -374,7 +374,7 @@ interface Collection extends Zipable { /** - * @return Iterator + * @return Traversable */ - public function getIterator(): Iterator; + public function getIterator(): Traversable; } 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/Equalsable.php b/src/Contract/Operation/Equalsable.php index 4f90f70f8..b0002ecc5 100644 --- a/src/Contract/Operation/Equalsable.php +++ b/src/Contract/Operation/Equalsable.php @@ -27,7 +27,7 @@ interface Equalsable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#equals * - * @param Collection $other + * @param iterable $other */ - public function equals(Collection $other): bool; + public function equals(iterable $other): bool; } diff --git a/src/Contract/Operation/Everyable.php b/src/Contract/Operation/Everyable.php index 56e8aa413..20b2c2041 100644 --- a/src/Contract/Operation/Everyable.php +++ b/src/Contract/Operation/Everyable.php @@ -9,8 +9,6 @@ namespace loophp\collection\Contract\Operation; -use Iterator; - /** * @template TKey * @template T @@ -22,7 +20,7 @@ interface Everyable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#every * - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks */ public function every(callable ...$callbacks): bool; } 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/Sameable.php b/src/Contract/Operation/Sameable.php index cd9ee9d4d..cc22b6ec6 100644 --- a/src/Contract/Operation/Sameable.php +++ b/src/Contract/Operation/Sameable.php @@ -32,8 +32,8 @@ interface Sameable * * @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#same * - * @param Collection $other + * @param iterable $other * @param null|callable(T, TKey): (Closure(T, TKey): bool) $comparatorCallback */ - public function same(Collection $other, ?callable $comparatorCallback = null): bool; + public function same(iterable $other, ?callable $comparatorCallback = null): bool; } 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/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 ca6e64d83..9f195849d 100644 --- a/src/Operation/Append.php +++ b/src/Operation/Append.php @@ -38,7 +38,7 @@ public function __invoke(): Closure */ static fn (...$items): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ 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/AsyncMap.php b/src/Operation/AsyncMap.php index 444e964e7..2d9cf3371 100644 --- a/src/Operation/AsyncMap.php +++ b/src/Operation/AsyncMap.php @@ -13,7 +13,6 @@ use Closure; use Exception; use Generator; -use Iterator; use function Amp\Iterator\fromIterable; use function Amp\ParallelFunctions\parallel; @@ -41,7 +40,7 @@ final class AsyncMap extends AbstractOperation * * @template V * - * @return Closure(callable(T, TKey): V): Closure(Iterator): Generator + * @return Closure(callable(T, TKey): V): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -51,11 +50,9 @@ public function __invoke(): Closure */ static fn (callable $callback): Closure => /** - * @param Iterator $iterator - * * @return Generator */ - static function (Iterator $iterator) use ($callback): Generator { + static function (iterable $iterable) use ($callback): Generator { $parallelCallBack = /** * @param array{0: TKey, 1: T} $value @@ -64,7 +61,7 @@ static function (Iterator $iterator) use ($callback): Generator { */ static fn (array $value): array => [$value[0], $callback($value[1], $value[0])]; - $iter = map(fromIterable(Pack::of()($iterator)), new LocalSemaphore(32), parallel($parallelCallBack)); + $iter = map(fromIterable(Pack::of()($iterable)), new LocalSemaphore(32), parallel($parallelCallBack)); while (wait($iter->advance())) { /** @var array{0: TKey, 1: V} $item */ diff --git a/src/Operation/AsyncMapN.php b/src/Operation/AsyncMapN.php index 5e126da97..da50765d7 100644 --- a/src/Operation/AsyncMapN.php +++ b/src/Operation/AsyncMapN.php @@ -13,7 +13,6 @@ use Closure; use Exception; use Generator; -use Iterator; use function Amp\Iterator\fromIterable; use function Amp\ParallelFunctions\parallel; @@ -39,7 +38,7 @@ final class AsyncMapN extends AbstractOperation /** * @pure * - * @return Closure(callable(mixed, mixed): mixed ...): Closure(Iterator): Generator + * @return Closure(callable(mixed, mixed): mixed ...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -49,11 +48,11 @@ public function __invoke(): Closure */ 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 @@ -77,7 +76,7 @@ static function (Iterator $iterator) use ($callbacks): Generator { */ static fn (array $value): array => [$value[0], array_reduce($callbacks, $callbackFactory($value[0]), $value[1])]; - $iter = map(fromIterable(Pack::of()($iterator)), new LocalSemaphore(32), parallel($callback)); + $iter = map(fromIterable(Pack::of()($iterable)), new LocalSemaphore(32), parallel($callback)); while (wait($iter->advance())) { /** @var array{0: mixed, 1: mixed} $item */ diff --git a/src/Operation/Cache.php b/src/Operation/Cache.php index 3f116128f..c281756db 100644 --- a/src/Operation/Cache.php +++ b/src/Operation/Cache.php @@ -10,8 +10,9 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; +use Generator; use loophp\collection\Iterator\PsrCacheIterator; +use loophp\iterators\IterableIteratorAggregate; use Psr\Cache\CacheItemPoolInterface; /** @@ -27,20 +28,20 @@ final class Cache extends AbstractOperation /** * @pure * - * @return Closure(CacheItemPoolInterface): Closure(Iterator): Iterator + * @return Closure(CacheItemPoolInterface): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): Generator */ static fn (CacheItemPoolInterface $cache): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return Generator */ - static fn (Iterator $iterator): Iterator => new PsrCacheIterator($iterator, $cache); + static fn (iterable $iterable): Generator => yield from new PsrCacheIterator((new IterableIteratorAggregate($iterable))->getIterator(), $cache); } } 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 6c0afe86e..f600bcbe4 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\iterators\MultipleIterableAggregate; use MultipleIterator; @@ -30,7 +29,7 @@ final class Combine extends AbstractOperation * * @template U * - * @return Closure(U...): Closure(Iterator): Generator + * @return Closure(U...): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -38,7 +37,7 @@ public function __invoke(): Closure /** * @param U ...$keys * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (...$keys): Closure { $buildMultipleIterable = @@ -47,13 +46,13 @@ static function (...$keys): Closure { */ static fn (array $keys): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return iterable + * @return Generator */ - static fn (Iterator $iterator): iterable => yield from new MultipleIterableAggregate([$keys, $iterator], MultipleIterator::MIT_NEED_ANY); + static fn (iterable $iterable): Generator => yield from new MultipleIterableAggregate([$keys, $iterable], MultipleIterator::MIT_NEED_ANY); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( $buildMultipleIterable($keys), Flatten::of()(1), 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 848409fdf..833a6dff6 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 bf9eb31fe..8150eb3aa 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 37d2d841e..52be54516 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 8367996a7..3acaaae0f 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -10,21 +10,22 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; -use LimitIterator; +use Generator; /** * @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): Generator */ public function __invoke(): Closure { diff --git a/src/Operation/DropWhile.php b/src/Operation/DropWhile.php index 1e15fad84..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,28 +26,28 @@ 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 { + static function (iterable $iterable) use ($callbacks): Generator { $skip = false; - foreach ($iterator as $key => $current) { + foreach ($iterable as $key => $current) { if (false === $skip) { - if (false === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + if (false === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { $skip = true; 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 4e275ffe4..99529d7c0 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/Equals.php b/src/Operation/Equals.php index 2af878e9d..82017087f 100644 --- a/src/Operation/Equals.php +++ b/src/Operation/Equals.php @@ -11,36 +11,44 @@ use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Equals extends AbstractOperation { /** * @pure * - * @return Closure(Iterator): Closure(Iterator): Generator + * @return Closure(iterable): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $other + * @param iterable $other * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ - static function (Iterator $other): Closure { + static function (iterable $other): Closure { /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - return static function (Iterator $iterator) use ($other): Generator { + return static function (iterable $iterable) use ($other): Generator { + $otherAggregate = (new IterableIteratorAggregate($other)); + $iteratorAggregate = new IterableIteratorAggregate($iterable); + + $iterator = $iteratorAggregate->getIterator(); + $other = $otherAggregate->getIterator(); + while ($other->valid() && $iterator->valid()) { $iterator->next(); $other->next(); @@ -54,9 +62,9 @@ static function (Iterator $other): Closure { /** * @param T $current */ - static fn ($current): bool => (new Contains())()($current)($other)->current(); + static fn ($current): bool => (new Contains())()($current)($otherAggregate)->current(); - yield from (new Every())()(static fn (): bool => false)($containsCallback)($iterator); + yield from (new Every())()(static fn (): bool => false)($containsCallback)($iteratorAggregate); }; }; } diff --git a/src/Operation/Every.php b/src/Operation/Every.php index 2e7b7269e..c033b12be 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 => $value), diff --git a/src/Operation/Explode.php b/src/Operation/Explode.php index 27a3d1979..d9bc29fd2 100644 --- a/src/Operation/Explode.php +++ b/src/Operation/Explode.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\collection\Contract\Operation\Splitable; /** @@ -25,7 +24,7 @@ final class Explode extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Generator> + * @return Closure(T...): Closure(iterable): Generator> */ public function __invoke(): Closure { @@ -33,10 +32,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 7ee8158b7..d7e5317f8 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 (bool $value): bool => $value; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( 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 e2c3d2805..56859f35d 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 (!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/FoldLeft1.php b/src/Operation/FoldLeft1.php index 88eced14c..c60ad686a 100644 --- a/src/Operation/FoldLeft1.php +++ b/src/Operation/FoldLeft1.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,18 +25,18 @@ final class FoldLeft1 extends AbstractOperation /** * @pure * - * @return Closure(callable((T|null), T, TKey, Iterator):(T|null)): Closure(Iterator): Generator + * @return Closure(callable((T|null), T, TKey, iterable):(T|null)): 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(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $callback): Closure { - /** @var Closure(Iterator):(Generator) $pipe */ + /** @var Closure(iterable):(Generator) $pipe */ $pipe = Pipe::of()( ScanLeft1::of()($callback), 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/FoldRight1.php b/src/Operation/FoldRight1.php index 7eb3d840a..dbd31b30f 100644 --- a/src/Operation/FoldRight1.php +++ b/src/Operation/FoldRight1.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,18 +25,18 @@ final class FoldRight1 extends AbstractOperation /** * @pure * - * @return Closure(callable((T|null), T, TKey, Iterator): (T|null)):Closure (Iterator): Generator + * @return Closure(callable((T|null), T, TKey, iterable): (T|null)):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(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $callback): Closure { - /** @var Closure(Iterator):(Generator) $pipe */ + /** @var Closure(iterable):(Generator) $pipe */ $pipe = Pipe::of()( ScanRight1::of()($callback), Head::of() diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index 48e1b4fc5..d71dec49a 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 c0ed8190c..5017641b2 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..ab7b99c5c 100644 --- a/src/Operation/Implode.php +++ b/src/Operation/Implode.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,13 +25,13 @@ final class Implode extends AbstractOperation /** * @pure * - * @return Closure(string): Closure(Iterator): Generator + * @return Closure(string): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (string $glue): Closure { $reducer = @@ -41,7 +40,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..50f2c4543 100644 --- a/src/Operation/Intersperse.php +++ b/src/Operation/Intersperse.php @@ -14,7 +14,6 @@ use Closure; use Generator; use InfiniteIterator; -use Iterator; /** * @immutable @@ -27,7 +26,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 { @@ -35,23 +34,23 @@ public function __invoke(): Closure /** * @param T $element * - * @return Closure(int): Closure(int): Closure(Iterator): Generator + * @return Closure(int): Closure(int): Closure(iterable): Generator */ 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 +60,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/IsEmpty.php b/src/Operation/IsEmpty.php index fcdfb47f0..cce075e03 100644 --- a/src/Operation/IsEmpty.php +++ b/src/Operation/IsEmpty.php @@ -11,23 +11,25 @@ use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class IsEmpty extends AbstractOperation { /** * @pure * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ public function __invoke(): Closure { - return static fn (Iterator $iterator): Generator => yield !$iterator->valid(); + return static fn (iterable $iterable): Generator => yield !(new IterableIteratorAggregate($iterable))->getIterator()->valid(); } } 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 caee2fbb4..8a65a7838 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..be14fcde5 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 Generator; +use loophp\iterators\LimitIterableAggregate; /** * @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): Generator */ public function __invoke(): Closure { return /** - * @return Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(iterable): Generator */ static fn (int $count = -1): Closure => /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): Generator */ static fn (int $offset = 0): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return Generator */ - static fn (Iterator $iterator): Iterator => new LimitIterator($iterator, $offset, $count); + static fn (iterable $iterable): Generator => yield from new LimitIterableAggregate($iterable, $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 2b1fbd50b..add22e899 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 => !$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 1cc88b0f8..619f98909 100644 --- a/src/Operation/Merge.php +++ b/src/Operation/Merge.php @@ -36,7 +36,7 @@ public function __invoke(): Closure */ static fn (iterable ...$sources): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ 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 fdd47ac69..aa42ff72a 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 (bool $value): bool => in_array($value, self::VALUES, true); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( 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..1c8cdc4f9 100644 --- a/src/Operation/Partition.php +++ b/src/Operation/Partition.php @@ -11,9 +11,9 @@ use Closure; use Generator; -use Iterator; -use IteratorAggregate; +use loophp\iterators\CachingIteratorAggregate; use loophp\iterators\ClosureIteratorAggregate; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -28,25 +28,27 @@ final class Partition extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(IteratorAggregate): Generator> + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator> */ public function __invoke(): Closure { /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(IteratorAggregate): Generator> + * @return Closure(iterable): Generator> */ return static fn (callable ...$callbacks): Closure => /** - * @param IteratorAggregate $iteratorAggregate + * @param iterable $iterable * - * @return Generator> + * @return Generator> */ - static function (IteratorAggregate $iteratorAggregate) use ($callbacks): Generator { - yield new ClosureIteratorAggregate((new Filter())()(...$callbacks), [$iteratorAggregate->getIterator()]); + static function (iterable $iterable) use ($callbacks): Generator { + $iteratorAggregate = (new CachingIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator())); - yield new ClosureIteratorAggregate((new Reject())()(...$callbacks), [$iteratorAggregate->getIterator()]); + yield new ClosureIteratorAggregate((new Filter())()(...$callbacks), [$iteratorAggregate]); + + yield new ClosureIteratorAggregate((new Reject())()(...$callbacks), [$iteratorAggregate]); }; } } diff --git a/src/Operation/Permutate.php b/src/Operation/Permutate.php index 405266314..4d2e4ee48 100644 --- a/src/Operation/Permutate.php +++ b/src/Operation/Permutate.php @@ -11,13 +11,15 @@ use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Permutate extends AbstractOperation { @@ -36,11 +38,11 @@ public function __invoke(): Closure return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator> */ - static fn (Iterator $iterator): Iterator => $getPermutations([...$iterator]); + static fn (iterable $iterable): Generator => $getPermutations([...(new IterableIteratorAggregate($iterable))]); } /** 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/Pluck.php b/src/Operation/Pluck.php index 4ecdf17a1..62c604257 100644 --- a/src/Operation/Pluck.php +++ b/src/Operation/Pluck.php @@ -10,10 +10,8 @@ namespace loophp\collection\Operation; use ArrayAccess; -use ArrayIterator; use Closure; use Generator; -use Iterator; use loophp\collection\Contract\Collection; use ReflectionClass; @@ -35,7 +33,7 @@ final class Pluck extends AbstractOperation /** * @pure * - * @return Closure(T):Closure(T):Closure(Iterator):Generator, mixed, void> + * @return Closure(T):Closure(T):Closure(iterable):Generator, mixed, void> */ public function __invoke(): Closure { @@ -43,31 +41,31 @@ public function __invoke(): Closure /** * @param T $key * - * @return Closure(T): Closure(Iterator): Generator, mixed, void> + * @return Closure(T): Closure(iterable): Generator, mixed, void> */ static fn ($key): Closure => /** * @param T $default * - * @return Closure(Iterator): Generator, mixed, void> + * @return Closure(iterable): Generator, mixed, void> */ static fn ($default): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator, mixed, void> */ - static function (Iterator $iterator) use ($key, $default): Generator { + static function (iterable $iterable) use ($key, $default): Generator { $pick = /** - * @param Iterator $iterator + * @param iterable $iterable * @param iterable|T $target * @param array $key * @param T $default * * @return iterable|T */ - static function (Iterator $iterator, $target, array $key, $default = null) use (&$pick) { + static function (iterable $iterable, $target, array $key, $default = null) use (&$pick) { while (null !== $segment = array_shift($key)) { if ('*' === $segment) { if (!is_iterable($target)) { @@ -78,16 +76,16 @@ static function (Iterator $iterator, $target, array $key, $default = null) use ( $result = []; foreach ($target as $item) { - $result[] = $pick($iterator, $item, $key); + $result[] = $pick($iterable, $item, $key); } /** @var Generator $collapse */ - $collapse = Collapse::of()(new ArrayIterator($result)); + $collapse = Collapse::of()($result); return in_array('*', $key, true) ? $collapse : $result; } - if ((is_array($target)) && (array_key_exists($segment, $target))) { + if (is_array($target) && array_key_exists($segment, $target)) { /** @var T $target */ $target = $target[$segment]; } elseif (($target instanceof ArrayAccess) && ($target->offsetExists($segment))) { @@ -95,8 +93,8 @@ static function (Iterator $iterator, $target, array $key, $default = null) use ( $target = $target[$segment]; } elseif ($target instanceof Collection) { /** @var T $target */ - $target = (Get::of()($segment)($default)($target->getIterator()))->current(); - } elseif ((is_object($target)) && (property_exists($target, $segment))) { + $target = (Get::of()($segment)($default)($target))->current(); + } elseif (is_object($target) && property_exists($target, $segment)) { /** @var T $target */ $target = (new ReflectionClass($target))->getProperty($segment)->getValue($target); } else { @@ -109,8 +107,8 @@ static function (Iterator $iterator, $target, array $key, $default = null) use ( $key = is_scalar($key) ? explode('.', trim((string) $key, '.')) : $key; - foreach ($iterator as $value) { - yield $pick($iterator, $value, $key, $default); + foreach ($iterable as $value) { + yield $pick($iterable, $value, $key, $default); } }; } diff --git a/src/Operation/Prepend.php b/src/Operation/Prepend.php index a606bb290..03fd62f6d 100644 --- a/src/Operation/Prepend.php +++ b/src/Operation/Prepend.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\iterators\ConcatIterableAggregate; /** @@ -27,7 +26,7 @@ final class Prepend extends AbstractOperation /** * @pure * - * @return Closure(T...): Closure(Iterator): Iterator + * @return Closure(T...): Closure(iterable): iterable */ public function __invoke(): Closure { @@ -35,14 +34,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 Generator */ - static fn (Iterator $iterator): Generator => yield from new ConcatIterableAggregate([$items, $iterator]); + static fn (iterable $iterable): Generator => yield from new ConcatIterableAggregate([$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 0f6c17f82..383a9b7ea 100644 --- a/src/Operation/RSample.php +++ b/src/Operation/RSample.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -26,13 +25,13 @@ 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 { $filter = (new 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/Range.php b/src/Operation/Range.php index 8ce8f13b5..9830ccf5b 100644 --- a/src/Operation/Range.php +++ b/src/Operation/Range.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use const INF; @@ -28,29 +27,27 @@ final class Range extends AbstractOperation /** * @pure * - * @return Closure(float = default):Closure (float=): Closure(float=): Closure(null|Iterator): Generator + * @return Closure(float=): Closure(float=): Closure(float=): Closure(): Generator */ public function __invoke(): Closure { return /** - * @return Closure(float=): Closure(float=): Closure(null|Iterator): Generator + * @return Closure(float=): Closure(float=): Closure(): Generator */ static fn (float $start = 0.0): Closure => /** - * @return Closure(float=): Closure(null|Iterator): Generator + * @return Closure(float=): Closure(): Generator */ static fn (float $end = INF): Closure => /** - * @return Closure(null|Iterator): Generator + * @return Closure(): Generator */ static fn (float $step = 1.0): Closure => /** - * @param Iterator|null $iterator - * * @return Generator */ - static function (?Iterator $iterator = null) use ($start, $end, $step): Generator { + static function () use ($start, $end, $step): Generator { for ($current = $start; $current < $end; $current += $step) { yield $current; } 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..4929cf1ca 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,34 @@ 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) { + /** @var W $initial */ + $initial = $callback($initial, $value, $key, $iterable); + + yield $key => $initial; } }; } 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/Same.php b/src/Operation/Same.php index 8a12670e6..1a9aa1530 100644 --- a/src/Operation/Same.php +++ b/src/Operation/Same.php @@ -11,7 +11,7 @@ use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -26,29 +26,35 @@ final class Same extends AbstractOperation /** * @pure * - * @return Closure(Iterator): Closure(callable(T, TKey): Closure(T, TKey): bool): Closure(Iterator): Generator + * @return Closure(iterable): Closure(callable(T, TKey): Closure(T, TKey): bool): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param Iterator $other + * @param iterable $other * - * @return Closure(callable(T, TKey): Closure(T, TKey): bool): Closure(Iterator): Generator + * @return Closure(callable(T, TKey): Closure(T, TKey): bool): Closure(iterable): Generator */ - static fn (Iterator $other): Closure => + static fn (iterable $other): Closure => /** * @param callable(T, TKey): (Closure(T, TKey): bool) $comparatorCallback * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable $comparatorCallback): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($other, $comparatorCallback): Generator { + static function (iterable $iterable) use ($other, $comparatorCallback): Generator { + $otherAggregate = (new IterableIteratorAggregate($other)); + $iteratorAggregate = new IterableIteratorAggregate($iterable); + + $iterator = $iteratorAggregate->getIterator(); + $other = $otherAggregate->getIterator(); + while ($iterator->valid() && $other->valid()) { if (!$comparatorCallback($iterator->current(), $iterator->key())($other->current(), $other->key())) { return yield false; 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/ScanLeft1.php b/src/Operation/ScanLeft1.php index fe4614d76..968bbcd57 100644 --- a/src/Operation/ScanLeft1.php +++ b/src/Operation/ScanLeft1.php @@ -11,7 +11,7 @@ use Closure; use Generator; -use Iterator; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -28,33 +28,31 @@ final class ScanLeft1 extends AbstractOperation * * @template V * - * @return Closure(callable(T|V, T, TKey, Iterator): V): Closure(Iterator): Generator + * @return Closure(callable(T|V, T, TKey, iterable): V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T|V, T, TKey, Iterator): V $callback + * @param callable(T|V, T, TKey, iterable): V $callback * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static fn (callable $callback): Closure => /** - * @param Iterator $iterator - * * @return Generator */ - static function (Iterator $iterator) use ($callback): Iterator { - $initial = $iterator->current(); + static function (iterable $iterable) use ($callback): Generator { + $initial = (new IterableIteratorAggregate($iterable))->getIterator()->current(); - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Tail::of(), Reduction::of()($callback)($initial), Prepend::of()($initial) ); - return $pipe($iterator); + return $pipe($iterable); }; } } 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/ScanRight1.php b/src/Operation/ScanRight1.php index 704adeb2b..a3a7bdabd 100644 --- a/src/Operation/ScanRight1.php +++ b/src/Operation/ScanRight1.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; /** * @immutable @@ -28,18 +27,18 @@ final class ScanRight1 extends AbstractOperation * * @template V * - * @return Closure(callable(T|V, T, TKey, Iterator): V): Closure(Iterator): Generator + * @return Closure(callable(T|V, T, TKey, iterable): V): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(T|V, T, TKey, Iterator): V $callback + * @param callable(T|V, T, TKey, iterable): V $callback * - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (callable $callback): Closure { - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Reverse::of(), ScanLeft1::of()($callback), diff --git a/src/Operation/Shuffle.php b/src/Operation/Shuffle.php index 8cf65a364..1560cf507 100644 --- a/src/Operation/Shuffle.php +++ b/src/Operation/Shuffle.php @@ -10,7 +10,7 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; +use Generator; use loophp\iterators\RandomIterableAggregate; /** @@ -26,20 +26,20 @@ final class Shuffle extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(Iterator): Iterator + * @return Closure(int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(Iterator): Iterator + * @return Closure(iterable): Generator */ static fn (int $seed): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return Iterator + * @return Generator */ - static fn (Iterator $iterator): Iterator => (new RandomIterableAggregate($iterator, $seed))->getIterator(); + static fn (iterable $iterable): Generator => yield from new RandomIterableAggregate($iterable, $seed); } } diff --git a/src/Operation/Since.php b/src/Operation/Since.php index 1e7cb21a5..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,28 +26,28 @@ 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 { + static function (iterable $iterable) use ($callbacks): Generator { $skip = false; - foreach ($iterator as $key => $current) { + foreach ($iterable as $key => $current) { if (false === $skip) { - if (true === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)) { + if (true === CallbacksArrayReducer::or()($callbacks, $current, $key, $iterable)) { $skip = true; 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/Sort.php b/src/Operation/Sort.php index 529dff4eb..7461e16e3 100644 --- a/src/Operation/Sort.php +++ b/src/Operation/Sort.php @@ -13,7 +13,6 @@ use Closure; use Exception; use Generator; -use Iterator; use loophp\collection\Contract\Operation; /** @@ -29,17 +28,17 @@ final class Sort extends AbstractOperation /** * @pure * - * @return Closure(int): Closure(callable(T|TKey, T|TKey): int): Closure(Iterator): Generator + * @return Closure(int): Closure(callable(T|TKey, T|TKey): int): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @return Closure(null|(callable(T|TKey, T|TKey): int)): Closure(Iterator): Generator + * @return Closure(null|(callable(T|TKey, T|TKey): int)): Closure(iterable): Generator */ static fn (int $type = Operation\Sortable::BY_VALUES): Closure => /** - * @return Closure(Iterator): Generator + * @return Closure(iterable): Generator */ static function (?callable $callback = null) use ($type): Closure { $callback ??= @@ -51,11 +50,11 @@ static function (?callable $callback = null) use ($type): Closure { return /** - * @param Iterator $iterator + * @param iterable $iterable * * @return Generator */ - static function (Iterator $iterator) use ($type, $callback): Iterator { + static function (iterable $iterable) use ($type, $callback): Generator { if (Operation\Sortable::BY_VALUES !== $type && Operation\Sortable::BY_KEYS !== $type) { throw new Exception('Invalid sort type.'); } @@ -83,13 +82,13 @@ static function (Iterator $iterator) use ($type, $callback): Iterator { */ static fn (array $left, array $right): int => $callback($left[1], $right[1]); - /** @var callable(Iterator): Generator | callable(Iterator): Generator $before */ + /** @var callable(iterable): Generator | callable(iterable): Generator $before */ $before = Pipe::of()(...$operations['before']); - $arrayIterator = new ArrayIterator([...$before($iterator)]); + $arrayIterator = new ArrayIterator([...$before($iterable)]); $arrayIterator->uasort($sortCallback($callback)); - return Pipe::of()(...$operations['after'])($arrayIterator); + yield from Pipe::of()(...$operations['after'])($arrayIterator); }; }; } diff --git a/src/Operation/Span.php b/src/Operation/Span.php index e25ee678d..99d8d1766 100644 --- a/src/Operation/Span.php +++ b/src/Operation/Span.php @@ -11,9 +11,9 @@ use Closure; use Generator; -use Iterator; -use IteratorAggregate; +use loophp\iterators\CachingIteratorAggregate; use loophp\iterators\ClosureIteratorAggregate; +use loophp\iterators\IterableIteratorAggregate; /** * @immutable @@ -28,25 +28,27 @@ final class Span extends AbstractOperation /** * @pure * - * @return Closure(callable(T=, TKey=, Iterator=): bool ...): Closure(IteratorAggregate): Generator> + * @return Closure(callable(T=, TKey=, iterable=): bool ...): Closure(iterable): Generator> */ public function __invoke(): Closure { /** - * @param callable(T=, TKey=, Iterator=): bool ...$callbacks + * @param callable(T=, TKey=, iterable=): bool ...$callbacks * - * @return Closure(IteratorAggregate): Generator> + * @return Closure(iterable): Generator> */ return static fn (callable ...$callbacks): Closure => /** - * @param IteratorAggregate $iteratorAggregate + * @param iterable $iterable * - * @return Generator> + * @return Generator> */ - static function (IteratorAggregate $iteratorAggregate) use ($callbacks): Generator { - yield new ClosureIteratorAggregate((new TakeWhile())()(...$callbacks), [$iteratorAggregate->getIterator()]); + static function (iterable $iterable) use ($callbacks): Generator { + $iteratorAggregate = (new CachingIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator())); - yield new ClosureIteratorAggregate((new DropWhile())()(...$callbacks), [$iteratorAggregate->getIterator()]); + yield new ClosureIteratorAggregate((new TakeWhile())()(...$callbacks), [$iteratorAggregate]); + + yield new ClosureIteratorAggregate((new DropWhile())()(...$callbacks), [$iteratorAggregate]); }; } } diff --git a/src/Operation/Split.php b/src/Operation/Split.php index e26da7da5..5f3d51438 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/Strict.php b/src/Operation/Strict.php index 2b2d06e24..42d12cdcb 100644 --- a/src/Operation/Strict.php +++ b/src/Operation/Strict.php @@ -10,8 +10,8 @@ namespace loophp\collection\Operation; use Closure; -use Iterator; -use IteratorAggregate; +use Generator; +use loophp\iterators\IterableIteratorAggregate; use loophp\iterators\TypedIteratorAggregate; /** @@ -19,13 +19,15 @@ * * @template TKey * @template T + * + * phpcs:disable Generic.Files.LineLength.TooLong */ final class Strict extends AbstractOperation { /** * @pure * - * @return Closure(null|callable(mixed): string): Closure(Iterator): IteratorAggregate + * @return Closure(null|callable(mixed): string): Closure(iterable): Generator */ public function __invoke(): Closure { @@ -33,14 +35,14 @@ public function __invoke(): Closure /** * @param null|callable(mixed): string $callback * - * @return Closure(Iterator): IteratorAggregate + * @return Closure(iterable): Generator */ static fn (?callable $callback = null): Closure => /** - * @param Iterator $iterator + * @param iterable $iterable * - * @return IteratorAggregate + * @return Generator */ - static fn (Iterator $iterator): IteratorAggregate => new TypedIteratorAggregate($iterator, $callback); + static fn (iterable $iterator): Generator => yield from new TypedIteratorAggregate((new IterableIteratorAggregate($iterator))->getIterator(), $callback); } } 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..d70a2897b 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 { - /** @var Iterator $iterator */ - $iterator = Pack::of()($iterator); + static function (iterable $iterable): Generator { + /** @var iterable $iterable */ + $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/Times.php b/src/Operation/Times.php index c52746a5e..e500f6018 100644 --- a/src/Operation/Times.php +++ b/src/Operation/Times.php @@ -32,19 +32,17 @@ public function __invoke(): Closure { return /** - * @return Closure(null|callable(int): (int|T)): Closure(null|Iterator): Generator + * @return Closure(null|callable(int): (int|T)): Closure(): Generator */ static fn (int $number = 0): Closure => /** - * @return Closure(null|Iterator): Generator + * @return Closure(): Generator */ static fn (?callable $callback = null): Closure => /** - * @param Iterator|null $iterator - * * @return Generator */ - static function (?Iterator $iterator = null) use ($number, $callback): Generator { + static function () use ($number, $callback): Generator { if (1 > $number) { return; } 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 667505519..2073924d8 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 (bool $value): bool => !$value; - /** @var Closure(Iterator): Generator $pipe */ + /** @var Closure(iterable): Generator $pipe */ $pipe = Pipe::of()( Map::of()( /** 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..9afb0c1f1 100644 --- a/src/Operation/Unpack.php +++ b/src/Operation/Unpack.php @@ -11,10 +11,9 @@ use Closure; use Generator; -use Iterator; +use IteratorAggregate; use loophp\iterators\IterableIteratorAggregate; use loophp\iterators\UnpackIterableAggregate; -use Traversable; // phpcs:disable Generic.Files.LineLength.TooLong @@ -32,16 +31,16 @@ 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() + static fn (iterable $iterable): IteratorAggregate => new UnpackIterableAggregate($iterable) ); // 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 a0e49e474..67524eda6 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 ($predicate($iterator) ? $whenTrue($iterator) : $whenFalse($iterator)); + static fn (iterable $iterable): Generator => yield from $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 08701b42e..43462a5f4 100644 --- a/src/Operation/Zip.php +++ b/src/Operation/Zip.php @@ -11,7 +11,6 @@ use Closure; use Generator; -use Iterator; use loophp\iterators\MultipleIterableAggregate; use MultipleIterator; @@ -31,7 +30,7 @@ final class Zip extends AbstractOperation * @template UKey * @template U * - * @return Closure(iterable...): Closure(Iterator): Generator, array> + * @return Closure(iterable...): Closure(iterable): Generator, array> */ public function __invoke(): Closure { @@ -39,7 +38,7 @@ public function __invoke(): Closure /** * @param iterable ...$iterables * - * @return Closure(Iterator): Generator, array> + * @return Closure(iterable): Generator, array> */ static fn (iterable ...$iterables): Closure => /** 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 ); } diff --git a/tests/unit/CollectionSpecificOperationTest.php b/tests/unit/CollectionSpecificOperationTest.php index a5fd4cc3c..353aae39a 100644 --- a/tests/unit/CollectionSpecificOperationTest.php +++ b/tests/unit/CollectionSpecificOperationTest.php @@ -467,13 +467,20 @@ public function testSpanOperation(): void CollectionInterface::class, $subject->last() ); + + $first = $subject->first()->current(); + $last = $subject->last()->current(); + + self::assertCount(3, $first); + self::assertCount(7, $last); + self::assertIdenticalIterable( [1, 2, 3], - $subject->first()->current() + $first ); self::assertIdenticalIterable( [3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8, 8 => 9, 9 => 10], - $subject->last()->current() + $last ); $callbacks = [ diff --git a/tests/unit/Traits/GenericCollectionProviders.php b/tests/unit/Traits/GenericCollectionProviders.php index 99b2fc25f..272eab3de 100644 --- a/tests/unit/Traits/GenericCollectionProviders.php +++ b/tests/unit/Traits/GenericCollectionProviders.php @@ -14,11 +14,10 @@ use Closure; use Doctrine\Common\Collections\Criteria; use Generator; -use Iterator; +use IteratorAggregate; use loophp\collection\Collection; use loophp\collection\Contract\Operation; use loophp\collection\Operation\AbstractOperation; -use loophp\iterators\ClosureIterator; use stdClass; use const PHP_EOL; @@ -1146,7 +1145,7 @@ public function everyOperationProvider() yield [ $operation, [ - static fn ($value, $key, Iterator $iterator): bool => $iterator instanceof ClosureIterator, + static fn ($value, $key, iterable $iterable): bool => $iterable instanceof IteratorAggregate, ], $input, true, @@ -4280,7 +4279,7 @@ public function whenOperationProvider() $operation, [ static fn (): bool => true, - static fn (Iterator $iterator) => new ArrayIterator(range('c', 'a')), + static fn (iterable $iterable) => range('c', 'a'), ], $input, [0 => 'c', 1 => 'b', 2 => 'a'], @@ -4290,7 +4289,7 @@ public function whenOperationProvider() $operation, [ static fn (): bool => false, - static fn (Iterator $iterator) => new ArrayIterator(range('c', 'a')), + static fn (iterable $iterable) => range('c', 'a'), ], $input, [0 => 'a', 1 => 'b', 2 => 'c'], diff --git a/tests/unit/Utils/CallbacksArrayReducerTest.php b/tests/unit/Utils/CallbacksArrayReducerTest.php index d038bffb3..249c6eb9b 100644 --- a/tests/unit/Utils/CallbacksArrayReducerTest.php +++ b/tests/unit/Utils/CallbacksArrayReducerTest.php @@ -9,8 +9,6 @@ namespace tests\loophp\collection\Utils; -use ArrayIterator; -use Iterator; use loophp\collection\Utils\CallbacksArrayReducer; use PHPUnit\Framework\TestCase; @@ -25,7 +23,7 @@ public function dataProvider() // Ensure Callbacks receive the needed arguments. yield [ [ - static fn (string $value, string $key, Iterator $iterator): bool => 'value_key_a_b_c' === sprintf('%s_%s_%s', $value, $key, implode('_', iterator_to_array($iterator))), + static fn (string $value, string $key, iterable $iterable): bool => 'value_key_a_b_c' === sprintf('%s_%s_%s', $value, $key, implode('_', $iterable)), ], 'value', 'key', @@ -111,6 +109,6 @@ public function testGeneric( array $iterator, bool $expected ): void { - self::assertSame($expected, CallbacksArrayReducer::or()($callbacks, $current, $key, new ArrayIterator($iterator))); + self::assertSame($expected, CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator)); } }