Skip to content

Commit

Permalink
Do not let Operations wrap the return of ::run() in a collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 22, 2019
1 parent 38d8f71 commit 8cfd4eb
Show file tree
Hide file tree
Showing 28 changed files with 236 additions and 273 deletions.
18 changes: 8 additions & 10 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ final class Append extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$items] = $this->parameters;

return $collection::with(
static function () use ($items, $collection): \Generator {
foreach ($collection as $item) {
yield $item;
}
return static function () use ($items, $collection): \Generator {
foreach ($collection as $item) {
yield $item;
}

foreach ($items as $item) {
yield $item;
}
foreach ($items as $item) {
yield $item;
}
);
};
}
}
26 changes: 13 additions & 13 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ public function __construct(int $size)
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$size] = $this->parameters;

if (0 >= $size) {
return $collection::with();
return static function () {
yield from [];
};
}

return $collection::with(
static function () use ($size, $collection): \Generator {
$iterator = $collection->getIterator();
return static function () use ($size, $collection): \Generator {
$iterator = $collection->getIterator();

while ($iterator->valid()) {
$values = [];
while ($iterator->valid()) {
$values = [];

for ($i = 0; $iterator->valid() && $i < $size; $i++, $iterator->next()) {
$values[$iterator->key()] = $iterator->current();
}

yield $collection::with($values);
for ($i = 0; $iterator->valid() && $i < $size; $i++, $iterator->next()) {
$values[$iterator->key()] = $iterator->current();
}

yield $collection::with($values);
}
);
};
}
}
16 changes: 7 additions & 9 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ final class Collapse extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
return $collection::with(
static function () use ($collection): \Generator {
foreach ($collection as $item) {
if (\is_array($item) || $item instanceof Collection) {
foreach ($item as $value) {
yield $value;
}
return static function () use ($collection): \Generator {
foreach ($collection as $item) {
if (\is_array($item) || $item instanceof Collection) {
foreach ($item as $value) {
yield $value;
}
}
}
);
};
}
}
22 changes: 10 additions & 12 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ final class Combine extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$keys] = $this->parameters;

return $collection::with(
static function () use ($keys, $collection): \Generator {
$original = $collection->getIterator();
$keysIterator = $collection::with($keys)->getIterator();
return static function () use ($keys, $collection): \Generator {
$original = $collection->getIterator();
$keysIterator = $collection::with($keys)->getIterator();

for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next()
for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next()
) {
yield $keysIterator->current() => $original->current();
}
yield $keysIterator->current() => $original->current();
}

if (($original->valid() && !$keysIterator->valid()) ||
if (($original->valid() && !$keysIterator->valid()) ||
(!$original->valid() && $keysIterator->valid())
) {
\trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING);
}
\trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING);
}
);
};
}
}
16 changes: 7 additions & 9 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Filter extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$callbacks] = $this->parameters;

Expand All @@ -24,16 +24,14 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac
};
}

return $collection::with(
static function () use ($callbacks, $collection): \Generator {
foreach ($callbacks as $callback) {
foreach ($collection as $key => $value) {
if (true === (bool) $callback($value, $key)) {
yield $key => $value;
}
return static function () use ($callbacks, $collection): \Generator {
foreach ($callbacks as $callback) {
foreach ($collection as $key => $value) {
if (true === (bool) $callback($value, $key)) {
yield $key => $value;
}
}
}
);
};
}
}
30 changes: 15 additions & 15 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ public function __construct(int $depth)
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$depth] = $this->parameters;

return $collection::with(
static function () use ($depth, $collection): \Generator {
foreach ($collection as $item) {
if (!\is_array($item) && !$item instanceof BaseCollectionInterface) {
yield $item;
} elseif (1 === $depth) {
foreach ($item as $i) {
yield $i;
}
} else {
foreach ((new Flatten($depth - 1))->run($collection::with($item)) as $flattenItem) {
yield $flattenItem;
}
return static function () use ($depth, $collection): \Generator {
foreach ($collection as $item) {
if (!\is_array($item) && !$item instanceof BaseCollectionInterface) {
yield $item;
} elseif (1 === $depth) {
foreach ($item as $i) {
yield $i;
}
} else {
$flatten = new Flatten($depth - 1);

foreach ($collection::with($flatten->run($collection::with($item))) as $flattenItem) {
yield $flattenItem;
}
}
}
);
};
}
}
12 changes: 5 additions & 7 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ final class Flip extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
return $collection::with(
static function () use ($collection): \Generator {
foreach ($collection as $key => $value) {
yield $value => $key;
}
return static function () use ($collection): \Generator {
foreach ($collection as $key => $value) {
yield $value => $key;
}
);
};
}
}
16 changes: 7 additions & 9 deletions src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ final class Forget extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$keys] = $this->parameters;

return $collection::with(
static function () use ($keys, $collection): \Generator {
$keys = \array_flip($keys);
return static function () use ($keys, $collection): \Generator {
$keys = \array_flip($keys);

foreach ($collection as $key => $value) {
if (!\array_key_exists($key, $keys)) {
yield $key => $value;
}
foreach ($collection as $key => $value) {
if (!\array_key_exists($key, $keys)) {
yield $key => $value;
}
}
);
};
}
}
18 changes: 8 additions & 10 deletions src/Operation/Intersperse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct($elementToIntersperse, int $atEvery = 1, int $startA
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$element, $every, $startAt] = $this->parameters;

Expand All @@ -42,16 +42,14 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac
throw new \InvalidArgumentException('The third parameter must be a positive integer.');
}

return $collection::with(
static function () use ($element, $every, $startAt, $collection): \Generator {
foreach ($collection as $key => $value) {
if (0 === $startAt++ % $every) {
yield $element;
}

yield $value;
return static function () use ($element, $every, $startAt, $collection): \Generator {
foreach ($collection as $key => $value) {
if (0 === $startAt++ % $every) {
yield $element;
}

yield $value;
}
);
};
}
}
12 changes: 5 additions & 7 deletions src/Operation/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ final class Keys extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
return $collection::with(
static function () use ($collection): \Generator {
foreach ($collection as $key => $value) {
yield $key;
}
return static function () use ($collection): \Generator {
foreach ($collection as $key => $value) {
yield $key;
}
);
};
}
}
16 changes: 9 additions & 7 deletions src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ final class Last extends Operation
*/
public function run(BaseCollectionInterface $collection)
{
return (
new Reduce(
static function ($carry, $item) {
return $item;
},
$collection->getIterator()->current()
))->run($collection);
$reduced =
new Reduce(
static function ($carry, $item) {
return $item;
},
$collection->getIterator()->current()
);

return $reduced->run($collection);
}
}
18 changes: 8 additions & 10 deletions src/Operation/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ public function __construct(int $limit)
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$limit] = $this->parameters;

return $collection::with(
static function () use ($limit, $collection): \Generator {
$i = 0;
return static function () use ($limit, $collection): \Generator {
$i = 0;

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

if (++$i === $limit) {
break;
}
if (++$i === $limit) {
break;
}
}
);
};
}
}
20 changes: 9 additions & 11 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ final class Merge extends Operation
/**
* {@inheritdoc}
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
public function run(BaseCollectionInterface $collection): \Closure
{
[$sources] = $this->parameters;

return $collection::with(
static function () use ($sources, $collection): \Generator {
foreach ($collection as $item) {
yield $item;
}
return static function () use ($sources, $collection): \Generator {
foreach ($collection as $item) {
yield $item;
}

foreach ($sources as $source) {
foreach ($source as $item) {
yield $item;
}
foreach ($sources as $source) {
foreach ($source as $item) {
yield $item;
}
}
);
};
}
}

0 comments on commit 8cfd4eb

Please sign in to comment.