Skip to content

Commit

Permalink
Convert Transformations into Operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 28, 2020
1 parent f40dc3b commit e36b03c
Show file tree
Hide file tree
Showing 42 changed files with 503 additions and 570 deletions.
26 changes: 13 additions & 13 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ truthy

Interface: `Truthyable`_

.. _Allable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Allable.php
.. _Allable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Allable.php
.. _Appendable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Appendable.php
.. _Applyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Applyable.php
.. _Associateable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Associateable.php
Expand All @@ -1528,28 +1528,28 @@ Interface: `Truthyable`_
.. _Combinateable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Combinateable.php
.. _Combineable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Combineable.php
.. _Compactable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Compactable.php
.. _Containsable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Containsable.php
.. _Containsable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Containsable.php
.. _Cycleable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Cycleable.php
.. _Diffable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffable.php
.. _Diffkeysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffkeysable.php
.. _Distinctable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Distinctable.php
.. _Explodeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Explodeable.php
.. _Falsyable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Falsyable.php
.. _Falsyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Falsyable.php
.. _Filterable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Filterable.php
.. _Firstable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Firstable.php
.. _Flattenable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Flattenable.php
.. _Flipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Flipable.php
.. _array_flip(): https://php.net/array_flip
.. _FoldLeftable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/FoldLeftable.php
.. _FoldRightable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/FoldRightable.php
.. _FoldLeftable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/FoldLeftable.php
.. _FoldRightable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/FoldRightable.php
.. _Forgetable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Forgetable.php
.. _Frequencyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Frequencyable.php
.. _Getable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Getable.php
.. _Getable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Getable.php
.. _Groupable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Groupable.php
.. _Hasable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Hasable.php
.. _Hasable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Hasable.php
.. _Headable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Headable.php
.. _IfThenElseable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/IfThenElseable.php
.. _Implodeable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Implodeable.php
.. _Implodeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Implodeable.php
.. _Intersectable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Intersectable.php
.. _Intersectkeysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Intersectkeysable.php
.. _Intersperseable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Intersperseable.php
Expand All @@ -1561,7 +1561,7 @@ Interface: `Truthyable`_
.. _Mergeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Mergeable.php
.. _Normalizeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Normalizeable.php
.. _Nthable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Nthable.php
.. _Nullsyable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Nullsyable.php
.. _Nullsyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Nullsyable.php
.. _Onlyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Onlyable.php
.. _Packable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Packable.php
.. _Padable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Padable.php
Expand All @@ -1571,20 +1571,20 @@ Interface: `Truthyable`_
.. _Prependable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Prependable.php
.. _Productable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Productable.php
.. _Randomable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Randomable.php
.. _Reduceable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Reduceable.php
.. _Reduceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Reduceable.php
.. _Reductionable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Reductionable.php
.. _Reverseable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Reverseable.php
.. _Runable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Runable.php
.. _Runable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Runable.php
.. _Scaleable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Scaleable.php
.. _Skipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Skipable.php
.. _Sinceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sinceable.php
.. _Sliceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sliceable.php
.. _Sortable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sortable.php
.. _Splitable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Splitable.php
.. _Tailable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Tailable.php
.. _Transformable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Transformable.php
.. _Transformable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Transformable.php
.. _Transposeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Transposeable.php
.. _Truthyable: https://github.com/loophp/collection/blob/master/src/Contract/Transformation/Truthyable.php
.. _Truthyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Truthyable.php
.. _Unpackable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unpackagle.php
.. _Unpairable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Unpairable.php
.. _Untilable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Untilable.php
Expand Down
62 changes: 29 additions & 33 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,28 @@
use loophp\collection\Operation\Combinate;
use loophp\collection\Operation\Combine;
use loophp\collection\Operation\Compact;
use loophp\collection\Operation\Compose;
use loophp\collection\Operation\Contains;
use loophp\collection\Operation\Cycle;
use loophp\collection\Operation\Diff;
use loophp\collection\Operation\DiffKeys;
use loophp\collection\Operation\Distinct;
use loophp\collection\Operation\Explode;
use loophp\collection\Operation\Falsy;
use loophp\collection\Operation\Filter;
use loophp\collection\Operation\First;
use loophp\collection\Operation\Flatten;
use loophp\collection\Operation\Flip;
use loophp\collection\Operation\FoldLeft;
use loophp\collection\Operation\FoldRight;
use loophp\collection\Operation\Forget;
use loophp\collection\Operation\Frequency;
use loophp\collection\Operation\Get;
use loophp\collection\Operation\Group;
use loophp\collection\Operation\Has;
use loophp\collection\Operation\Head;
use loophp\collection\Operation\IfThenElse;
use loophp\collection\Operation\Implode;
use loophp\collection\Operation\Intersect;
use loophp\collection\Operation\IntersectKeys;
use loophp\collection\Operation\Intersperse;
Expand All @@ -48,6 +56,7 @@
use loophp\collection\Operation\Merge;
use loophp\collection\Operation\Normalize;
use loophp\collection\Operation\Nth;
use loophp\collection\Operation\Nullsy;
use loophp\collection\Operation\Only;
use loophp\collection\Operation\Pack;
use loophp\collection\Operation\Pad;
Expand All @@ -58,9 +67,11 @@
use loophp\collection\Operation\Product;
use loophp\collection\Operation\Random;
use loophp\collection\Operation\Range;
use loophp\collection\Operation\Reduce;
use loophp\collection\Operation\Reduction;
use loophp\collection\Operation\Reverse;
use loophp\collection\Operation\RSample;
use loophp\collection\Operation\Run;
use loophp\collection\Operation\Scale;
use loophp\collection\Operation\Shuffle;
use loophp\collection\Operation\Since;
Expand All @@ -71,27 +82,14 @@
use loophp\collection\Operation\Tail;
use loophp\collection\Operation\Times;
use loophp\collection\Operation\Transpose;
use loophp\collection\Operation\Truthy;
use loophp\collection\Operation\Unpack;
use loophp\collection\Operation\Unpair;
use loophp\collection\Operation\Until;
use loophp\collection\Operation\Unwrap;
use loophp\collection\Operation\Window;
use loophp\collection\Operation\Wrap;
use loophp\collection\Operation\Zip;
use loophp\collection\Transformation\All;
use loophp\collection\Transformation\Contains;
use loophp\collection\Transformation\Count;
use loophp\collection\Transformation\Falsy;
use loophp\collection\Transformation\FoldLeft;
use loophp\collection\Transformation\FoldRight;
use loophp\collection\Transformation\Get;
use loophp\collection\Transformation\Has;
use loophp\collection\Transformation\Implode;
use loophp\collection\Transformation\Nullsy;
use loophp\collection\Transformation\Reduce;
use loophp\collection\Transformation\Run;
use loophp\collection\Transformation\Transform;
use loophp\collection\Transformation\Truthy;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

Expand Down Expand Up @@ -225,7 +223,7 @@ static function ($data): Generator {

public function all(): array
{
return $this->transform(All::of());
return iterator_to_array($this);
}

public function append(...$items): CollectionInterface
Expand Down Expand Up @@ -282,12 +280,12 @@ public function compact(...$values): CollectionInterface

public function contains(...$value): bool
{
return $this->transform(Contains::of()(...$value));
return $this->run(Contains::of()(...$value))->getIterator()->current();
}

public function count(): int
{
return $this->transform(Count::of());
return iterator_count($this);
}

public function cycle(int $length = 0): CollectionInterface
Expand Down Expand Up @@ -322,7 +320,7 @@ public function explode(...$explodes): CollectionInterface

public function falsy(): bool
{
return $this->transform(Falsy::of());
return $this->run(Falsy::of())->getIterator()->current();
}

public function filter(callable ...$callbacks): CollectionInterface
Expand All @@ -347,12 +345,12 @@ public function flip(): CollectionInterface

public function foldLeft(callable $callback, $initial = null)
{
return $this->transform(FoldLeft::of()($callback)($initial));
return $this->run(FoldLeft::of()($callback)($initial))->getIterator()->current();
}

public function foldRight(callable $callback, $initial = null)
{
return $this->transform(Foldright::of()($callback)($initial));
return $this->run(Foldright::of()($callback)($initial))->getIterator()->current();
}

public function forget(...$keys): CollectionInterface
Expand Down Expand Up @@ -409,7 +407,7 @@ static function (string $string, string $delimiter): Generator {

public function get($key, $default = null)
{
return $this->transform(Get::of()($key)($default));
return $this->run(Get::of()($key)($default))->getIterator()->current();
}

public function getIterator(): ClosureIterator
Expand All @@ -424,7 +422,7 @@ public function group(?callable $callable = null): CollectionInterface

public function has(callable $callback): bool
{
return $this->transform(Has::of()($callback));
return $this->run(Has::of()($callback))->getIterator()->current();
}

public function head(): CollectionInterface
Expand All @@ -443,7 +441,7 @@ public function ifThenElse(callable $condition, callable $then, ?callable $else

public function implode(string $glue = ''): string
{
return $this->transform(Implode::of()($glue));
return $this->run(Implode::of()($glue))->getIterator()->current();
}

public function intersect(...$values): CollectionInterface
Expand Down Expand Up @@ -516,7 +514,7 @@ public function nth(int $step, int $offset = 0): CollectionInterface

public function nullsy(): bool
{
return $this->transform(Nullsy::of());
return $this->run(Nullsy::of())->getIterator()->current();
}

public function only(...$keys): CollectionInterface
Expand Down Expand Up @@ -571,7 +569,7 @@ public static function range(float $start = 0.0, float $end = INF, float $step =

public function reduce(callable $callback, $initial = null)
{
return $this->transform(Reduce::of()($callback)($initial));
return $this->run(Reduce::of()($callback)($initial))->getIterator()->current();
}

public function reduction(callable $callback, $initial = null): CollectionInterface
Expand All @@ -589,9 +587,12 @@ public function rsample(float $probability): CollectionInterface
return $this->run(RSample::of()($probability));
}

public function run(callable ...$operations)
public function run(callable ...$operations): CollectionInterface
{
return self::fromIterable(Run::of()(...$operations)($this->getIterator()));
return self::fromCallable(
Compose::of()(...$operations),
$this->getIterator()
);
}

public function scale(
Expand Down Expand Up @@ -644,19 +645,14 @@ public static function times(int $number = 0, ?callable $callback = null): Colle
return (new self())->run(Times::of()($number)($callback));
}

public function transform(callable ...$transformers)
{
return Transform::of()(...$transformers)($this->getIterator());
}

public function transpose(): CollectionInterface
{
return $this->run(Transpose::of());
}

public function truthy(): bool
{
return $this->transform(Truthy::of());
return $this->run(Truthy::of())->getIterator()->current();
}

public function unpack(): CollectionInterface
Expand Down
28 changes: 12 additions & 16 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use IteratorAggregate;
use JsonSerializable;
use loophp\collection\Contract\Operation\Allable;
use loophp\collection\Contract\Operation\Appendable;
use loophp\collection\Contract\Operation\Applyable;
use loophp\collection\Contract\Operation\Associateable;
Expand All @@ -16,20 +17,27 @@
use loophp\collection\Contract\Operation\Combinateable;
use loophp\collection\Contract\Operation\Combineable;
use loophp\collection\Contract\Operation\Compactable;
use loophp\collection\Contract\Operation\Containsable;
use loophp\collection\Contract\Operation\Cycleable;
use loophp\collection\Contract\Operation\Diffable;
use loophp\collection\Contract\Operation\Diffkeysable;
use loophp\collection\Contract\Operation\Distinctable;
use loophp\collection\Contract\Operation\Explodeable;
use loophp\collection\Contract\Operation\Falsyable;
use loophp\collection\Contract\Operation\Filterable;
use loophp\collection\Contract\Operation\Firstable;
use loophp\collection\Contract\Operation\Flattenable;
use loophp\collection\Contract\Operation\Flipable;
use loophp\collection\Contract\Operation\FoldLeftable;
use loophp\collection\Contract\Operation\FoldRightable;
use loophp\collection\Contract\Operation\Forgetable;
use loophp\collection\Contract\Operation\Frequencyable;
use loophp\collection\Contract\Operation\Getable;
use loophp\collection\Contract\Operation\Groupable;
use loophp\collection\Contract\Operation\Hasable;
use loophp\collection\Contract\Operation\Headable;
use loophp\collection\Contract\Operation\IfThenElseable;
use loophp\collection\Contract\Operation\Implodeable;
use loophp\collection\Contract\Operation\Intersectable;
use loophp\collection\Contract\Operation\Intersectkeysable;
use loophp\collection\Contract\Operation\Intersperseable;
Expand All @@ -42,6 +50,7 @@
use loophp\collection\Contract\Operation\Mergeable;
use loophp\collection\Contract\Operation\Normalizeable;
use loophp\collection\Contract\Operation\Nthable;
use loophp\collection\Contract\Operation\Nullsyable;
use loophp\collection\Contract\Operation\Onlyable;
use loophp\collection\Contract\Operation\Padable;
use loophp\collection\Contract\Operation\Pairable;
Expand All @@ -51,9 +60,11 @@
use loophp\collection\Contract\Operation\Productable;
use loophp\collection\Contract\Operation\Randomable;
use loophp\collection\Contract\Operation\Rangeable;
use loophp\collection\Contract\Operation\Reduceable;
use loophp\collection\Contract\Operation\Reductionable;
use loophp\collection\Contract\Operation\Reverseable;
use loophp\collection\Contract\Operation\RSampleable;
use loophp\collection\Contract\Operation\Runable;
use loophp\collection\Contract\Operation\Scaleable;
use loophp\collection\Contract\Operation\Shuffleable;
use loophp\collection\Contract\Operation\Sinceable;
Expand All @@ -64,25 +75,13 @@
use loophp\collection\Contract\Operation\Tailable;
use loophp\collection\Contract\Operation\Timesable;
use loophp\collection\Contract\Operation\Transposeable;
use loophp\collection\Contract\Operation\Truthyable;
use loophp\collection\Contract\Operation\Unpairable;
use loophp\collection\Contract\Operation\Untilable;
use loophp\collection\Contract\Operation\Unwrapable;
use loophp\collection\Contract\Operation\Windowable;
use loophp\collection\Contract\Operation\Wrapable;
use loophp\collection\Contract\Operation\Zipable;
use loophp\collection\Contract\Transformation\Allable;
use loophp\collection\Contract\Transformation\Containsable;
use loophp\collection\Contract\Transformation\Falsyable;
use loophp\collection\Contract\Transformation\FoldLeftable;
use loophp\collection\Contract\Transformation\FoldRightable;
use loophp\collection\Contract\Transformation\Getable;
use loophp\collection\Contract\Transformation\Hasable;
use loophp\collection\Contract\Transformation\Implodeable;
use loophp\collection\Contract\Transformation\Nullsyable;
use loophp\collection\Contract\Transformation\Reduceable;
use loophp\collection\Contract\Transformation\Runable;
use loophp\collection\Contract\Transformation\Transformable;
use loophp\collection\Contract\Transformation\Truthyable;

/**
* @psalm-template TKey
Expand Down Expand Up @@ -159,7 +158,6 @@
* @template-extends Zipable<TKey, T>
* @template-extends \IteratorAggregate<TKey, T>
* @template-extends Runable<TKey, T>
* @template-extends Transformable<TKey, T>
*/
interface Collection extends
Allable,
Expand Down Expand Up @@ -222,7 +220,6 @@ interface Collection extends
Reductionable,
Reverseable,
RSampleable,
Runable,
Scaleable,
Shuffleable,
Sinceable,
Expand All @@ -232,7 +229,6 @@ interface Collection extends
Splitable,
Tailable,
Timesable,
Transformable,
Transposeable,
Truthyable,
Unpairable,
Expand Down
Loading

0 comments on commit e36b03c

Please sign in to comment.