Skip to content

Commit

Permalink
Remove unneeded ArrayIterators and use simple arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 26, 2019
1 parent 97769de commit 01d5ae2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
12 changes: 0 additions & 12 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace spec\drupol\collection;

use ArrayIterator;
use ArrayObject;
use Closure;
use drupol\collection\Collection;
Expand Down Expand Up @@ -357,11 +356,6 @@ public function it_can_explode(): void

$this
->explode('o')
->map(
static function ($item) {
return iterator_to_array($item);
}
)
->shouldIterateAs(
[
0 => [
Expand Down Expand Up @@ -1071,18 +1065,12 @@ public function it_can_split(): void
->split(static function ($value) {
return 0 === $value % 3;
})
->map(static function (ArrayIterator $item) {
return iterator_to_array($item);
})
->shouldIterateAs([0 => [1, 2, 3], 1 => [4, 5, 6], 2 => [7, 8, 9], 3 => [10, 11, 12], 4 => [13, 14, 15], 5 => [16, 17]]);

$this::with(range(1, 15))
->split(static function ($value) {
return 0 === $value % 3;
})
->map(static function (ArrayIterator $item) {
return iterator_to_array($item);
})
->shouldIterateAs([0 => [1, 2, 3], 1 => [4, 5, 6], 2 => [7, 8, 9], 3 => [10, 11, 12], 4 => [13, 14, 15]]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Operation/Apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use drupol\collection\Contract\Operation;
use Generator;

/**
* Class Apply.
Expand Down Expand Up @@ -34,7 +35,7 @@ public function on(iterable $collection): Closure
{
$callbacks = $this->callbacks;

return static function () use ($callbacks, $collection): iterable {
return static function () use ($callbacks, $collection): Generator {
foreach ($callbacks as $callback) {
foreach ($collection as $key => $value) {
if (false === $callback($value, $key)) {
Expand Down
11 changes: 6 additions & 5 deletions src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace drupol\collection\Operation;

use ArrayIterator;
use Closure;
use drupol\collection\Contract\Operation;
use Generator;

use function count;

/**
* Class Split.
*/
Expand Down Expand Up @@ -37,10 +38,10 @@ public function on(iterable $collection): Closure
$callbacks = $this->callbacks;

return static function () use ($callbacks, $collection): Generator {
$carry = new ArrayIterator();
$carry = [];

foreach ($collection as $key => $value) {
$carry->append($value);
$carry[] = $value;

foreach ($callbacks as $callback) {
if (true !== $callback($value, $key)) {
Expand All @@ -49,11 +50,11 @@ public function on(iterable $collection): Closure

yield $carry;

$carry = new ArrayIterator();
$carry = [];
}
}

if (0 !== $carry->count()) {
if (0 !== count($carry)) {
yield $carry;
}
};
Expand Down
8 changes: 3 additions & 5 deletions src/Transformation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace drupol\collection\Transformation;

use drupol\collection\Contract\Transformation;
use drupol\collection\Iterator\ClosureIterator;
use Generator;

/**
* Class Last.
Expand All @@ -20,12 +18,12 @@ final class Last implements Transformation
*/
public function on(iterable $collection)
{
$value = null;
$return = null;

foreach ($collection as $key => $value) {

$return = $value;
}

return $value;
return $return;
}
}

0 comments on commit 01d5ae2

Please sign in to comment.