swoole / library

📚 Swoole Library
https://wiki.swoole.com/#/library
Apache License 2.0
237 stars 58 forks source link

Add Co\map() function #57

Closed leocavalcante closed 3 years ago

leocavalcante commented 3 years ago

Declaration

<?php
Swoole\Coroutine\map(array $list, callable $fn, float $timeout = -1): array

Parameters

Return

Description

Conceptually similar to batch() and parallel(), but behaves more like array_map(), each element will be mapped within a coroutine.

Example

<?php declare(strict_types=1);

use Swoole\Coroutine;
use function Swoole\Coroutine\map;

function fatorial(int $n): int
{
    return array_product(range($n, 1));
}

Coroutine\run(function () {
    $results = map([2, 3, 4], 'fatorial'); 
    print_r($results); // 2 6 24
});