Skip to content

Commit

Permalink
Merge pull request #58 from loophp/57-use-key-and-current-at-same-time
Browse files Browse the repository at this point in the history
Use key and current operation at the same time
  • Loading branch information
drupol authored Dec 30, 2020
2 parents 739e9c6 + a7b6a9c commit 318c2ea
Show file tree
Hide file tree
Showing 33 changed files with 149 additions and 170 deletions.
13 changes: 11 additions & 2 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@ Signature: ``Collection::every(callable $callback);``
->every($callback)
->current(); // true
Collection::fromIterable(range(0, 10))
->append(21)
->every($callback)
->current(); // false
Collection::fromIterable([])
->every($callback)
->current(); // true
explode
~~~~~~~

Expand Down Expand Up @@ -1434,8 +1443,8 @@ Signature: ``Collection::scanLeft(callable $callback, $initial = null);``
->normalize(); // [64 ,16 ,8 ,2]
Collection::empty()
->scanLeft($callback, 3)
->normalize(); // [3]
->scanLeft($callback, 3); // [0 => 3]
scanLeft1
~~~~~~~~~
Expand Down
32 changes: 30 additions & 2 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ public function it_can_every(): void

$this::empty()
->every($callback)
->shouldIterateAs([]);
->shouldIterateAs([true]);

$this::fromIterable(range(0, 10))
->every(
Expand Down Expand Up @@ -2028,7 +2028,7 @@ public function it_can_scanleft(): void

$this::fromIterable([])
->scanLeft($callback, 3)
->shouldIterateAs([]);
->shouldIterateAs([0 => 3]);
}

public function it_can_scanleft1(): void
Expand Down Expand Up @@ -3034,6 +3034,34 @@ public function it_can_zip(): void
->shouldIterateAs([[1, 'A'], [2, 'B'], [3, 'C'], [4, 'D'], [5, 'E']]);
}

/**
* @see https://github.com/loophp/collection/issues/57
*/
public function it_fix_bug_57()
{
$input = array_combine(range(1, 26), range('a', 'z'));

$collection = $this::fromIterable($input);

$collection
->key()
->shouldReturn(1);

$collection
->current()
->shouldReturn('a');

$last = $collection->last();

$last
->key()
->shouldReturn(26);

$last
->current()
->shouldReturn('z');
}

public function it_is_initializable(): void
{
$this->shouldHaveType(Collection::class);
Expand Down
3 changes: 1 addition & 2 deletions spec/loophp/collection/Operation/CurrentSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function it_can_get_current()

$this
->__invoke()(0)($iterator)
->current()
->shouldReturn('a');
->shouldIterateAs(['a']);

$this
->__invoke()(0)($iterator)
Expand Down
3 changes: 1 addition & 2 deletions spec/loophp/collection/Operation/LimitSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function it_can_set_an_offset()

$this
->__invoke()(1)(2)($iterator)
->current()
->shouldReturn('c');
->shouldIterateAs([2 => 'c']);

$this
->__invoke()(2)(2)($iterator)
Expand Down
9 changes: 3 additions & 6 deletions src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,16 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

foreach ($iterator as $key => $value) {
foreach ($callbacks as $callback) {
if (true === $callback($current, $key)) {
if (true === $callback($value, $key)) {
continue;
}

break;
}

yield $key => $current;
yield $key => $value;
}
};
}
Expand Down
9 changes: 3 additions & 6 deletions src/Operation/Associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ static function (Iterator $iterator) use ($callbackForKeys, $callbackForValues):
*/
static fn ($initial, callable $callback, int $callbackId, Iterator $iterator) => $callback($initial, $key, $value, $iterator);

for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

foreach ($iterator as $key => $value) {
/** @psalm-var Generator<int, TKey|T> $k */
$k = FoldLeft::of()($callbackFactory($key)($current))($key)(new ArrayIterator($callbackForKeys));
$k = FoldLeft::of()($callbackFactory($key)($value))($key)(new ArrayIterator($callbackForKeys));

/** @psalm-var Generator<int, T|TKey> $c */
$c = FoldLeft::of()($callbackFactory($key)($current))($current)(new ArrayIterator($callbackForValues));
$c = FoldLeft::of()($callbackFactory($key)($value))($value)(new ArrayIterator($callbackForValues));

yield $k->current() => $c->current();
}
Expand Down
8 changes: 3 additions & 5 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ static function (Iterator $iterator) use ($sizes): Generator {

$values = [];

for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

foreach ($iterator as $value) {
if (0 >= $sizesIterator->current()) {
return new EmptyIterator();
}

if (count($values) !== $sizesIterator->current()) {
$values[] = $current;
$values[] = $value;

continue;
}
Expand All @@ -58,7 +56,7 @@ static function (Iterator $iterator) use ($sizes): Generator {

yield $values;

$values = [$current];
$values = [$value];
}

return yield $values;
Expand Down
8 changes: 3 additions & 5 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

if (false === is_iterable($current)) {
foreach ($iterator as $value) {
if (false === is_iterable($value)) {
continue;
}

yield from $current;
yield from $value;
}
};
}
Expand Down
6 changes: 2 additions & 4 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ static function (Iterator $iterator) use ($values): Generator {
// yield count($values) === Intersect::of()(...$values)($iterator);
// But it would not be very optimal because it would have to traverse
// the whole iterator.
for (; $iterator->valid(); $iterator->next()) {
$current = $iterator->current();

foreach ($iterator as $value) {
foreach ($values as $k => $v) {
if ($v === $current) {
if ($v === $value) {
unset($values[$k]);
}

Expand Down
13 changes: 6 additions & 7 deletions src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Closure;
use EmptyIterator;
use Generator;
use Iterator;
use LimitIterator;

Expand All @@ -18,26 +17,26 @@
final class Drop extends AbstractOperation
{
/**
* @psalm-return Closure(int...): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(int...): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (int ...$offsets): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
if (!$iterator->valid()) {
static function (Iterator $iterator) use ($offsets): Iterator {
if (false === $iterator->valid()) {
return new EmptyIterator();
}

return yield from new LimitIterator($iterator, array_sum($offsets));
return new LimitIterator($iterator, array_sum($offsets));
};
}
}
2 changes: 1 addition & 1 deletion src/Operation/DropWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
* @psalm-param callable(T, TKey, Iterator<TKey, T>):bool $callback
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
Expand Down
11 changes: 4 additions & 7 deletions src/Operation/Duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@ public function __invoke(): Closure
static function (Iterator $iterator): Generator {
$stack = [];

for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (true === in_array($current, $stack, true)) {
yield $key => $current;
foreach ($iterator as $key => $value) {
if (true === in_array($value, $stack, true)) {
yield $key => $value;
}

$stack[] = $current;
$stack[] = $value;
}
};
}
Expand Down
11 changes: 5 additions & 6 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CallbackFilterIterator;
use Closure;
use Generator;
use Iterator;

/**
Expand All @@ -19,23 +18,23 @@
final class Filter extends AbstractOperation
{
/**
* @psalm-return Closure(callable(T , TKey , Iterator<TKey, T> ): bool ...):Closure (Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(callable(T , TKey , Iterator<TKey, T> ): bool ...): Closure (Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (callable ...$callbacks): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
static function (Iterator $iterator) use ($callbacks): Iterator {
$defaultCallback =
/**
* @param mixed $value
Expand All @@ -52,7 +51,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
[$defaultCallback] :
$callbacks;

return yield from array_reduce(
return array_reduce(
$callbacks,
static fn (Iterator $carry, callable $callback): CallbackFilterIterator => new CallbackFilterIterator($carry, $callback),
$iterator
Expand Down
13 changes: 5 additions & 8 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ public function __invoke(): Closure
* @psalm-param Iterator<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($depth): Generator {
for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (false === is_iterable($current)) {
yield $key => $current;
foreach ($iterator as $key => $value) {
if (false === is_iterable($value)) {
yield $key => $value;

continue;
}
Expand All @@ -44,10 +41,10 @@ static function (Iterator $iterator) use ($depth): Generator {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $flatten */
$flatten = Flatten::of()($depth - 1);

$current = $flatten(new IterableIterator($current));
$value = $flatten(new IterableIterator($value));
}

yield from $current;
yield from $value;
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
final class Flip extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<T, TKey>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Closure(Iterator<T, TKey>): Generator<T, TKey>
*/
public function __invoke(): Closure
{
Expand All @@ -40,7 +40,7 @@ public function __invoke(): Closure
*/
static fn ($carry, $key, $value) => $key;

/** @psalm-var Closure(Iterator<T, TKey>): Generator<TKey, T> $associate */
/** @psalm-var Closure(Iterator<TKey, T>): Generator<T, TKey> $associate */
$associate = Associate::of()($callbackForKeys)($callbackForValues);

// Point free style.
Expand Down
Loading

0 comments on commit 318c2ea

Please sign in to comment.