Skip to content

Commit

Permalink
Proposal: Update Scan* operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 17, 2021
1 parent 70e3fe1 commit 56ffc34
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/Contract/Operation/ScanLeft1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;
use Iterator;

/**
* @template TKey
Expand All @@ -18,7 +19,9 @@
interface ScanLeft1able
{
/**
* @return Collection<TKey, T>
* @param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return Collection<int|TKey, T>
*/
public function scanLeft1(callable $callback): Collection;
}
8 changes: 6 additions & 2 deletions src/Contract/Operation/ScanLeftable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;
use Iterator;

/**
* @template TKey
Expand All @@ -18,9 +19,12 @@
interface ScanLeftable
{
/**
* @param T|null $initial
* @template V
*
* @return Collection<TKey, T>
* @param callable(V, T, TKey, Iterator<TKey, T>): V $callback
* @param V $initial
*
* @return Collection<int|TKey, V|T>
*/
public function scanLeft(callable $callback, $initial = null): Collection;
}
4 changes: 3 additions & 1 deletion src/Contract/Operation/ScanRight1able.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
interface ScanRight1able
{
/**
* @return Collection<TKey, T>
* @param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return Collection<int|TKey, T>
*/
public function scanRight1(callable $callback): Collection;
}
8 changes: 6 additions & 2 deletions src/Contract/Operation/ScanRightable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Collection;
use Iterator;

/**
* @template TKey
Expand All @@ -18,9 +19,12 @@
interface ScanRightable
{
/**
* @param T|null $initial
* @template V
*
* @return Collection<TKey, T>
* @param callable(V, T, TKey, Iterator<TKey, T>): V $callback
* @param V $initial
*
* @return Collection<int|TKey, V|T>
*/
public function scanRight(callable $callback, $initial = null): Collection;
}
16 changes: 9 additions & 7 deletions src/Operation/ScanLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ final class ScanLeft extends AbstractOperation
/**
* @pure
*
* @return Closure(callable((T|null), T, TKey, Iterator<TKey, T>): (T|null)):Closure (T|null): Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @template V
*
* @return Closure(callable(V, T, TKey, Iterator<TKey, T>): V|T): Closure (V): Closure(Iterator<TKey, T>): Generator<TKey, V|T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
* @param callable(V, T, TKey, Iterator<TKey, T>): V|T $callback
*
* @return Closure(T|null): Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<int|TKey, V|T>
*/
static fn (callable $callback): Closure =>
/**
* @param T|null $initial
* @param V $initial
*
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, V|T>
*/
static function ($initial = null) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
static function ($initial) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<int|TKey, V|T> $pipe */
$pipe = Pipe::of()(
Reduction::of()($callback)($initial),
Prepend::of()($initial)
Expand Down
10 changes: 5 additions & 5 deletions src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ final class ScanLeft1 extends AbstractOperation
/**
* @pure
*
* @return Closure(callable((T|null), T, TKey, Iterator<TKey, T>): (T|null)):Closure (Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(callable(T, T, TKey, Iterator<TKey, T>): T):Closure (Iterator<TKey, T>): Generator<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
* @param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
static fn (callable $callback): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<int|TKey, T|null>
* @return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($callback): Iterator {
$initial = $iterator->current();

/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, T|null>) $pipe */
/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, T>) $pipe */
$pipe = Pipe::of()(
Tail::of(),
Reduction::of()($callback)($initial),
Expand Down
16 changes: 9 additions & 7 deletions src/Operation/ScanRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ final class ScanRight extends AbstractOperation
/**
* @pure
*
* @return Closure(callable((T|null), T, TKey, Iterator<TKey, T>): (T|null)):Closure (T|null): Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @template V
*
* @return Closure(callable(V, T, TKey, Iterator<TKey, T>): V): Closure(V): Closure(Iterator<TKey, T>): Generator<int|TKey, V|T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
* @param callable(V, T, TKey, Iterator<TKey, T>): V $callback
*
* @return Closure(T|null): Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<int|TKey, V|T>
*/
static fn (callable $callback): Closure =>
/**
* @param T|null $initial
* @param V $initial
*
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, V|T>
*/
static function ($initial = null) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, T|null>) $pipe */
static function ($initial) use ($callback): Closure {
/** @var Closure(Iterator<TKey, T>):(Generator<int|TKey, V|T>) $pipe */
$pipe = Pipe::of()(
Reverse::of(),
Reduction::of()($callback)($initial),
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/ScanRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final class ScanRight1 extends AbstractOperation
/**
* @pure
*
* @return Closure(callable((T|null), T, TKey, Iterator<TKey, T>): (T|null)):Closure (Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(callable(T, T, TKey, Iterator<TKey, T>): T): Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
* @param callable(T, T, TKey, Iterator<TKey, T>): T $callback
*
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
* @return Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
static function (callable $callback): Closure {
/** @var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
Expand Down
41 changes: 41 additions & 0 deletions tests/static-analysis/scanleft.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

$sum = static fn (int $carry, int $value): int => $carry + $value;
$concat = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string);

/**
* @param CollectionInterface<int, int> $collection
*/
function scanleft_checkListInt(CollectionInterface $collection): void
{
}

/**
* @param CollectionInterface<int, string> $collection
*/
function scanleft_checkListString(CollectionInterface $collection): void
{
}

/**
* @param CollectionInterface<int, string|null> $collection
*/
function scanleft_checkListStringWithNull(CollectionInterface $collection): void
{
}

scanleft_checkListInt(Collection::fromIterable([1, 2, 3])->scanLeft($sum, 5));
scanleft_checkListString(Collection::fromIterable(range('a', 'c'))->scanLeft($concat, ''));
scanleft_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanLeft($concat));
25 changes: 25 additions & 0 deletions tests/static-analysis/scanleft1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

$concat = static fn (string $carry, string $string): string => sprintf('%s%s', $carry, $string);

/**
* @param CollectionInterface<int, string> $collection
*/
function scanleft1_checkListString(CollectionInterface $collection): void
{
}

// see Psalm bug: https://github.com/vimeo/psalm/issues/6108
scanleft1_checkListString(Collection::fromIterable(range('a', 'c'))->scanLeft1($concat));
41 changes: 41 additions & 0 deletions tests/static-analysis/scanright.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

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

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

$sum = static fn (int $carry, int $value): int => $carry + $value;
$concat = static fn (?string $carry, string $string): string => sprintf('%s%s', (string) $carry, $string);

/**
* @param CollectionInterface<int, int> $collection
*/
function scanright_checkListInt(CollectionInterface $collection): void
{
}

/**
* @param CollectionInterface<int, string> $collection
*/
function scanright_checkListString(CollectionInterface $collection): void
{
}

/**
* @param CollectionInterface<int, string|null> $collection
*/
function scanright_checkListStringWithNull(CollectionInterface $collection): void
{
}

scanright_checkListInt(Collection::fromIterable([1, 2, 3])->scanRight($sum, 5));
scanright_checkListString(Collection::fromIterable(range('a', 'c'))->scanRight($concat, ''));
scanright_checkListStringWithNull(Collection::fromIterable(range('a', 'c'))->scanRight($concat));
25 changes: 25 additions & 0 deletions tests/static-analysis/scanright1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

include __DIR__ . '/../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

$concat = static fn (string $carry, string $string): string => sprintf('%s%s', $carry, $string);

/**
* @param CollectionInterface<int, string> $collection
*/
function scanright1_checkListString(CollectionInterface $collection): void
{
}

// see Psalm bug: https://github.com/vimeo/psalm/issues/6108
scanright1_checkListString(Collection::fromIterable(range('a', 'c'))->scanRight1($concat));

0 comments on commit 56ffc34

Please sign in to comment.