Skip to content

Commit

Permalink
refactor: Use more iterator aggregates.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 28, 2022
1 parent 669a956 commit 19616a6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 226 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"require": {
"php": ">= 7.4",
"loophp/iterators": "^1.5.9"
"loophp/iterators": "^1.5.13"
},
"require-dev": {
"amphp/parallel-functions": "^1",
Expand Down
79 changes: 0 additions & 79 deletions src/Iterator/MultipleIterableIterator.php

This file was deleted.

14 changes: 8 additions & 6 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,38 @@
namespace loophp\collection\Operation;

use Closure;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;
use Generator;
use loophp\iterators\ConcatIterableAggregate;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Append extends AbstractOperation
{
/**
* @pure
*
* @return Closure(T...): Closure(Iterator<TKey, T>): Iterator<int|TKey, T>
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$items
*
* @return Closure(Iterator<TKey, T>): Iterator<int|TKey, T>
* @return Closure(iterable<TKey, T>): Generator<int|TKey, T>
*/
static fn (...$items): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Iterator<int|TKey, T>
* @return Generator<int|TKey, T>
*/
static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($iterator, $items);
static fn (iterable $iterable): Generator => yield from new ConcatIterableAggregate([$iterable, $items]);
}
}
33 changes: 13 additions & 20 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

namespace loophp\collection\Operation;

use ArrayIterator;
use Closure;
use Generator;
use Iterator;
use loophp\iterators\MultipleIterableAggregate;
use MultipleIterator;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Combine extends AbstractOperation
{
Expand All @@ -39,30 +41,21 @@ public function __invoke(): Closure
* @return Closure(Iterator<TKey, T>): Generator<null|U, null|T>
*/
static function (...$keys): Closure {
$buildMultipleIterator =
$buildMultipleIterable =
/**
* @param Iterator<int, U> $keyIterator
* @param array<array-key, U> $keys
*/
static function (Iterator $keyIterator): Closure {
return
/**
* @param Iterator<TKey, T> $iterator
*
* @return MultipleIterator
*/
static function (Iterator $iterator) use ($keyIterator): MultipleIterator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);

$mit->attachIterator($keyIterator);
$mit->attachIterator($iterator);

return $mit;
};
};
static fn (array $keys): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return iterable
*/
static fn (Iterator $iterator): iterable => yield from new MultipleIterableAggregate([$keys, $iterator], MultipleIterator::MIT_NEED_ANY);

/** @var Closure(Iterator<TKey, T>): Generator<null|U, null|T> $pipe */
$pipe = Pipe::of()(
$buildMultipleIterator(new ArrayIterator($keys)),
$buildMultipleIterable($keys),
Flatten::of()(1),
Pair::of(),
);
Expand Down
12 changes: 7 additions & 5 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@
namespace loophp\collection\Operation;

use Closure;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;
use Generator;
use loophp\iterators\ConcatIterableAggregate;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Merge extends AbstractOperation
{
/**
* @pure
*
* @return Closure(iterable<TKey, T>...): Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @return Closure(iterable<TKey, T> ...$sources): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
Expand All @@ -36,8 +38,8 @@ public function __invoke(): Closure
/**
* @param Iterator<TKey, T> $iterator
*
* @return Iterator<TKey, T>
* @return Generator<TKey, T>
*/
static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($iterator, ...$sources);
static fn (iterable $iterable): Generator => yield from new ConcatIterableAggregate([$iterable, ...array_values($sources)]);
}
}
9 changes: 6 additions & 3 deletions src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;
use loophp\iterators\ConcatIterableAggregate;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Prepend extends AbstractOperation
{
Expand All @@ -38,8 +41,8 @@ public function __invoke(): Closure
/**
* @param Iterator<TKey, T> $iterator
*
* @return Iterator<int|TKey, T>
* @return Generator<int|TKey, T>
*/
static fn (Iterator $iterator): Iterator => new MultipleIterableIterator($items, $iterator);
static fn (Iterator $iterator): Generator => yield from new ConcatIterableAggregate([$items, $iterator]);
}
}
63 changes: 10 additions & 53 deletions src/Operation/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

namespace loophp\collection\Operation;

use ArrayIterator;
use Closure;
use Generator;
use Iterator;
use loophp\iterators\IterableIterator;
use loophp\iterators\MultipleIterableAggregate;
use MultipleIterator;

/**
Expand All @@ -32,64 +31,22 @@ final class Zip extends AbstractOperation
* @template UKey
* @template U
*
* @return Closure(iterable<UKey, U>...): Closure(Iterator<TKey, T>): Iterator<list<TKey|UKey>, list<T|U>>
* @return Closure(iterable<UKey, U>...): Closure(Iterator<TKey, T>): Generator<array<int, TKey|UKey|null>, array<int, T|U|null>>
*/
public function __invoke(): Closure
{
return
/**
* @param iterable<UKey, U> ...$iterables
*
* @return Closure(Iterator<TKey, T>): Iterator<list<TKey|UKey>, list<T|U>>
* @return Closure(Iterator<TKey, T>): Generator<array<int, TKey|UKey|null>, array<int, T|U|null>>
*/
static function (iterable ...$iterables): Closure {
$buildArrayIterator =
/**
* @param list<iterable<UKey, U>> $iterables
*/
static fn (array $iterables): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return ArrayIterator<int, (Iterator<TKey, T>|IterableIterator<UKey, U>)>
*/
static fn (Iterator $iterator): Iterator => new ArrayIterator([
$iterator,
...array_map(
/**
* @param iterable<UKey, U> $iterable
*
* @return IterableIterator<UKey, U>
*/
static fn (iterable $iterable): IterableIterator => new IterableIterator($iterable),
$iterables
),
]);

$buildMultipleIterator =
/**
* @return Closure(ArrayIterator<int, (Iterator<TKey, T>|IterableIterator<UKey, U>)>): MultipleIterator
*/
(new Reduce())()(
/**
* @param Iterator<TKey, T> $iterator
*/
static function (MultipleIterator $acc, Iterator $iterator): MultipleIterator {
$acc->attachIterator($iterator);

return $acc;
}
)(new MultipleIterator(MultipleIterator::MIT_NEED_ANY));

/** @var Closure(Iterator<TKey, T>): Generator<list<TKey|UKey>, list<T|U>> $pipe */
$pipe = Pipe::of()(
$buildArrayIterator($iterables),
$buildMultipleIterator,
((new Flatten())()(1))
);

// Point free style.
return $pipe;
};
static fn (iterable ...$iterables): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<array<int, TKey|UKey|null>, array<int, T|U|null>>
*/
static fn (iterable $iterable): Generator => yield from new MultipleIterableAggregate([$iterable, ...array_values($iterables)], MultipleIterator::MIT_NEED_ANY);
}
}
Loading

0 comments on commit 19616a6

Please sign in to comment.