Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 23, 2022
1 parent e19bde8 commit a55efd4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
13 changes: 6 additions & 7 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

use Closure;
use Generator;
use Iterator;
use loophp\iterators\IterableIterator;
use loophp\iterators\IterableIteratorAggregate;

/**
* @immutable
Expand All @@ -25,19 +24,19 @@ final class Flatten extends AbstractOperation
/**
* @pure
*
* @return Closure(int): Closure(Iterator<TKey, T>): Generator<mixed, mixed>
* @return Closure(int): Closure(iterable<TKey, T>): Generator<mixed, mixed>
*/
public function __invoke(): Closure
{
return
/**
* @return Closure(Iterator<TKey, T>): Generator<mixed, mixed>
* @return Closure(iterable<TKey, T>): Generator<mixed, mixed>
*/
static fn (int $depth): Closure =>
/**
* @param Iterator<TKey, T> $iterator
* @param iterable<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($depth): Generator {
static function (iterable $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
Expand All @@ -46,7 +45,7 @@ static function (Iterator $iterator) use ($depth): Generator {
}

yield from (1 !== $depth)
? (new Flatten())()($depth - 1)(new IterableIterator($value))
? (new Flatten())()($depth - 1)(new IterableIteratorAggregate($value))
: $value;
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/Operation/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use Traversable;

/**
* @immutable
Expand Down Expand Up @@ -42,7 +43,7 @@ public function __invoke(): Closure
*
* @return Generator<TKey, V>
*/
static function (Iterator $iterator) use ($callback): Generator {
static function (Traversable $iterator) use ($callback): Generator {
foreach ($iterator as $key => $value) {
yield $key => $callback($value, $key, $iterator);
}
Expand Down
22 changes: 11 additions & 11 deletions src/Operation/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace loophp\collection\Operation;

use Closure;
use Iterator;
use Traversable;

/**
* @immutable
Expand All @@ -25,32 +25,32 @@ final class Pipe extends AbstractOperation
/**
* @pure
*
* @return Closure(callable(Iterator<TKey, T>): Iterator<TKey, T> ...): Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @return Closure(callable(Traversable<TKey, T>): Traversable<TKey, T> ...): Closure(Traversable<TKey, T>): Traversable<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> ...$operations
* @param callable(Traversable<TKey, T>): Traversable<TKey, T> ...$operations
*
* @return Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @return Closure(Traversable<TKey, T>): Traversable<TKey, T>
*/
static fn (callable ...$operations): Closure => array_reduce(
$operations,
/**
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> $f
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> $g
* @param callable(Traversable<TKey, T>): Traversable<TKey, T> $f
* @param callable(Traversable<TKey, T>): Traversable<TKey, T> $g
*
* @return Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @return Closure(Traversable<TKey, T>): Traversable<TKey, T>
*/
static fn (callable $f, callable $g): Closure =>
/**
* @param Iterator<TKey, T> $iterator
* @param Traversable<TKey, T> $traversable
*
* @return Iterator<TKey, T>
* @return Traversable<TKey, T>
*/
static fn (Iterator $iterator): Iterator => $g($f($iterator)),
static fn (Iterator $iterator): Iterator => $iterator
static fn (Traversable $traversable): Traversable => $g($f($traversable)),
static fn (Traversable $traversable): Traversable => $traversable
);
}
}
27 changes: 15 additions & 12 deletions src/Operation/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
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
{
Expand All @@ -35,47 +38,47 @@ public function __invoke(): Closure
/**
* @param iterable<UKey, U> ...$iterables
*
* @return Closure(Iterator<TKey, T>): Generator<int, list<T|U>>
* @return Closure(Iterator<TKey, T>): Generator<int, array<array-key, T|U>>
*/
static function (iterable ...$iterables): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int, list<T|U>> $pipe */
/** @var Closure(Iterator<TKey, T>): Generator<int, array<array-key, T|U>> $pipe */
$pipe = Pipe::of()(
(
/**
* @param list<Iterator<UKey, U>> $iterables
* @param array<int, Traversable<UKey, U>> $iterables
*/
static fn (array $iterables): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*/
static fn (Iterator $iterator): Generator => (
/**
* @param Closure(Iterator<TKey, T>): (Closure(Iterator<UKey, U>): Generator<list<T|U>>) $f
* @param Closure(Iterator<TKey, T>): (Closure(Iterator<UKey, U>): Generator<array<array-key, T|U>>) $f
*/
static fn (Closure $f): Closure => (new FoldLeft())()(
/**
* @param Iterator<UKey, U> $a
* @param Iterator<TKey, T> $x
*/
static fn (Iterator $a, Iterator $x): Generator => $f($x)($a)
static fn (Iterator $a, Traversable $x): Generator => $f($x)($a)
)
)(
/**
* @param (Iterator<TKey, T>|Iterator<UKey, U>) $xs
*/
static fn (Iterator $xs): Closure =>
static fn (Traversable $xs): Closure =>
/**
* @param Iterator<int, list<T>> $as
* @param Iterator<int, array<array-key, T>> $as
*/
static fn (Iterator $as): Generator => (new FlatMap())()(
/**
* @param list<T> $a
* @param array<int, T> $a
*/
static fn (array $a): Generator => (new FlatMap())()(
/**
* @param T|U $x
*
* @return Generator<int, list<T|U>>
* @return Generator<int, array<array-key, T|U>>
*/
static fn ($x): Generator => yield [...$a, $x]
)($xs)
Expand All @@ -86,9 +89,9 @@ static function (iterable ...$iterables): Closure {
/**
* @param iterable<UKey, U> $iterable
*
* @return Iterator<UKey, U>
* @return IterableIteratorAggregate<UKey, U>
*/
static fn (iterable $iterable): Iterator => new IterableIterator($iterable),
static fn (iterable $iterable): IterableIteratorAggregate => new IterableIteratorAggregate($iterable),
$iterables
)
),
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\iterators\IterableIterator;
use loophp\iterators\IterableIteratorAggregate;
use MultipleIterator;

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __invoke(): Closure
$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;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Generator;
use Iterator;
use loophp\iterators\IterableIterator;
use loophp\iterators\IterableIteratorAggregate;
use MultipleIterator;

/**
Expand Down Expand Up @@ -51,17 +52,17 @@ static function (iterable ...$iterables): Closure {
/**
* @param Iterator<TKey, T> $iterator
*
* @return ArrayIterator<int, (Iterator<TKey, T>|IterableIterator<UKey, U>)>
* @return ArrayIterator<int, (Iterator<TKey, T>|Iterator<UKey, U>)>
*/
static fn (Iterator $iterator): Iterator => new ArrayIterator([
$iterator,
...array_map(
/**
* @param iterable<UKey, U> $iterable
*
* @return IterableIterator<UKey, U>
* @return Iterator<UKey, U>
*/
static fn (iterable $iterable): IterableIterator => new IterableIterator($iterable),
static fn (iterable $iterable): Iterator => (new IterableIteratorAggregate($iterable))->getIterator(),
$iterables
),
]);
Expand Down

0 comments on commit a55efd4

Please sign in to comment.