Skip to content

Commit

Permalink
refactor: Leverage tacit programming.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Oct 10, 2020
1 parent 7ea007d commit aa4df6e
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ first time till the end of the list.

Interface: `DropWhileable`_

Signature: ``Collection::dropWhile(callable $callback);``
Signature: ``Collection::dropWhile(callable ...$callbacks);``

.. code-block:: php
Expand Down
14 changes: 9 additions & 5 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,16 @@ public function it_can_drop(): void

public function it_can_dropWhile(): void
{
$isSmallerThanThree = static function ($value) {
$isSmallerThanThree = static function (int $value): bool {
return 3 > $value;
};

$isGreaterThanFive = static function (int $value): bool {
return 5 > $value;
};

$this::fromIterable([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3])
->dropWhile($isSmallerThanThree)
->dropWhile($isGreaterThanFive, $isSmallerThanThree)
->shouldIterateAs([
2 => 3,
3 => 4,
Expand Down Expand Up @@ -1876,11 +1880,11 @@ public function it_can_scanright1(): void
$result = static function () {
yield 0 => 8;

yield 0 => 1;
yield 1 => 1;

yield 1 => 12;
yield 2 => 12;

yield 2 => 2;
yield 0 => 2;
};

$this::fromIterable([8, 12, 24, 2])
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ public function drop(int ...$counts): CollectionInterface
return $this->pipe(Drop::of()(...$counts));
}

public function dropWhile(callable $callback): CollectionInterface
public function dropWhile(callable ...$callbacks): CollectionInterface
{
return $this->pipe(DropWhile::of()($callback));
return $this->pipe(DropWhile::of()(...$callbacks));
}

public function duplicate(): CollectionInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Operation/DropWhileable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface DropWhileable
/**
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function dropWhile(callable $callback): Collection;
public function dropWhile(callable ...$callbacks): Collection;
}
16 changes: 13 additions & 3 deletions src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@ public function __invoke(): Closure
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable $callback): Closure {
static function (callable ...$callbacks): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback): Generator {
static function (Iterator $iterator) use ($callbacks): Generator {
for (; $iterator->valid(); $iterator->next()) {
if (true === $callback($iterator->current(), $iterator->key(), $iterator)) {
$reduced = array_reduce(
$callbacks,
static function (bool $carry, callable $callback) use ($iterator): bool {
return ($callback($iterator->current(), $iterator->key(), $iterator)) ?
$carry :
false;
},
true
);

if (true === $reduced) {
continue;
}

Expand Down
1 change: 0 additions & 1 deletion src/Operation/FoldRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __invoke(): Closure
static function (callable $callback): Closure {
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reverse::of(),
ScanRight1::of()($callback),
Head::of()
);
Expand Down
18 changes: 7 additions & 11 deletions src/Operation/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, TKey>
*/
static function (int $index): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, TKey>
*/
static function (Iterator $iterator) use ($index): Generator {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, TKey> $pipe */
$pipe = Pipe::of()(
Limit::of()(1)($index),
Flip::of()
);

return yield $iterator->key();
};
// Point free style.
return $pipe;
};
}
}
17 changes: 7 additions & 10 deletions src/Operation/ScanLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@ static function (callable $callback): Closure {
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
static function ($initial = null) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, T|null>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
yield $initial;
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reduction::of()($callback)($initial),
Prepend::of()($initial)
);

return yield from Reduction::of()($callback)($initial)($iterator);
};
// Point free style.
return $pipe;
};
};
}
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ static function (callable $callback): Closure {
static function (Iterator $iterator) use ($callback): Generator {
$initial = $iterator->current();

yield $initial;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Tail::of(),
Reduction::of()($callback)($initial)
Reduction::of()($callback)($initial),
Prepend::of()($initial)
);

return yield from $pipe($iterator);
Expand Down
26 changes: 9 additions & 17 deletions src/Operation/ScanRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,16 @@ static function (callable $callback): Closure {
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
static function ($initial = null) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, T|null>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Reverse::of(),
Reduction::of()($callback)($initial),
Reverse::of()
);
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reverse::of(),
Reduction::of()($callback)($initial),
Reverse::of(),
Append::of()($initial)
);

yield from $pipe($iterator);

return yield $initial;
};
// Point free style.
return $pipe;
};
};
}
Expand Down
26 changes: 8 additions & 18 deletions src/Operation/ScanRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,15 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
static function (callable $callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, T|null>
*/
static function (Iterator $iterator) use ($callback): Generator {
$initial = $iterator->current();
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Reverse::of(),
ScanLeft1::of()($callback),
Reverse::of()
);

yield $initial;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
Init::of(),
Reduction::of()($callback)($initial)
);

return yield from $pipe($iterator);
};
// Point free style.
return $pipe;
};
}
}

0 comments on commit aa4df6e

Please sign in to comment.