Skip to content

Commit

Permalink
Fix small things.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 26, 2020
1 parent c19e95d commit dc8d719
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public function intersperse($element, int $every = 1, int $startAt = 0): Collect

public static function iterate(callable $callback, ...$parameters): CollectionInterface
{
return (new self())->run(Iterate::of()($callback)($parameters));
return (new self())->run(Iterate::of()($callback)(...$parameters));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Operation/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ final class Compose extends AbstractOperation implements Operation
{
// phpcs:disable
/**
* @psalm-return Closure((callable(Iterator<TKey, T>):Generator)...): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure((callable(Iterator<TKey, T>):Generator<TKey, T>)...): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
// phpcs:enable
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(Iterator<TKey, T>):Generator<TKey, T> ...$operations
* @psalm-param callable(Iterator<TKey, T>): Generator<TKey, T> ...$operations
*/
static function (callable ...$operations): Closure {
return
Expand All @@ -45,9 +45,7 @@ static function (Iterator $iterator, callable $fn): Generator {
return $fn($iterator);
};

return yield from (
FoldLeft::of()($callback)($iterator)(new ArrayIterator($operations))
)->current();
return yield from array_reduce($operations, $callback, $iterator);
};
};
}
Expand Down
8 changes: 3 additions & 5 deletions src/Operation/Iterate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
final class Iterate extends AbstractOperation implements Operation
{
/**
* @psalm-return Closure(callable(T...):(array<array-key, T>)): Closure(T...): Generator<array-key, T>
* @psalm-return Closure(callable(T...):(array<TKey, T>)): Closure(T...): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T...):(array<array-key, T>) $callback
* @psalm-param callable(T...):(array<TKey, T>) $callback
*/
static function (callable $callback): Closure {
return
Expand All @@ -35,11 +35,9 @@ static function (...$parameters) use ($callback): Closure {
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<array-key, T>
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback, $parameters): Generator {
$parameters = current($parameters);

while (true) {
yield $parameters = $callback(...array_values((array) $parameters));
}
Expand Down
5 changes: 3 additions & 2 deletions src/Operation/Reverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function __invoke(): Closure
* @psalm-return \Generator<TKey, T, mixed, void>
*/
static function (Iterator $iterator): Generator {
/** @psalm-var array<int, array{0: TKey, 1: T}> $all */
$all = iterator_to_array(Pack::of()($iterator));
/** @psalm-var Generator<int, array{0: TKey, 1: T}> $pack */
$pack = Pack::of()($iterator);
$all = iterator_to_array($pack);

for (end($all); null !== key($all); prev($all)) {
$item = current($all);
Expand Down
21 changes: 13 additions & 8 deletions src/Operation/Scale.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,38 @@ final class Scale extends AbstractOperation implements Operation
{
// phpcs:disable
/**
* @psalm-return Closure(float): Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(float): Closure(float): Closure(float): Closure(float): Closure(float): Closure(Iterator<TKey, float|int>): Generator<TKey, float|int>
*/
// phpcs:enable
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 {
return static function (float $base = 0.0) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound): Closure {
return static function (Iterator $iterator) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): Generator {
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(Iterator<TKey, int|float|double>): Generator<TKey, float> $mapper */
/** @psalm-var callable(Generator<TKey, float|int>): Generator<TKey, float> $mapper */
$mapper = Map::of()(
static function ($v) use ($lowerBound, $upperBound, $wantedLowerBound, $wantedUpperBound, $base): float { // phpcs:ignore
/**
* @param mixed $value
* @psalm-param float|int|double $value
*/
static function ($value) 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);
($value - $lowerBound) / ($upperBound - $lowerBound) :
log($value - $lowerBound, $base) / log($upperBound - $lowerBound, $base);

$mx = $mx === -INF ? 0 : $mx;

return $wantedLowerBound + $mx * ($wantedUpperBound - $wantedLowerBound);
}
);

/** @psalm-var callable(Iterator<TKey, float|int>): Generator<TKey, float|int> $filter */
$filter = Filter::of()(
/**
* @param float|int $item
Expand Down

0 comments on commit dc8d719

Please sign in to comment.