mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2025-01-22 05:11:47 +01:00
[Iter] split into multiple functions (#84)
This commit is contained in:
parent
1bebacb3ce
commit
2336e5624e
@ -43,7 +43,7 @@ function pipe(callable ...$stages): callable
|
||||
*
|
||||
* @psalm-return IO
|
||||
*/
|
||||
static fn ($input, int $key, callable $next) => $next($input),
|
||||
static fn ($input, callable $next) => $next($input),
|
||||
$input
|
||||
);
|
||||
}
|
||||
|
@ -143,6 +143,8 @@ final class Loader
|
||||
'Psl\Iter\random',
|
||||
'Psl\Iter\range',
|
||||
'Psl\Iter\reduce',
|
||||
'Psl\Iter\reduce_keys',
|
||||
'Psl\Iter\reduce_with_keys',
|
||||
'Psl\Iter\reductions',
|
||||
'Psl\Iter\reindex',
|
||||
'Psl\Iter\repeat',
|
||||
|
@ -23,17 +23,17 @@ namespace Psl\Iter;
|
||||
* @psalm-template Tv
|
||||
* @psalm-template Ts
|
||||
*
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
* @psalm-param (callable(?Ts, Tk, Tv): Ts) $function
|
||||
* @psalm-param Ts|null $initial
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
* @psalm-param (callable(?Ts, Tv): Ts) $function
|
||||
* @psalm-param Ts|null $initial
|
||||
*
|
||||
* @psalm-return Ts|null
|
||||
*/
|
||||
function reduce(iterable $iterable, callable $function, $initial = null)
|
||||
{
|
||||
$accumulator = $initial;
|
||||
foreach ($iterable as $k => $v) {
|
||||
$accumulator = $function($accumulator, $k, $v);
|
||||
foreach ($iterable as $v) {
|
||||
$accumulator = $function($accumulator, $v);
|
||||
}
|
||||
|
||||
return $accumulator;
|
||||
|
41
src/Psl/Iter/reduce_keys.php
Normal file
41
src/Psl/Iter/reduce_keys.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Iter;
|
||||
|
||||
/**
|
||||
* Reduce iterable keys using a function.
|
||||
*
|
||||
* The reduction function is passed an accumulator value and the current
|
||||
* iterator value and returns a new accumulator. The accumulator is initialized
|
||||
* to $initial.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* Iter\reduce_keys(
|
||||
* Iter\range(1, 5),
|
||||
* static fn(int $accumulator, int $key): int => $accumulator + $key,
|
||||
* 0,
|
||||
* )
|
||||
* => 10
|
||||
*
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
* @psalm-template Ts
|
||||
*
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
* @psalm-param (callable(?Ts, Tk): Ts) $function
|
||||
* @psalm-param Ts|null $initial
|
||||
*
|
||||
* @psalm-return Ts|null
|
||||
*/
|
||||
function reduce_keys(iterable $iterable, callable $function, $initial = null)
|
||||
{
|
||||
$accumulator = $initial;
|
||||
foreach ($iterable as $k => $v) {
|
||||
$accumulator = $function($accumulator, $k);
|
||||
}
|
||||
|
||||
return $accumulator;
|
||||
}
|
48
src/Psl/Iter/reduce_with_keys.php
Normal file
48
src/Psl/Iter/reduce_with_keys.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Iter;
|
||||
|
||||
/**
|
||||
* Reduce iterable with both values and keys using a function.
|
||||
*
|
||||
* The reduction function is passed an accumulator value and the current
|
||||
* iterator value and returns a new accumulator. The accumulator is initialized
|
||||
* to $initial.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* Iter\reduce_with_keys(
|
||||
* Iter\range(1, 5),
|
||||
* static fn(int $accumulator, int $key, int $value): int => $accumulator + $value,
|
||||
* 0,
|
||||
* )
|
||||
* => 15
|
||||
*
|
||||
* Iter\reduce_with_keys(
|
||||
* Iter\range(1, 5),
|
||||
* static fn(int $accumulator, int $key, int $value): int => $accumulator * $value,
|
||||
* 0,
|
||||
* )
|
||||
* => 120
|
||||
*
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
* @psalm-template Ts
|
||||
*
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
* @psalm-param (callable(?Ts, Tk, Tv): Ts) $function
|
||||
* @psalm-param Ts|null $initial
|
||||
*
|
||||
* @psalm-return Ts|null
|
||||
*/
|
||||
function reduce_with_keys(iterable $iterable, callable $function, $initial = null)
|
||||
{
|
||||
$accumulator = $initial;
|
||||
foreach ($iterable as $k => $v) {
|
||||
$accumulator = $function($accumulator, $k, $v);
|
||||
}
|
||||
|
||||
return $accumulator;
|
||||
}
|
26
tests/Psl/Iter/ReduceKeysTest.php
Normal file
26
tests/Psl/Iter/ReduceKeysTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
final class ReduceKeysTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testReduceKeys($expected, iterable $iterable, callable $function, $initial = null): void
|
||||
{
|
||||
self::assertSame($expected, Iter\reduce_keys($iterable, $function, $initial));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [null, [], fn ($accumulator, $k) => $accumulator, null];
|
||||
yield [3, [1, 2, 3], fn ($accumulator, $k) => $accumulator + $k, 0];
|
||||
yield [3, Iter\to_iterator([1, 2, 3]), fn ($accumulator, $k) => $accumulator + $k, 0];
|
||||
}
|
||||
}
|
@ -19,8 +19,8 @@ class ReduceTest extends TestCase
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [null, [], fn ($accumulator, $k, $v) => $accumulator, null];
|
||||
yield [6, [1, 2, 3], fn ($accumulator, $k, $v) => $accumulator + $v, 0];
|
||||
yield [6, Iter\to_iterator([1, 2, 3]), fn ($accumulator, $k, $v) => $accumulator + $v, 0];
|
||||
yield [null, [], fn ($accumulator, $v) => $accumulator, null];
|
||||
yield [6, [1, 2, 3], fn ($accumulator, $v) => $accumulator + $v, 0];
|
||||
yield [6, Iter\to_iterator([1, 2, 3]), fn ($accumulator, $v) => $accumulator + $v, 0];
|
||||
}
|
||||
}
|
||||
|
26
tests/Psl/Iter/ReduceWithKeysTest.php
Normal file
26
tests/Psl/Iter/ReduceWithKeysTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
class ReduceWithKeysTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testReduceWithKeys($expected, iterable $iterable, callable $function, $initial = null): void
|
||||
{
|
||||
self::assertSame($expected, Iter\reduce_with_keys($iterable, $function, $initial));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [null, [], fn ($accumulator, $k, $v) => $accumulator, null];
|
||||
yield [6, [1, 2, 3], fn ($accumulator, $k, $v) => $accumulator + $v, 0];
|
||||
yield [6, Iter\to_iterator([1, 2, 3]), fn ($accumulator, $k, $v) => $accumulator + $v, 0];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user