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

Provide util class for reducer callback #131

Merged
merged 7 commits into from
Jul 9, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions spec/loophp/collection/Utils/CallbacksArrayReducerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\loophp\collection\Utils;

use ArrayIterator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;
use PhpSpec\ObjectBehavior;

class CallbacksArrayReducerSpec extends ObjectBehavior
{
public function it_ensure_callbacks_receive_the_needed_arguments(): void
{
$callbacks = [
static fn (string $value, string $key, Iterator $iterator): bool => 'value_key_a_b_c' === sprintf('%s_%s_%s', $value, $key, implode('_', iterator_to_array($iterator))),
];

$iterator = new ArrayIterator(range('a', 'c'));

$this::or()($callbacks, 'value', 'key', $iterator)
->shouldReturn(true);
}

public function it_is_initializable()
{
$this->shouldHaveType(CallbacksArrayReducer::class);
}

public function it_reduces_empty_callbacks_array(): void
{
$iterator = new ArrayIterator(range(0, 10));

$this::or()([], 0, 0, $iterator)
->shouldReturn(false);
}

public function it_reduces_multiple_callbacks(): void
{
$callbacks = [
static fn (int $v): bool => 5 < $v,
static fn (int $v): bool => 0 === $v % 2,
];

$iterator = new ArrayIterator(range(0, 10));

$this::or()($callbacks, 0, 0, $iterator)
->shouldReturn(true);

$this::or()($callbacks, 13, 0, $iterator)
->shouldReturn(true);

$this::or()($callbacks, 3, 0, $iterator)
->shouldReturn(false);
}

public function it_reduces_single_callback(): void
{
$callbacks = [
static fn (int $v): bool => 5 < $v,
];

$iterator = new ArrayIterator(range(0, 10));

$this::or()($callbacks, 0, 0, $iterator)
->shouldReturn(false);

$this::or()($callbacks, 6, 0, $iterator)
->shouldReturn(true);
}
}
32 changes: 2 additions & 30 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -43,40 +44,11 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
$reducerCallback =
/**
* @param TKey $key
*
* @return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($key): Closure =>
/**
* @param T $current
*
* @return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($current): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (Iterator $iterator): Closure =>
/**
* @param bool $carry
* @param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);

$result = true;

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

if (true === $result) {
Expand Down
9 changes: 3 additions & 6 deletions src/Operation/Every.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -52,15 +53,11 @@ static function (callable ...$callbacks) use ($matchers): Closure {
*/
static fn (array $callbacks): Closure =>
/**
* @param T $value
* @param T $current
* @param TKey $key
* @param Iterator<TKey, T> $iterator
*/
static fn ($value, $key, Iterator $iterator): bool => array_reduce(
$callbacks,
static fn (bool $carry, callable $callback): bool => $carry || $callback($value, $key, $iterator),
false
);
static fn ($current, $key, Iterator $iterator): bool => CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator);

$mapCallback =
/**
Expand Down
33 changes: 2 additions & 31 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -43,32 +44,6 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
// TODO: Find a way to avoid repeating this everywhere.
$reducerCallback =
/**
* @param TKey $key
*
* @return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($key): Closure =>
/**
* @param T $current
*
* @return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($current): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (Iterator $iterator): Closure =>
/**
* @param bool $carry
* @param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);

$defaultCallback =
/**
* @param T $value
Expand All @@ -80,11 +55,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
$callbacks;

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

if (true === $result) {
yield $key => $current;
Expand Down
9 changes: 3 additions & 6 deletions src/Operation/MatchOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -52,15 +53,11 @@ static function (callable ...$callbacks) use ($matchers): Closure {
*/
static fn (array $callbacks): Closure =>
/**
* @param T $value
* @param T $current
* @param TKey $key
* @param Iterator<TKey, T> $iterator
*/
static fn ($value, $key, Iterator $iterator): bool => array_reduce(
$callbacks,
static fn (bool $carry, callable $callback): bool => $carry || $callback($value, $key, $iterator),
false
);
static fn ($current, $key, Iterator $iterator): bool => CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator);

$mapCallback =
/**
Expand Down
33 changes: 2 additions & 31 deletions src/Operation/Reject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -43,32 +44,6 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
// TODO: Find a way to avoid repeating this everywhere.
$reducerCallback =
/**
* @param TKey $key
*
* @return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($key): Closure =>
/**
* @param T $current
*
* @return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($current): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (Iterator $iterator): Closure =>
/**
* @param bool $carry
* @param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);

$defaultCallback =
/**
* @param T $value
Expand All @@ -80,11 +55,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
$callbacks;

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

if (false === $result) {
yield $key => $current;
Expand Down
32 changes: 2 additions & 30 deletions src/Operation/Since.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -43,40 +44,11 @@ public function __invoke(): Closure
* @return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
$reducerCallback =
/**
* @param TKey $key
*
* @return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($key): Closure =>
/**
* @param T $current
*
* @return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($current): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (Iterator $iterator): Closure =>
/**
* @param bool $carry
* @param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);

$result = false;

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

if (false === $result) {
Expand Down
38 changes: 5 additions & 33 deletions src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Generator;
use Iterator;
use loophp\collection\Contract\Operation\Splitable;
use loophp\collection\Utils\CallbacksArrayReducer;

/**
* @immutable
Expand Down Expand Up @@ -50,40 +51,11 @@ public function __invoke(): Closure
static function (Iterator $iterator) use ($type, $callbacks): Generator {
$carry = [];

$reducerCallback =
/**
* @param TKey $key
*
* @return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($key): Closure =>
/**
* @param T $current
*
* @return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn ($current): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (Iterator $iterator): Closure =>
/**
* @param bool $carry
* @param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => $carry || $callable($current, $key, $iterator);

foreach ($iterator as $key => $value) {
$callbackReturn = array_reduce(
$callbacks,
$reducerCallback($key)($value)($iterator),
false
);
foreach ($iterator as $key => $current) {
$callbackReturn = CallbacksArrayReducer::or()($callbacks, $current, $key, $iterator);

if (Splitable::AFTER === $type) {
$carry[] = $value;
$carry[] = $current;
}

if (Splitable::REMOVE === $type && true === $callbackReturn) {
Expand All @@ -101,7 +73,7 @@ static function (Iterator $iterator) use ($type, $callbacks): Generator {
}

if (Splitable::AFTER !== $type) {
$carry[] = $value;
$carry[] = $current;
}
}

Expand Down
Loading