Skip to content

Commit

Permalink
docs: Update annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 12, 2020
1 parent ff6c556 commit 6cb1e77
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 80 deletions.
9 changes: 5 additions & 4 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;

/**
Expand All @@ -15,24 +16,24 @@
final class Current extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): T
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<int, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param int $index
*
* @psalm-return Closure(Iterator<TKey, T>): T
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, T>
*/
static function (int $index): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return T
* @psalm-return Generator<int, T>
*/
static function (Iterator $iterator) use ($index) {
static function (Iterator $iterator) use ($index): Generator {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}

Expand Down
9 changes: 5 additions & 4 deletions src/Operation/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;

/**
Expand All @@ -15,24 +16,24 @@
final class Key extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): TKey
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<int, TKey>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param int $index
*
* @psalm-return Closure(Iterator<TKey, T>): TKey
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, TKey>
*/
static function (int $index): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return TKey
* @psalm-return Generator<int, TKey>
*/
static function (Iterator $iterator) use ($index) {
static function (Iterator $iterator) use ($index): Generator {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}

Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\IterableIterator;

use function count;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class Product extends AbstractOperation
{
Expand Down Expand Up @@ -43,8 +44,7 @@ static function (Iterator $iterator) use ($iterables): Generator {
*
* @psalm-return Generator<int, array<int, T>>
*/
static function (iterable ...$iterables) use (&$cartesian): Generator
{
static function (iterable ...$iterables) use (&$cartesian): Generator {
$iterable = array_pop($iterables);

if (null === $iterable) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __invoke(): Closure
static function (int $size): Closure {
return
/**
* @psalm-param Iterator<TKey, T>
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
Expand Down
45 changes: 36 additions & 9 deletions src/Operation/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,47 @@

use const INF;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Range extends AbstractOperation
{
/**
* @psalm-return Closure(float=): Closure(float=): Closure(float=): Closure(null|Iterator<TKey, T>): Generator<int, float>
*/
public function __invoke(): Closure
{
return static function (float $start = 0.0): Closure {
return static function (float $end = INF) use ($start): Closure {
return static function (float $step = 1.0) use ($start, $end): Closure {
return static function (Iterator $iterator) use ($start, $end, $step): Generator {
for ($current = $start; $current < $end; $current += $step) {
yield $current;
}
return
/**
* @psalm-return Closure(float=): Closure(float=): Closure(null|Iterator<TKey, T>): Generator<int, float>
*/
static function (float $start = 0.0): Closure {
return
/**
* @psalm-return Closure(float=): Closure(null|Iterator<TKey, T>): Generator<int, float>
*/
static function (float $end = INF) use ($start): Closure {
return
/**
* @psalm-return Closure(null|Iterator<TKey, T>): Generator<int, float>
*/
static function (float $step = 1.0) use ($start, $end): Closure {
return
/**
* @psalm-param null|Iterator<TKey, T> $iterator
* @psalm-return Generator<int, float>
*/
static function (?Iterator $iterator = null) use ($start, $end, $step): Generator {
for ($current = $start; $current < $end; $current += $step) {
yield $current;
}
};
};
};
};
};
};
}
}
3 changes: 1 addition & 2 deletions src/Operation/Reduce.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public function __invoke(): Closure
static function (callable $callback): Closure {
return
/**
* @param mixed|null $initial
* @psalm-param T|null $initial
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*
* @param mixed|null $initial
*/
static function ($initial = null) use ($callback): Closure {
return
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/Reverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
final class Reverse extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T, mixed, void>
*/
public function __invoke(): Closure
{
return
Expand Down
104 changes: 64 additions & 40 deletions src/Operation/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,78 @@ final class Scale extends AbstractOperation
*/
public function __invoke(): Closure
{
return static function (float $lowerBound): Closure {
return static function (float $upperBound) use ($lowerBound): Closure {
return static function (float $wantedLowerBound = 0.0) use ($lowerBound, $upperBound): Closure {
return static function (float $wantedUpperBound = 1.0) use ($lowerBound, $upperBound, $wantedLowerBound): Closure { // phpcs:ignore
return static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { // phpcs:ignore
return static function (Iterator $iterator) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): Generator { // phpcs:ignore
$wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; // phpcs:ignore
$wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; // phpcs:ignore

/** @psalm-var callable(Generator<TKey, float|int>): Generator<TKey, float> $mapper */
$mapper = Map::of()(
return
/**
* @psalm-return Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
static function (float $lowerBound): Closure {
return
/**
* @psalm-return Closure(float): Closure(float): Closure(float): Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
static function (float $upperBound) use ($lowerBound): Closure {
return
/**
* @psalm-return Closure(float): Closure(float): Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
static function (float $wantedLowerBound = 0.0) use ($lowerBound, $upperBound): Closure {
return
/**
* @param mixed $v
* @psalm-param float|int $v
* @psalm-return Closure(float): Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { // phpcs:ignore
$mx = 0.0 === $base ?
($v - $lowerBound) / ($upperBound - $lowerBound) :
log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base);
static function (float $wantedUpperBound = 1.0) use ($lowerBound, $upperBound, $wantedLowerBound): Closure { // phpcs:ignore
return
/**
* @psalm-return Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure { // phpcs:ignore
return
/**
* @psalm-return Generator<TKey, float|int>
*/
static function (Iterator $iterator) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): Generator { // phpcs:ignore
$wantedLowerBound = (0.0 === $wantedLowerBound) ? (0.0 === $base ? 0.0 : 1.0) : $wantedLowerBound; // phpcs:ignore
$wantedUpperBound = (1.0 === $wantedUpperBound) ? (0.0 === $base ? 1.0 : $base) : $wantedUpperBound; // phpcs:ignore

$mx = $mx === -INF ? 0 : $mx;
/** @psalm-var callable(Generator<TKey, float|int>): Generator<TKey, float> $mapper */
$mapper = Map::of()(
/**
* @param mixed $v
* @psalm-param float|int $v
*/
static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { // phpcs:ignore
$mx = 0.0 === $base ?
($v - $lowerBound) / ($upperBound - $lowerBound) :
log($v - $lowerBound, $base) / log($upperBound - $lowerBound, $base);

return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound);
}
);
$mx = $mx === -INF ? 0 : $mx;

/** @psalm-var callable(Iterator<TKey, float|int>): Generator<TKey, float|int> $filter */
$filter = Filter::of()(
/**
* @param float|int $item
*/
static function ($item) use ($lowerBound): bool {
return $item >= $lowerBound;
},
/**
* @param float|int $item
*/
static function ($item) use ($upperBound): bool {
return $item <= $upperBound;
}
);
return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound);
}
);

/** @psalm-var callable(Iterator<TKey, float|int>): Generator<TKey, float|int> $filter */
$filter = Filter::of()(
/**
* @param float|int $item
*/
static function ($item) use ($lowerBound): bool {
return $item >= $lowerBound;
},
/**
* @param float|int $item
*/
static function ($item) use ($upperBound): bool {
return $item <= $upperBound;
}
);

return yield from Compose::of()($filter, $mapper)($iterator);
return yield from Compose::of()($filter, $mapper)($iterator);
};
};
};
};
};
};
};
};
};
}
}
3 changes: 3 additions & 0 deletions src/Operation/Shuffle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
final class Shuffle extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T, mixed, void>
*/
public function __invoke(): Closure
{
return
Expand Down
48 changes: 34 additions & 14 deletions src/Operation/Times.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,47 @@
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class Times extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(null|callable(int): T): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
*/
public function __invoke(): Closure
{
return static function (int $number = 0): Closure {
return static function (?callable $callback = null) use ($number): Closure {
return static function (Iterator $iterator) use ($number, $callback): Generator {
if (1 > $number) {
throw new InvalidArgumentException('Invalid parameter. $number must be greater than 1.');
}
return
/**
* @psalm-return Closure(null|callable(int): int|T): Closure(null|Iterator<TKey, T>): Generator<int, int|T>
*/
static function (int $number = 0): Closure {
return
/**
* @psalm-return Closure(null|Iterator<TKey, T>): Generator<int, int|T>
*/
static function (?callable $callback = null) use ($number): Closure {
return
/**
* @psalm-param null|Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, int|T>
*/
static function (?Iterator $iterator = null) use ($number, $callback): Generator {
if (1 > $number) {
throw new InvalidArgumentException('Invalid parameter. $number must be greater than 1.');
}

$callback = $callback ?? static function (int $value): int {
return $value;
};
$callback = $callback ?? static function (int $value): int {
return $value;
};

for ($current = 1; $current <= $number; ++$current) {
yield $callback($current);
}
};
for ($current = 1; $current <= $number; ++$current) {
yield $callback($current);
}
};
};
};
};
}
}

0 comments on commit 6cb1e77

Please sign in to comment.