Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Update every operation. #242

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"require": {
"php": ">= 7.4",
"loophp/iterators": "^1.5"
"loophp/iterators": "^1.6"
},
"require-dev": {
"amphp/parallel-functions": "^1",
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ parameters:
path: src/Operation/GroupBy.php

-
message: "#^Parameter \\#1 \\.\\.\\.\\$ of closure expects callable\\(mixed, mixed, iterable\\)\\: bool, Closure\\(mixed, mixed, CachingIterator\\)\\: bool given\\.$#"
message: "#^Parameter \\#1 \\.\\.\\.\\$ of closure expects callable\\(mixed, mixed, iterable\\)\\: bool, Closure\\(mixed, mixed, loophp\\\\iterators\\\\CachingIteratorAggregate\\)\\: bool given\\.$#"
count: 1
path: src/Operation/Init.php

Expand Down
4 changes: 3 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
use loophp\collection\Operation\Words;
use loophp\collection\Operation\Wrap;
use loophp\collection\Operation\Zip;
use loophp\collection\Utils\CallbacksArrayReducer;
use loophp\iterators\ClosureIteratorAggregate;
use loophp\iterators\IterableIteratorAggregate;
use loophp\iterators\ResourceIteratorAggregate;
Expand Down Expand Up @@ -360,7 +361,8 @@ public function equals(iterable $other): bool

public function every(callable ...$callbacks): bool
{
return (new Every())()(static fn (): bool => false)(...$callbacks)($this)->current();
return (new Every())()(static fn (int $index, $value, $key, iterable $iterable) => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))(static fn (bool $r): bool => $r)($this)
->current();
AlexandruGG marked this conversation as resolved.
Show resolved Hide resolved
}

public function explode(...$explodes): CollectionInterface
Expand Down
26 changes: 13 additions & 13 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Closure;
use Generator;
use loophp\collection\Utils\CallbacksArrayReducer;
use loophp\iterators\CachingIteratorAggregate;
use loophp\iterators\IterableIteratorAggregate;

/**
* @immutable
Expand Down Expand Up @@ -41,22 +43,20 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (iterable $iterable) use ($callbacks): Generator {
$skip = false;
$callback = CallbacksArrayReducer::or()($callbacks);
$iteratorAggregate = (new CachingIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator()));

foreach ($iterable as $key => $current) {
if (false === $skip) {
if (false === $callback($current, $key, $iterable)) {
$skip = true;
$every = (new Every())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);

yield $key => $current;
}
$offset = (true === $current = $every->current()) ? 0 : $current;

continue;
}

yield $key => $current;
}
yield from (new Limit())()(-1)($offset)($iteratorAggregate);
};
}
}
6 changes: 3 additions & 3 deletions src/Operation/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static function (iterable $other): Closure {
* @return Generator<int|TKey, bool>
*/
return static function (iterable $iterable) use ($other): Generator {
$otherAggregate = (new IterableIteratorAggregate($other));
$otherAggregate = new IterableIteratorAggregate($other);
$iteratorAggregate = new IterableIteratorAggregate($iterable);

$iterator = $iteratorAggregate->getIterator();
Expand All @@ -60,9 +60,9 @@ static function (iterable $other): Closure {
/**
* @param T $current
*/
static fn ($current): bool => (new Contains())()($current)($otherAggregate)->current();
static fn (int $index, $current): bool => (new Contains())()($current)($otherAggregate)->current();

yield from (new Every())()(static fn (): bool => false)($containsCallback)($iteratorAggregate);
yield from (new Every())()($containsCallback)(static fn (bool $i): bool => $i)($iteratorAggregate);
};
};
}
Expand Down
47 changes: 22 additions & 25 deletions src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,41 @@
final class Every extends AbstractOperation
{
/**
* @return Closure(callable(T, TKey, iterable<TKey, T>...): bool): Closure(callable(T, TKey, iterable<TKey, T>...): bool): Closure(iterable<TKey, T>): Generator<TKey, bool>
* @return Closure(callable(int=, T=, TKey=, iterable<TKey, T>=): bool): Closure(callable(bool, int, T, TKey, iterable<TKey, T>...): mixed): Closure(iterable<TKey, T>): Generator<int, mixed>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T=, TKey=, iterable<TKey, T>=): bool ...$matchers
* @param callable(int=, T=, TKey=, iterable<TKey, T>=): bool $predicate
*
* @return Closure(...callable(T=, TKey=, iterable<TKey, T>=): bool): Closure(iterable<TKey, T>): Generator<TKey, bool>
* @return Closure(callable(bool, int, T=, TKey=, iterable<TKey, T>=): mixed): Closure(iterable<TKey, T>): Generator<int, mixed>
*/
static function (callable ...$matchers): Closure {
static function (callable ...$predicates): Closure {
return
/**
* @param callable(T=, TKey=, iterable<TKey, T>=): bool ...$callbacks
* @param callable(bool, int, T=, TKey=, iterable<TKey, T>=): mixed $return
*
* @return Closure(iterable<TKey, T>): Generator<TKey, bool>
* @return Closure(iterable<TKey, T>): Generator<int, mixed>
*/
static function (callable ...$callbacks) use ($matchers): Closure {
$callback = CallbacksArrayReducer::or()($callbacks);
$matcher = CallbacksArrayReducer::or()($matchers);
static function (callable $return) use ($predicates): Closure {
return
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<int, mixed>
*/
static function (iterable $iterable) use ($return, $predicates): Generator {
$predicate = CallbacksArrayReducer::or()($predicates);

/** @var Closure(iterable<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = (new Pipe())()(
(new Map())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn ($value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) !== $matcher($value, $key, $iterable)
),
(new DropWhile())()(static fn (bool $value): bool => $value),
(new Append())()(true),
(new Head())(),
);
foreach ((new Pack())()($iterable) as $index => [$key, $value]) {
if (false === $predicate($index, $value, $key, $iterable)) {
return yield $return(false, $index, $value, $key, $iterable);
}
}

// Point free style.
return $pipe;
return yield true;
};
};
};
}
Expand Down
26 changes: 7 additions & 19 deletions src/Operation/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace loophp\collection\Operation;

use CachingIterator;
use Closure;
use Generator;
use loophp\iterators\IterableIteratorAggregate;
use loophp\iterators\CachingIteratorAggregate;

/**
* @immutable
Expand All @@ -30,25 +29,14 @@ final class Init extends AbstractOperation
*/
public function __invoke(): Closure
{
$buildCachingIterator =
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $takeWhile */
$takeWhile = (new TakeWhile())()(
/**
* @param iterable<TKey, T> $iterable
*
* @return CachingIterator<TKey, T>
* @param T $value
* @param TKey $key
* @param CachingIteratorAggregate<TKey, T> $iterator
*/
static fn (iterable $iterator): CachingIterator => new CachingIterator((new IterableIteratorAggregate($iterator))->getIterator(), CachingIterator::FULL_CACHE);

/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $takeWhile */
$takeWhile = (new Pipe())()(
$buildCachingIterator,
(new TakeWhile())()(
/**
* @param T $value
* @param TKey $key
* @param CachingIterator<TKey, T> $iterator
*/
static fn ($value, $key, CachingIterator $iterator): bool => $iterator->hasNext()
)
static fn ($value, $key, CachingIteratorAggregate $iterator): bool => $iterator->hasNext()
);

// Point free style.
Expand Down
14 changes: 8 additions & 6 deletions src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ static function (callable ...$callbacks) use ($matchers): Closure {

/** @var Closure(iterable<TKey, T>): Generator<TKey, bool> $pipe */
$pipe = (new Pipe())()(
(new Map())()(
(new Every())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn ($value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) === $matcher($value, $key, $iterable)
static fn (int $index, $value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) !== $matcher($value, $key, $iterable)
)(
static fn (bool $i): bool => $i
),
(new DropWhile())()(static fn (bool $value): bool => !$value),
(new Append())()(false),
(new Head())()
(new Head())(),
(new Map())()(
static fn (bool $i): bool => !$i
)
);

// Point free style.
Expand Down
21 changes: 14 additions & 7 deletions src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Closure;
use Generator;
use loophp\collection\Utils\CallbacksArrayReducer;
use loophp\iterators\CachingIteratorAggregate;
use loophp\iterators\IterableIteratorAggregate;

/**
* @immutable
Expand Down Expand Up @@ -41,15 +43,20 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (iterable $iterable) use ($callbacks): Generator {
$callback = CallbacksArrayReducer::or()($callbacks);
$iteratorAggregate = (new CachingIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator()));

foreach ($iterable as $key => $current) {
if (false === $callback($current, $key, $iterable)) {
break;
}
$every = (new Every())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);

yield $key => $current;
}
$size = (true === $current = $every->current()) ? -1 : $current;

yield from (new Limit())()($size)(0)($iteratorAggregate);
};
}
}
21 changes: 14 additions & 7 deletions src/Operation/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Closure;
use Generator;
use loophp\collection\Utils\CallbacksArrayReducer;
use loophp\iterators\CachingIteratorAggregate;
use loophp\iterators\IterableIteratorAggregate;

/**
* @immutable
Expand Down Expand Up @@ -41,15 +43,20 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (iterable $iterable) use ($callbacks): Generator {
$callback = CallbacksArrayReducer::or()($callbacks);
$iteratorAggregate = (new CachingIteratorAggregate((new IterableIteratorAggregate($iterable))->getIterator()));

foreach ($iterable as $key => $current) {
yield $key => $current;
$every = (new Every())()(
/**
* @param T $value
* @param TKey $key
* @param iterable<TKey, T> $iterable
*/
static fn (int $index, $value, $key, iterable $iterable): bool => !CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable)
)(static fn (bool $r, int $index): int => $index)($iteratorAggregate);

if ($callback($current, $key, $iterable)) {
break;
}
}
$size = (true === $current = $every->current()) ? -1 : $current + 1;

yield from (new Limit())()($size)(0)($iteratorAggregate);
};
}
}