Skip to content

Commit

Permalink
Convert Operations and Transformations into function object.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 28, 2020
1 parent 7daf209 commit 6ce8ecf
Show file tree
Hide file tree
Showing 73 changed files with 958 additions and 1,619 deletions.
159 changes: 79 additions & 80 deletions src/Collection.php

Large diffs are not rendered by default.

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

/**
* @return array<string, mixed>
*/
public function getArguments(): array;
}
5 changes: 1 addition & 4 deletions src/Contract/Transformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace loophp\collection\Contract;

use Iterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -14,11 +12,10 @@
interface Transformation
{
/**
* @param iterable<mixed> $collection
* @psalm-param \Iterator<TKey, T> $collection
*
* @return mixed
* @psalm-return T|scalar|\Iterator<TKey, T>|array<TKey, T>|null
*/
public function __invoke(Iterator $collection);
public function __invoke();
}
4 changes: 1 addition & 3 deletions src/Contract/Transformation/Runable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace loophp\collection\Contract\Transformation;

use loophp\collection\Contract\Operation;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
Expand All @@ -16,5 +14,5 @@ interface Runable
/**
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function run(Operation ...$operations);
public function run(callable ...$operations);
}
10 changes: 1 addition & 9 deletions src/Contract/Transformation/Transformable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@

namespace loophp\collection\Contract\Transformation;

use loophp\collection\Contract\Transformation;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Transformable
{
/**
* @param \loophp\collection\Contract\Transformation ...$transformers
* @psalm-param \loophp\collection\Contract\Transformation<TKey, T> ...$transformers
*
* @return \loophp\collection\Iterator\ClosureIterator|mixed
*/
public function transform(Transformation ...$transformers);
public function transform(callable ...$transformers);
}
20 changes: 3 additions & 17 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,10 @@
*/
final class Append extends AbstractOperation implements Operation
{
/**
* @param mixed ...$items
* @psalm-param T ...$items
*/
public function __construct(...$items)
{
$this->storage['items'] = $items;
}

public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param list<T> $items
*
* @psalm-return Generator<TKey|int, T>
*/
static function (Iterator $iterator, array $items): Generator {
return static function (...$items): Closure {
return static function (Iterator $iterator) use ($items): Generator {
foreach ($iterator as $key => $value) {
yield $key => $value;
}
Expand All @@ -43,5 +28,6 @@ static function (Iterator $iterator, array $items): Generator {
yield $key => $item;
}
};
};
}
}
36 changes: 16 additions & 20 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@
*/
final class Apply extends AbstractOperation implements Operation
{
/**
* @param callable ...$callbacks
* @psalm-param callable(T, TKey):(bool) ...$callbacks
*/
public function __construct(callable ...$callbacks)
{
$this->storage['callbacks'] = $callbacks;
}

/**
* @psalm-return Closure(Iterator<TKey, T>, list<callable(T, TKey):(bool)>):(Generator<TKey, T>)
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param list<callable(T, TKey):(bool)> $callbacks
*/
static function (Iterator $iterator, array $callbacks): Generator {
foreach ($iterator as $key => $value) {
foreach ($callbacks as $callback) {
if (true === $callback($value, $key)) {
continue;
}
static function (callable ...$callbacks): Closure {
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($callbacks): Generator {
foreach ($iterator as $key => $value) {
foreach ($callbacks as $callback) {
if (true === $callback($value, $key)) {
continue;
}

break;
}
break;
}

yield $key => $value;
}
yield $key => $value;
}
};
};
}
}
46 changes: 24 additions & 22 deletions src/Operation/Associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,39 @@
*/
final class Associate extends AbstractOperation implements Operation
{
/**
* @psalm-param null|callable(TKey, T):(TKey) $callbackForKeys
* @psalm-param null|callable(TKey, T):(T) $callbackForValues
*/
public function __construct(?callable $callbackForKeys = null, ?callable $callbackForValues = null)
{
$this->storage = [
'callbackForKeys' => $callbackForKeys ?? static function ($key, $value) {
return $key;
},
'callbackForValues' => $callbackForValues ?? static function ($key, $value) {
return $value;
},
];
}

/**
* @psalm-return Closure(Iterator<TKey, T>, callable(TKey, T):(TKey), callable(TKey, T):(T)):(Generator<TKey, T>)
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param callable(TKey, T):(TKey) $callbackForKeys
* @psalm-param callable(TKey, T):(T) $callbackForValues
*/
static function (Iterator $iterator, callable $callbackForKeys, callable $callbackForValues): Generator {
foreach ($iterator as $key => $value) {
yield $callbackForKeys($key, $value) => $callbackForValues($key, $value);
}
static function (?callable $callbackForKeys = null): Closure {
$callbackForKeys = $callbackForKeys ?? static function ($key, $value) {
return $key;
};

return
/**
* @psalm-param callable(TKey, T):(T) $callbackForValues
*/
static function (?callable $callbackForValues = null) use ($callbackForKeys): Closure {
$callbackForValues = $callbackForValues ?? static function ($key, $value) {
return $value;
};

return
/**
* @psalm-param \Iterator<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues): Generator {
foreach ($iterator as $key => $value) {
yield $callbackForKeys($key, $value) => $callbackForValues($key, $value);
}
};
};
};
}
}
25 changes: 11 additions & 14 deletions src/Operation/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
*/
final class Cache extends AbstractOperation implements Operation
{
public function __construct(CacheItemPoolInterface $cache)
{
$this->storage['cache'] = $cache;
}

public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator, CacheItemPoolInterface $cache): Generator {
return yield from new CacheIterator($iterator, $cache);
};
return static function (CacheItemPoolInterface $cache): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($cache): Generator {
return yield from new CacheIterator($iterator, $cache);
};
};
}
}
52 changes: 26 additions & 26 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,47 @@
*/
final class Chunk extends AbstractOperation implements Operation
{
public function __construct(int ...$sizes)
{
$this->storage['sizes'] = new ArrayIterator($sizes);
}

/**
* @psalm-return Closure(Iterator<TKey, T>, list<int>):(Generator<int, list<T>>)
* @psalm-return Closure(int...): Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param ArrayIterator<int, int> $sizes
*
* @psalm-return Generator<int, list<T>>
* @psalm-param int ...$sizes
*/
static function (Iterator $iterator, ArrayIterator $sizes): Generator {
$sizesIterator = (new Run(new Loop()))($sizes);
static function (int ...$sizes): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($sizes): Generator {
$sizesIterator = (new Run())()((new Loop())())(new ArrayIterator($sizes));

$values = [];
$values = [];

foreach ($iterator as $value) {
if (0 >= $sizesIterator->current()) {
return yield from [];
}
foreach ($iterator as $value) {
if (0 >= $sizesIterator->current()) {
return yield from [];
}

if (count($values) !== $sizesIterator->current()) {
$values[] = $value;
if (count($values) !== $sizesIterator->current()) {
$values[] = $value;

continue;
}
continue;
}

$sizesIterator->next();
$sizesIterator->next();

yield $values;
yield $values;

$values = [$value];
}
$values = [$value];
}

return yield $values;
return yield $values;
};
};
}
}
24 changes: 4 additions & 20 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,23 @@
*/
final class Column extends AbstractOperation implements Operation
{
/**
* @param int|string $column
* @psalm-param array-key $column
*/
public function __construct($column)
{
$this->storage['column'] = $column;
}

/**
* @psalm-return Closure(Iterator<TKey, T>, array-key):(Generator<int, iterable<TKey, T>>)
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param array-key $column
*
* @psalm-return Generator<int, iterable<TKey, T>>
*
* @param mixed $column
*/
static function (Iterator $iterator, $column): Generator {
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) {
foreach ((new Run())()((new Transpose())())($iterator) as $key => $value) {
if ($key === $column) {
return yield from $value;
}
}
};
};
}
}

0 comments on commit 6ce8ecf

Please sign in to comment.