Skip to content

Commit

Permalink
Syntactic sugar to reduce the amount of parenthesis.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 28, 2020
1 parent 6ce8ecf commit f40dc3b
Show file tree
Hide file tree
Showing 38 changed files with 320 additions and 281 deletions.
160 changes: 80 additions & 80 deletions src/Collection.php

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Contract/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
interface Operation
{
public function __invoke(): Closure;

public static function of(): Closure;
}
19 changes: 9 additions & 10 deletions src/Operation/AbstractOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

namespace loophp\collection\Operation;

abstract class AbstractOperation
use Closure;
use loophp\collection\Contract\Operation;

abstract class AbstractOperation implements Operation
{
/**
* @var array<string, mixed>
*/
protected $storage = [];
final public function __construct()
{
}

/**
* @return array<string, mixed>
*/
public function getArguments(): array
public static function of(): Closure
{
return $this->storage;
return (new static())->__invoke();
}
}
4 changes: 2 additions & 2 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

use function count;

Expand Down Expand Up @@ -37,7 +36,8 @@ static function (int ...$sizes): Closure {
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($sizes): Generator {
$sizesIterator = (new Run())()((new Loop())())(new ArrayIterator($sizes));
/** @var Iterator<int, int> $sizesIterator */
$sizesIterator = Loop::of()(new ArrayIterator($sizes));

$values = [];

Expand Down
37 changes: 24 additions & 13 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

/**
* @psalm-template TKey
Expand All @@ -22,18 +21,30 @@ final class Column extends AbstractOperation implements Operation
*/
public function __invoke(): Closure
{
return static function ($column): Closure {
return static function (Iterator $iterator) use ($column): Generator {
/**
* @psalm-var array-key $key
* @psalm-var iterable<TKey, T> $value
*/
foreach ((new Run())()((new Transpose())())($iterator) as $key => $value) {
if ($key === $column) {
return yield from $value;
}
}
return
/**
* @param int|string $column
*
* @psalm-param array-key $column
*/
static function ($column): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, iterable<TKey, T>>
*/
static function (Iterator $iterator) use ($column): Generator {
/**
* @psalm-var array-key $key
* @psalm-var iterable<TKey, T> $value
*/
foreach (Transpose::of()($iterator) as $key => $value) {
if ($key === $column) {
return yield from $value;
}
}
};
};
};
}
}
39 changes: 26 additions & 13 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

use function in_array;

Expand All @@ -19,23 +18,37 @@
*/
final class Compact extends AbstractOperation implements Operation
{
/**
* @psalm-return Closure(T...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return static function (...$values): Closure {
return static function (Iterator $iterator) use ($values): Generator {
$values = [] === $values ? [null] : $values;

$filter = (new Filter())()(
return
/**
* @psalm-param T ...$values
*/
static function (...$values): Closure {
return
/**
* @param mixed $item
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-return Generator<TKey, T>
*/
static function ($item) use ($values): bool {
return !in_array($item, $values, true);
}
);
static function (Iterator $iterator) use ($values): Generator {
$values = [] === $values ? [null] : $values;

/** @psalm-var callable(Iterator<TKey, T>):Generator<TKey, T> $filter */
$filter = Filter::of()(
/**
* @param mixed $value
* @psalm-param T $value
*/
static function ($value) use ($values): bool {
return !in_array($value, $values, true);
}
);

return yield from (new Run())()($filter)($iterator);
return yield from $filter($iterator);
};
};
};
}
}
53 changes: 30 additions & 23 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

/**
* @psalm-template TKey
Expand All @@ -19,30 +18,38 @@ final class Explode extends AbstractOperation implements Operation
{
public function __invoke(): Closure
{
return static function (...$explodes): Closure {
return static function (Iterator $iterator) use ($explodes): Generator {
$split = (new Split())()(
...array_map(
/**
* @param mixed $explode
* @psalm-param T $explode
*/
static function ($explode): Closure {
return
return
/**
* @psalm-param T ...$explodes
*/
static function (...$explodes): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($explodes): Generator {
return yield from Split::of()(
...array_map(
/**
* @param mixed $value
* @psalm-param T $value
* @param mixed $explode
* @psalm-param T $explode
*/
static function ($value) use ($explode): bool {
return $value === $explode;
};
},
$explodes
)
);

return yield from (new Run())()($split)($iterator);
static function ($explode): Closure {
return
/**
* @param mixed $value
* @psalm-param T $value
*/
static function ($value) use ($explode): bool {
return $value === $explode;
};
},
$explodes
)
)($iterator);
};
};
};
}
}
9 changes: 4 additions & 5 deletions src/Operation/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

/**
* @psalm-template TKey
Expand Down Expand Up @@ -44,10 +43,10 @@ static function ($value, $key, Iterator $iterator): bool {

$callback = $callback ?? $defaultCallback;

$filter = (new Filter())()($callback);
$limit = (new Limit())()($size)(0);

return yield from (new Run())()($filter, $limit)($iterator);
return yield from Compose::of()(
Filter::of()($callback),
Limit::of()($size)(0)
)($iterator);
};
};
};
Expand Down
41 changes: 25 additions & 16 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,37 @@
*/
final class Flatten extends AbstractOperation implements Operation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<int, T>
*/
public function __invoke(): Closure
{
return static function (int $depth): Closure {
return static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
} elseif (1 === $depth) {
/** @psalm-var T $subValue */
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
} else {
/** @psalm-var IterableIterator<TKey, T> $flatten */
$flatten = (new Flatten())()($depth - 1);
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, T>
*/
static function (Iterator $iterator) use ($depth): Generator {
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;
} elseif (1 === $depth) {
/** @psalm-var T $subValue */
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
} else {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $flatten */
$flatten = Flatten::of()($depth - 1);

foreach ((new Run())()($flatten)(new IterableIterator($value)) as $subKey => $subValue) {
yield $subKey => $subValue;
foreach ($flatten(new IterableIterator($value)) as $subKey => $subValue) {
yield $subKey => $subValue;
}
}
}
}
};
};
};
}
}
4 changes: 1 addition & 3 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\FoldLeft;
use loophp\collection\Transformation\Transform;

/**
* @psalm-template TKey
Expand Down Expand Up @@ -72,7 +70,7 @@ static function (array $collect, $value, $key) use ($callable): array {
return $collect;
};

return yield from (new Transform(new FoldLeft($callback, [])))($iterator);
return yield from (FoldLeft::of()($callback)([])($iterator))->current();
};
};
}
Expand Down
37 changes: 27 additions & 10 deletions src/Operation/Iterate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,35 @@
*/
final class Iterate extends AbstractOperation implements Operation
{
/**
* @psalm-return Closure(callable(T...):(array<TKey, T>)): Closure(T...): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return static function (callable $callback): Closure {
return static function (...$parameters) use ($callback): Closure {
return static function (Iterator $iterator) use ($callback, $parameters): Generator {
while (true) {
yield current(
$parameters = (array) $callback(...array_values((array) $parameters))
);
}
};
return
/**
* @psalm-param callable(T...):(array<TKey, T>) $callback
*/
static function (callable $callback): Closure {
return
/**
* @psalm-param T ...$parameters
*/
static function (...$parameters) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback, $parameters): Generator {
while (true) {
yield current(
$parameters = (array) $callback(...array_values((array) $parameters))
);
}
};
};
};
};
}
}
11 changes: 5 additions & 6 deletions src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

/**
* @psalm-template TKey
Expand Down Expand Up @@ -42,11 +41,11 @@ static function ($value, $key, Iterator $iterator): bool {

$callback = $callback ?? $defaultCallback;

$filter = (new Filter())()($callback);
$reverse = (new Reverse())();
$limit = (new Limit())()($size)(0);

return yield from (new Run())()($filter, $reverse, $limit)($iterator);
return yield from Compose::of()(
Filter::of()($callback),
Reverse::of(),
Limit::of()($size)(0)
)($iterator);
};
};
};
Expand Down

0 comments on commit f40dc3b

Please sign in to comment.