Skip to content

Commit

Permalink
refactor: Update Since operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 8, 2021
1 parent dbb9307 commit c8c3d9b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 17 additions & 2 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2177,8 +2177,10 @@ static function ($letter) {
return 5 < $value;
};

$this::fromIterable([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3])
->since($isGreaterThanFive, $isGreaterThanThree)
$input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3];

$this::fromIterable($input)
->since($isGreaterThanThree, $isGreaterThanFive)
->shouldIterateAs([
3 => 4,
4 => 5,
Expand All @@ -2190,6 +2192,19 @@ static function ($letter) {
10 => 2,
11 => 3,
]);

$this::fromIterable($input)
->since($isGreaterThanThree)
->since($isGreaterThanFive)
->shouldIterateAs([
5 => 6,
6 => 7,
7 => 8,
8 => 9,
9 => 1,
10 => 2,
11 => 3,
]);
}

public function it_can_slice(): void
Expand Down
24 changes: 13 additions & 11 deletions src/Operation/Since.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,24 @@ static function (Iterator $iterator) use ($callbacks): Generator {
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) || $carry;
static fn (bool $carry, callable $callable): bool => $carry || ($callable($current, $key, $iterator));

$result = false;

foreach ($iterator as $key => $current) {
$result = array_reduce(
$callbacks,
$reducerCallback($key)($current)($iterator),
false
);
if (false === $result) {
$result = array_reduce(
$callbacks,
$reducerCallback($key)($current)($iterator),
false
);
}

if (false !== $result) {
break;
if (false === $result) {
continue;
}
}

for (; $iterator->valid(); $iterator->next()) {
yield $iterator->key() => $iterator->current();
yield $key => $current;
}
};
}
Expand Down

0 comments on commit c8c3d9b

Please sign in to comment.