Skip to content

Commit

Permalink
refactor: Update Since operation.
Browse files Browse the repository at this point in the history
Variadic callbacks are evaluated as logical OR.
If AND is needed, multiple calls to since needs to be made.

BREAKING CHANGE: yes
  • Loading branch information
drupol committed Jan 3, 2021
1 parent 1e36e85 commit 0420906
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
30 changes: 30 additions & 0 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1538,12 +1538,17 @@ since

Skip items until callback is met.

.. warning:: The callback parameter is variadic and they are evaluated as a logical ``OR``.
If you're looking for a logical ``AND``, you have make multiple calls to the
since operations.

Interface: `Sinceable`_

Signature: ``Collection::since(callable ...$callbacks);``

.. code-block:: php
// Example 1
// Parse the composer.json of a package and get the require-dev dependencies.
$collection = Collection::fromResource(fopen(__DIR__ . '/composer.json', 'rb'))
// Group items when EOL character is found.
Expand Down Expand Up @@ -1598,6 +1603,31 @@ Signature: ``Collection::since(callable ...$callbacks);``
print_r($collection);
// Example 2
$input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3];
$isGreaterThanThree = static function (int $value): bool {
return 3 < $value;
};
$isGreaterThanEight = static function (int $value): bool {
return 8 < $value;
};
$collection = Collection::fromIterable($input)
->since(
$isGreaterThanThree,
$isGreaterThanEight
); // [4, 5, 6, 7, 8, 9, 1, 2, 3]
$collection = Collection::fromIterable($input)
->since(
$isGreaterThanThree
)
->since(
$isGreaterThanEight
); // [9, 1, 2, 3]
slice
~~~~~

Expand Down
34 changes: 29 additions & 5 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2137,15 +2137,17 @@ public function it_can_shuffle(): void

public function it_can_since(): void
{
$this::fromIterable(range('a', 'z'))
$input = range('a', 'z');

$this::fromIterable($input)
->since(
static function ($letter) {
return 'x' === $letter;
}
)
->shouldIterateAs([23 => 'x', 24 => 'y', 25 => 'z']);

$this::fromIterable(range('a', 'z'))
$this::fromIterable($input)
->since(
static function ($letter) {
return 'x' === $letter;
Expand All @@ -2154,9 +2156,9 @@ static function ($letter) {
return 1 === mb_strlen($letter);
}
)
->shouldIterateAs([23 => 'x', 24 => 'y', 25 => 'z']);
->shouldIterateAs($input);

$this::fromIterable(range('a', 'z'))
$this::fromIterable($input)
->since(
static function ($letter) {
return 'foo' === $letter;
Expand All @@ -2165,7 +2167,29 @@ static function ($letter) {
return 'x' === $letter;
}
)
->shouldIterateAs([]);
->shouldIterateAs([23 => 'x', 24 => 'y', 25 => 'z']);

$isGreaterThanThree = static function (int $value): bool {
return 3 < $value;
};

$isGreaterThanFive = static function (int $value): bool {
return 5 < $value;
};

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

public function it_can_slice(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Since.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ 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 : false;
static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) || $carry;

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

if (false !== $result) {
Expand Down

0 comments on commit 0420906

Please sign in to comment.