Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add All Operation #133

Merged
merged 4 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,8 @@ Interface: `Allable`_

Signature: ``Collection::all();``

.. code-block:: php

$generator = static function (): Generator {
yield 0 => 'a';
yield 1 => 'b';
yield 0 => 'c';
yield 2 => 'd';
};

Collection::fromIterable($generator())
->all(); // [0 => 'c', 1 => 'b', 2 => 'd']
.. literalinclude:: code/operations/all.php
:language: php

append
~~~~~~
Expand Down
39 changes: 39 additions & 0 deletions docs/pages/code/operations/all.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App;

use ArrayIterator;
use Generator;
use loophp\collection\Collection;
use loophp\collection\Operation\All;
use loophp\collection\Operation\Filter;
use loophp\collection\Operation\Pipe;

include __DIR__ . '/../../../../vendor/autoload.php';

// Example 1 -> usage with Collection
$generator = static function (): Generator {
yield 0 => 'a';

yield 1 => 'b';

yield 0 => 'c';

yield 2 => 'd';
};

$collection = Collection::fromIterable($generator());
print_r($collection->all()); // [0 => 'c', 1 => 'b', 2 => 'd']

// Example 2 -> standalone usage
$even = static fn (int $value): bool => $value % 2 === 0;

$piped = Pipe::of()(Filter::of()($even), All::of())(new ArrayIterator([1, 2, 3, 4]));
print_r($piped); // [2, 4]
26 changes: 26 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@

class CollectionSpec extends ObjectBehavior
{
public function it_can_all(): void
{
$this::fromIterable([1, 2, 3])
->all()
->shouldIterateAs([1, 2, 3]);

$this::fromIterable(['foo' => 'f', 'bar' => 'b'])
->all()
->shouldIterateAs(['foo' => 'f', 'bar' => 'b']);

$duplicateKeyGen = static function (): Generator {
yield 'a' => 1;

yield 'b' => 2;

yield 'a' => 3;
};

$this::fromIterable($duplicateKeyGen())
->shouldIterateAs($duplicateKeyGen());

$this::fromIterable($duplicateKeyGen())
->all()
->shouldIterateAs(['a' => 3, 'b' => 2]);
}

public function it_can_append(): void
{
$generator = static function (): Generator {
Expand Down
3 changes: 2 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use loophp\collection\Iterator\IterableIterator;
use loophp\collection\Iterator\ResourceIterator;
use loophp\collection\Iterator\StringIterator;
use loophp\collection\Operation\All;
use loophp\collection\Operation\Append;
use loophp\collection\Operation\Apply;
use loophp\collection\Operation\Associate;
Expand Down Expand Up @@ -169,7 +170,7 @@ public function __construct(callable $callable, ...$parameters)

public function all(): array
{
return iterator_to_array($this->getIterator());
return All::of()($this->getIterator());
}

public function append(...$items): CollectionInterface
Expand Down
4 changes: 1 addition & 3 deletions src/Contract/Operation/Pipeable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace loophp\collection\Contract\Operation;

use Generator;
use Iterator;
use loophp\collection\Contract\Collection;

/**
Expand All @@ -20,7 +18,7 @@
interface Pipeable
{
/**
* @param callable(Iterator<TKey, T>): Generator<TKey, T> ...$callbacks
* @param callable(iterable<TKey, T>): iterable<TKey, T> ...$callbacks
*
* @return Collection<TKey, T>
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Operation/All.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Iterator;

/**
* @immutable
*
* @template TKey
* @template T
*/
final class All extends AbstractOperation
{
/**
* @pure
*
* @return Closure(Iterator<TKey, T>): array<TKey, T>
*/
public function __invoke(): Closure
{
return static fn (Iterator $iterator): array => iterator_to_array($iterator);
}
}
22 changes: 10 additions & 12 deletions src/Operation/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;

/**
* @immutable
Expand All @@ -26,31 +24,31 @@ final class Pipe extends AbstractOperation
/**
* @pure
*
* @return Closure(...callable(Iterator<TKey, T>):Generator<TKey, T, mixed, mixed>):Closure(Iterator<TKey, T>):Iterator<TKey, T>
* @return Closure(callable(iterable<TKey, T>): iterable<TKey, T> ...): Closure(iterable<TKey, T>): iterable<TKey, T>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded the type here from Iterator to iterable to allow for the All usage, which returns an array

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one!

*/
public function __invoke(): Closure
{
return
/**
* @param callable(Iterator<TKey, T>): Generator<TKey, T> ...$operations
* @param callable(iterable<TKey, T>): iterable<TKey, T> ...$operations
*
* @return Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @return Closure(iterable<TKey, T>): iterable<TKey, T>
*/
static fn (callable ...$operations): Closure =>
/**
* @param Iterator<TKey, T> $iterator
* @param iterable<TKey, T> $iterator
*
* @return Iterator<TKey, T>
* @return iterable<TKey, T>
*/
static function (Iterator $iterator) use ($operations): Iterator {
static function (iterable $iterator) use ($operations): iterable {
$callback =
/**
* @param Iterator<TKey, T> $iterator
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> $callable
* @param iterable<TKey, T> $iterator
* @param callable(iterable<TKey, T>): iterable<TKey, T> $callable
*
* @return Iterator<TKey, T>
* @return iterable<TKey, T>
*/
static fn (Iterator $iterator, callable $callable): Iterator => $callable($iterator);
static fn (iterable $iterator, callable $callable): iterable => $callable($iterator);

return array_reduce($operations, $callback, $iterator);
};
Expand Down