Skip to content

Commit

Permalink
refactor: Update asyncMap operation, make it variadic.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol committed Nov 6, 2020
1 parent 113b4aa commit 8b848bb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
12 changes: 8 additions & 4 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,28 @@ asyncMap

Apply one callback to every item of a collection and use the return value.

.. warning:: Operation are asynchronous and the result is then, non deterministic.
.. warning:: Asynchronously apply callbacks to a collection. This operation is deterministic, we cannot ensure the elements order at the end.

.. warning:: Keys are preserved, use the "normalize" operation if you want to re-index the keys.

Interface: `AsyncMapable`_

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

.. code-block:: php
$mapper = static function(int $value): int {
$mapper1 = static function(int $value): int {
sleep($value);
return $value;
};
$mapper2 = static function(int $value): int {
return $value * 2;
};
$collection = Collection::fromIterable(['c' => 3, 'b' => 2, 'a' => 1])
->asyncMap($mapper); // ['a' => 1, 'b' => 2, 'c' => 3]
->asyncMap($mapper1, $mapper2); // ['a' => 2, 'b' => 4, 'c' => 6]
cache
~~~~~
Expand Down
10 changes: 7 additions & 3 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,19 @@ static function ($carry, $key, $value) {

public function it_can_asyncMap(): void
{
$callback = static function (int $v): int {
$callback1 = static function (int $v): int {
sleep($v);

return $v;
};

$callback2 = static function (int $v): int {
return $v * 2;
};

$this::fromIterable(['c' => 3, 'b' => 2, 'a' => 1])
->asyncMap($callback)
->shouldIterateAs(['a' => 1, 'b' => 2, 'c' => 3]);
->asyncMap($callback1, $callback2)
->shouldIterateAs(['a' => 2, 'b' => 4, 'c' => 6]);
}

public function it_can_be_constructed_from_a_file(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ public function associate(
return $this->pipe(Associate::of()($callbackForKeys)($callbackForValues));
}

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

public function cache(?CacheItemPoolInterface $cache = null): CollectionInterface
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Operation/AsyncMapable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
interface AsyncMapable
{
/**
* Apply one asynchronous callback to a collection and use the return value.
* Asynchronously apply callbacks to a collection.
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function asyncMap(callable $callback): Collection;
public function asyncMap(callable ...$callbacks): Collection;
}
23 changes: 21 additions & 2 deletions src/Operation/AsyncMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,32 @@ public function __invoke(): Closure
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static fn (callable $callback): Closure =>
static fn (callable ...$callbacks): Closure =>
/**
* @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 {
$callbackFactory =
/**
* @param mixed $key
* @psalm-param TKey $key
*
* @psalm-return Closure(T, callable(T, TKey): T): T
*/
static fn ($key): Closure =>
/**
* @param mixed $carry
* @psalm-param T $carry
* @psalm-param callable(T, TKey): T $callback
*
* @psalm-return T
*/
static fn ($carry, callable $callback) => $callback($carry, $key);

$callback = static fn ($value, $key) => array_reduce($callbacks, $callbackFactory($key), $value);

$emitter = new Emitter();
$iter = $emitter->iterate();
$callback = parallel($callback);
Expand Down

0 comments on commit 8b848bb

Please sign in to comment.