Skip to content

Commit

Permalink
Rename minor stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 28, 2019
1 parent 03c7f87 commit ec8ee0e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ On top of this, this library:
* is [immutable](https://en.wikipedia.org/wiki/Immutable_object),
* is extendable,
* leverages the power of PHP [generators](https://www.php.net/manual/en/class.generator.php) and [iterators](https://www.php.net/manual/en/class.iterator.php),
* uses [S.O.L.I.D principles](https://en.wikipedia.org/wiki/SOLID),
* uses [S.O.L.I.D. principles](https://en.wikipedia.org/wiki/SOLID),
* doesn't depends or require any other library or framework.

Except a few methods, most of methods are [pure](https://en.wikipedia.org/wiki/Pure_function) and return a
Expand Down Expand Up @@ -119,7 +119,7 @@ Collection::with(['A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E'])
)
->all(); // ['A' => 'a', B => 'b', 'C' => 'c', 'D' = >'d', 'E' => 'e']

// Infinitely loop over numbers, square them, filter those that are not divisible by 5, take the first 100 of them.
// Infinitely loop over numbers, cube them, filter those that are not divisible by 5, take the first 100 of them.
Collection::range(0, INF)
->map(
static function ($value, $key) {
Expand Down
13 changes: 8 additions & 5 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ static function () use ($collection) {
);
$keysIterator = new \ArrayIterator($keys);

for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next()
) {
while ($original->valid() && $keysIterator->valid()) {
yield $keysIterator->current() => $original->current();

$original->next();
$keysIterator->next();
}

if ((true === $original->valid() && false === $keysIterator->valid()) ||
(false === $original->valid() && true === $keysIterator->valid())
) {
$predicate1 = (true === $original->valid() && false === $keysIterator->valid());
$predicate2 = (false === $original->valid() && true === $keysIterator->valid());

if ($predicate1 || $predicate2) {
\trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING);
}
};
Expand Down
5 changes: 5 additions & 0 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class Merge implements Operation
*/
private $sources;

/**
* Merge constructor.
*
* @param iterable ...$sources
*/
public function __construct(iterable ...$sources)
{
$this->sources = $sources;
Expand Down
8 changes: 3 additions & 5 deletions src/Transformation/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public function on(iterable $collection)
$result = [];

foreach ($collection as $key => $value) {
if (true === \is_iterable($value)) {
$result[$key] = $this->on($value);
} else {
$result[$key] = $value;
}
$result[$key] = \is_iterable($value) ?
$this->on($value) :
$value;
}

return $result;
Expand Down
8 changes: 7 additions & 1 deletion src/Transformation/Implode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public function __construct(string $glue = '')
*/
public function on(iterable $collection): string
{
return \implode($this->glue, (new All())->on($collection));
$result = '';

foreach ($collection as $value) {
$result .= $value . $this->glue;
}

return \rtrim($result, $this->glue);
}
}
6 changes: 2 additions & 4 deletions src/Transformation/Reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ public function on(iterable $collection)
$callback = $this->callback;
$initial = $this->initial;

$carry = $initial;

foreach ($collection as $key => $value) {
$carry = $callback($carry, $value, $key);
$initial = $callback($initial, $value, $key);
}

return $carry;
return $initial;
}
}
24 changes: 6 additions & 18 deletions src/Transformation/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace drupol\collection\Transformation;

use drupol\collection\Contract\Operation as OperationInterface;
use drupol\collection\Contract\Operation;
use drupol\collection\Contract\Transformation;
use drupol\collection\Iterator\ClosureIterator;

Expand All @@ -23,7 +23,7 @@ final class Run implements Transformation
*
* @param \drupol\collection\Contract\Operation ...$operations
*/
public function __construct(OperationInterface ...$operations)
public function __construct(Operation ...$operations)
{
$this->operations = $operations;
}
Expand All @@ -33,22 +33,10 @@ public function __construct(OperationInterface ...$operations)
*/
public function on(iterable $collection)
{
return \array_reduce($this->operations, [$this, 'doRun'], $collection);
}
$callback = static function (iterable $collection, Operation $operation) {
return new ClosureIterator($operation->on($collection));
};

/**
* Run an operation on the collection.
*
* @param iterable $collection
* The collection.
* @param \drupol\collection\Contract\Operation $operation
* The operation.
*
* @return mixed
* The operation result.
*/
private function doRun(iterable $collection, OperationInterface $operation)
{
return new ClosureIterator($operation->on($collection));
return (new Reduce($callback, $collection))->on($this->operations);
}
}
19 changes: 4 additions & 15 deletions src/Transformation/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@ public function __construct(Transformation ...$transformers)
*/
public function on(iterable $collection)
{
return \array_reduce($this->transformers, [$this, 'doRun'], $collection);
}
$callback = static function (iterable $collection, Transformation $transformer) {
return $transformer->on($collection);
};

/**
* Run an operation on the collection.
*
* @param iterable $collection
* The collection.
* @param \drupol\collection\Contract\Transformation $transformer
*
* @return mixed
* The operation result.
*/
private function doRun(iterable $collection, Transformation $transformer)
{
return $transformer->on($collection);
return (new Reduce($callback, $collection))->on($this->transformers);
}
}

0 comments on commit ec8ee0e

Please sign in to comment.