Skip to content

Commit

Permalink
refactor: Update various operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 15, 2020
1 parent 0127f40 commit b5da182
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 110 deletions.
17 changes: 10 additions & 7 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ final class Collection implements CollectionInterface
private array $parameters;

/**
* @psalm-var callable(...mixed): Generator<TKey, T>
* @psalm-var callable(...mixed): (Generator<TKey, T>|Iterator<TKey, T>)
*/
private Closure $source;

/**
* @param callable|Closure $callable
* @psalm-param callable(...mixed): Generator<TKey, T> $callable
* @psalm-param callable(...mixed): Iterator<TKey, T> $callable
*
* @param mixed ...$parameters
* @psalm-param mixed ...$parameters
Expand Down Expand Up @@ -240,7 +240,7 @@ public function contains(...$value): CollectionInterface

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

public function current(int $index = 0)
Expand Down Expand Up @@ -360,7 +360,7 @@ public static function fromCallable(callable $callable, ...$parameters): self
* @psalm-param callable(T...) $callable
* @psalm-param list<T> $parameters
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (callable $callable, array $parameters): Iterator => new IteratorIterator($callable(...$parameters)),
$callable,
Expand All @@ -371,6 +371,9 @@ public static function fromCallable(callable $callable, ...$parameters): self
public static function fromFile(string $filepath): self
{
return new self(
/**
* @psalm-return Iterator<int, string>
*/
static fn (string $filepath): Iterator => new ResourceIterator(fopen($filepath, 'rb')),
$filepath
);
Expand All @@ -382,7 +385,7 @@ public static function fromIterable(iterable $iterable): self
/**
* @psalm-param iterable<TKey, T> $iterable
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (iterable $iterable): Iterator => new IterableIterator($iterable),
$iterable
Expand All @@ -396,7 +399,7 @@ public static function fromResource($resource): self
* @param mixed $resource
* @psalm-param resource $resource
*
* @psalm-return Generator<int, string>
* @psalm-return Iterator<int, string>
*/
static fn ($resource): Iterator => new ResourceIterator($resource),
$resource
Expand All @@ -407,7 +410,7 @@ public static function fromString(string $string, string $delimiter = ''): self
{
return new self(
/**
* @psalm-return Generator<int, string>
* @psalm-return Iterator<int, string>
*/
static fn (string $string, string $delimiter): Iterator => new StringIterator($string, $delimiter),
$string,
Expand Down
3 changes: 1 addition & 2 deletions src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection\Iterator;

use Iterator;
use IteratorIterator;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

Expand All @@ -14,7 +13,7 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends IteratorIterator<TKey, T>
* @extends ProxyIterator<TKey, T>
*/
final class CacheIterator extends ProxyIterator
{
Expand Down
3 changes: 1 addition & 2 deletions src/Iterator/ProxyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace loophp\collection\Iterator;

use Generator;
use Iterator;
use OuterIterator;

Expand All @@ -17,7 +16,7 @@
abstract class ProxyIterator implements OuterIterator
{
/**
* @psalm-var Generator<TKey, T>|Iterator<TKey, T>
* @psalm-var Iterator<TKey, T>
*/
protected Iterator $iterator;

Expand Down
2 changes: 2 additions & 0 deletions src/Iterator/RandomIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T of string
*
* @implements Iterator<TKey, T>
*/
final class RandomIterator implements Iterator, OuterIterator
{
Expand Down
2 changes: 1 addition & 1 deletion src/Iterator/ResourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @extends ProxyIterator<int, string>
*/
final class ResourceIterator extends ProxyIterator
{
Expand Down
38 changes: 21 additions & 17 deletions src/Iterator/StringIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@
* @psalm-template TKey of array-key
* @psalm-template T of string
*
* @extends ProxyIterator<TKey, T>
* @extends ProxyIterator<int, string>
*/
final class StringIterator extends ProxyIterator
{
public function __construct(string $data, string $delimiter = '')
{
$callback = static function (string $input, string $delimiter): Generator {
$offset = 0;

while (
mb_strlen($input) > $offset
&& false !== $nextOffset = '' !== $delimiter ? mb_strpos($input, $delimiter, $offset) : 1 + $offset
) {
yield (string) mb_substr($input, $offset, $nextOffset - $offset);

$offset = $nextOffset + mb_strlen($delimiter);
}

if ('' !== $delimiter) {
yield (string) mb_substr($input, $offset);
}
};
$callback =
/**
* @psalm-return Generator<int, string>
*/
static function (string $input, string $delimiter): Generator {
$offset = 0;

while (
mb_strlen($input) > $offset
&& false !== $nextOffset = '' !== $delimiter ? mb_strpos($input, $delimiter, $offset) : 1 + $offset
) {
yield (string) mb_substr($input, $offset, $nextOffset - $offset);

$offset = $nextOffset + mb_strlen($delimiter);
}

if ('' !== $delimiter) {
yield (string) mb_substr($input, $offset);
}
};

$this->iterator = new IteratorIterator($callback($data, $delimiter));

Expand Down
7 changes: 3 additions & 4 deletions src/Operation/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\CacheIterator;
use Psr\Cache\CacheItemPoolInterface;
Expand All @@ -20,19 +19,19 @@
final class Cache extends AbstractOperation
{
/**
* @psalm-return Closure(CacheItemPoolInterface): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(CacheItemPoolInterface): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (CacheItemPoolInterface $cache): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (Iterator $iterator): Iterator => new CacheIterator($iterator, $cache);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Combinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static function (Iterator $iterator) use ($length, $getCombinations): Generator
$dataset = [...$iterator];

if (0 < $length) {
return yield from $getCombinations($dataset, (int) $length);
return yield from $getCombinations($dataset, $length);
}

$collectionSize = count($dataset);
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use InfiniteIterator;
use Iterator;

Expand All @@ -17,15 +16,15 @@
final class Cycle extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (Iterator $iterator): Iterator => new InfiniteIterator($iterator);
}
Expand Down
18 changes: 12 additions & 6 deletions src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public function __invoke(): Closure
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static fn (int ...$offsets): Closure => static function (Iterator $iterator) use ($offsets): Generator {
if (!$iterator->valid()) {
return yield from [];
}
static fn (int ...$offsets): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
if (!$iterator->valid()) {
return yield from [];
}

return yield from new LimitIterator($iterator, array_sum($offsets));
};
return yield from new LimitIterator($iterator, array_sum($offsets));
};
}
}
25 changes: 21 additions & 4 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,32 @@
final class Flip extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<T, TKey>
* @psalm-return Closure(Iterator<TKey, T>): Closure(Iterator<T, TKey>): Generator<T, TKey>
*/
public function __invoke(): Closure
{
$callbackForKeys = static fn ($carry, $key, $value) => $value;
$callbackForValues = static fn ($carry, $key, $value) => $key;
$callbackForKeys =
/**
* @psalm-param mixed $carry
* @psalm-param TKey $key
* @psalm-param T $value
*
* @psalm-return T
*/
static fn ($carry, $key, $value) => $value;

$callbackForValues =
/**
* @psalm-param mixed $carry
* @psalm-param TKey $key
* @psalm-param T $value
*
* @psalm-return TKey
*/
static fn ($carry, $key, $value) => $key;

/**
* @var Closure(Iterator<TKey, T>): Generator<T, TKey>
* @psalm-var Closure(Iterator<TKey, T>): Generator<T, TKey>
*/
$associate = Associate::of()($callbackForKeys)($callbackForValues);

Expand Down
14 changes: 7 additions & 7 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ static function (Iterator $iterator): Generator {
*/
static fn ($value): bool => $value === $current;

for (; $iterator->valid(); $span->next(), $iterator = $span->current()) {
$key = $iterator->key();
$current = $iterator->current();

do {
/** @psalm-var Iterator<int, Iterator<TKey, T>> $span */
$span = Span::of()($spanCallback($current))($iterator);
$span = Span::of()($spanCallback($iterator->current()))($iterator);

yield $iterator->key() => [...$span->current()];

yield $key => [...$span->current()];
}
$span->next();
$iterator = $span->current();
} while ($iterator->valid());
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/Inits.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __invoke(): Closure
* @param mixed $value
* @param mixed $key
*/
static function (array $carry, $value, $key, Iterator $iterator): array {
static function (array $carry, $value, $key): array {
$carry[$key] = $value;

return $carry;
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
final class Limit extends AbstractOperation
{
/**
* @psalm-return Closure(int = default):Closure (int=): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(int): Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(int=): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static fn (int $count = -1): Closure =>
/**
Expand Down
6 changes: 5 additions & 1 deletion src/Operation/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ final class Lines extends AbstractOperation
*/
public function __invoke(): Closure
{
$mapCallback = static fn (array $value): string => implode('', $value);
$mapCallback =
/**
* @psalm-param T $value
*/
static fn (array $value): string => implode('', $value);

/** @psalm-var Closure(Iterator<TKey, (T | string)>):Generator<int, string> $pipe */
$pipe = Pipe::of()(
Expand Down
30 changes: 15 additions & 15 deletions src/Operation/Nth.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ public function __invoke(): Closure
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static fn (int $offset): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($step, $offset): Generator {
$position = 0;
static function (int $offset) use ($step): Closure {
$filterCallback =
/**
* @psalm-param array{0: TKey, 1: T} $value
*/
static fn ($value, int $key): bool => (($key % $step) === $offset);

foreach ($iterator as $key => $value) {
if ($position++ % $step !== $offset) {
continue;
}
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Pack::of(),
Filter::of()($filterCallback),
Unpack::of()
);

yield $key => $value;
}
};
// Point free style.
return $pipe;
};
}
}

0 comments on commit b5da182

Please sign in to comment.