[Iter] split into multiple functions (#84)

This commit is contained in:
Toon Verwerft 2020-10-08 03:03:23 +02:00 committed by GitHub
parent 1bebacb3ce
commit 2336e5624e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 152 additions and 9 deletions

View File

@ -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
);
}

View File

@ -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',

View File

@ -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;

View 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;
}

View 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;
}

View 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];
}
}

View File

@ -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];
}
}

View 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];
}
}