mirror of
https://github.com/danog/parallel-functions.git
synced 2024-11-26 12:24:52 +01:00
Rename functions back to parallelMap and parallelFilter
This commit is contained in:
parent
96c5ee7bae
commit
43c6fc74c9
@ -2,7 +2,7 @@
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use function Amp\ParallelFunctions\map;
|
||||
use function Amp\ParallelFunctions\parallelMap;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
$start = \microtime(true);
|
||||
@ -11,6 +11,6 @@ $start = \microtime(true);
|
||||
//
|
||||
// All communication is non-blocking and can be used in an event loop. Amp\Promise\wait() can be used to use the library
|
||||
// in a traditional synchronous environment.
|
||||
wait(map([1, 2, 3], 'sleep'));
|
||||
wait(parallelMap([1, 2, 3], 'sleep'));
|
||||
|
||||
print 'Took ' . (\microtime(true) - $start) . ' milliseconds.' . \PHP_EOL;
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use function Amp\ParallelFunctions\map;
|
||||
use function Amp\ParallelFunctions\parallelMap;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
// All output in the parallel environment is redirected to STDERR of the parent process automatically.
|
||||
// You might notice that the output order varies here when running it multiple times.
|
||||
wait(map([1, 2, 3], 'var_dump'));
|
||||
wait(parallelMap([1, 2, 3], 'var_dump'));
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use function Amp\ParallelFunctions\map;
|
||||
use function Amp\ParallelFunctions\parallelMap;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
// We have seen that the order can vary in the previous example, values returned have a deterministic order.
|
||||
\var_dump(wait(map([1, 2, 3], 'abs')));
|
||||
\var_dump(wait(parallelMap([1, 2, 3], 'abs')));
|
||||
|
@ -2,12 +2,12 @@
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use function Amp\ParallelFunctions\map;
|
||||
use function Amp\ParallelFunctions\parallelMap;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
// Parallel function execution is nice, but it's even better being able to use closures instead of having to write a
|
||||
// function that has to be autoloadable.
|
||||
\var_dump(wait(map([1, 2, 3], function ($time) {
|
||||
\var_dump(wait(parallelMap([1, 2, 3], function ($time) {
|
||||
\sleep($time); // a blocking function call, might also do blocking I/O here
|
||||
|
||||
return $time * $time;
|
||||
|
@ -64,7 +64,7 @@ function parallel(callable $callable): callable {
|
||||
* @return Promise Resolves to the result once the operation finished.
|
||||
* @throws \Error
|
||||
*/
|
||||
function map(array $array, callable $callable): Promise {
|
||||
function parallelMap(array $array, callable $callable): Promise {
|
||||
return call(function () use ($array, $callable) {
|
||||
// Amp\Promise\any() guarantees that all operations finished prior to resolving. Amp\Promise\all() doesn't.
|
||||
// Additionally, we return all errors as a MultiReasonException instead of throwing on the first error.
|
||||
@ -87,7 +87,7 @@ function map(array $array, callable $callable): Promise {
|
||||
*
|
||||
* @return Promise
|
||||
*/
|
||||
function filter(array $array, callable $callable = null, int $flag = 0): Promise {
|
||||
function parallelFilter(array $array, callable $callable = null, int $flag = 0): Promise {
|
||||
return call(function () use ($array, $callable, $flag) {
|
||||
if ($callable === null) {
|
||||
if ($flag === \ARRAY_FILTER_USE_BOTH || $flag === \ARRAY_FILTER_USE_KEY) {
|
||||
|
@ -4,14 +4,14 @@ namespace Amp\ParallelFunctions\Test;
|
||||
|
||||
use Amp\MultiReasonException;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
use function Amp\ParallelFunctions\filter;
|
||||
use function Amp\ParallelFunctions\parallelFilter;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
class FilterTest extends TestCase {
|
||||
public function testWithoutCallback() {
|
||||
$input = [1, 0, 3, false, true, null];
|
||||
|
||||
$this->assertSame(\array_filter($input), wait(filter($input)));
|
||||
$this->assertSame(\array_filter($input), wait(parallelFilter($input)));
|
||||
}
|
||||
|
||||
public function testWithCallback() {
|
||||
@ -20,7 +20,7 @@ class FilterTest extends TestCase {
|
||||
return $value === false;
|
||||
};
|
||||
|
||||
$this->assertSame(\array_filter($input, $callback), wait(filter($input, $callback)));
|
||||
$this->assertSame(\array_filter($input, $callback), wait(parallelFilter($input, $callback)));
|
||||
}
|
||||
|
||||
public function testWithCallbackAndFlagKey() {
|
||||
@ -29,7 +29,7 @@ class FilterTest extends TestCase {
|
||||
return $key === 2;
|
||||
};
|
||||
|
||||
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_KEY), wait(filter($input, $callback, \ARRAY_FILTER_USE_KEY)));
|
||||
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_KEY), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_KEY)));
|
||||
}
|
||||
|
||||
public function testWithCallbackAndFlagBoth() {
|
||||
@ -38,13 +38,13 @@ class FilterTest extends TestCase {
|
||||
return $key === 2 || $value === true;
|
||||
};
|
||||
|
||||
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_BOTH), wait(filter($input, $callback, \ARRAY_FILTER_USE_BOTH)));
|
||||
$this->assertSame(\array_filter($input, $callback, \ARRAY_FILTER_USE_BOTH), wait(parallelFilter($input, $callback, \ARRAY_FILTER_USE_BOTH)));
|
||||
}
|
||||
|
||||
public function testException() {
|
||||
$this->expectException(MultiReasonException::class);
|
||||
|
||||
wait(filter([1, 2, 3], function () {
|
||||
wait(parallelFilter([1, 2, 3], function () {
|
||||
throw new \Exception;
|
||||
}));
|
||||
}
|
||||
@ -57,7 +57,7 @@ class FilterTest extends TestCase {
|
||||
];
|
||||
|
||||
try {
|
||||
wait(filter($files, function ($args) {
|
||||
wait(parallelFilter($files, function ($args) {
|
||||
list($id, $filename) = $args;
|
||||
|
||||
if ($id === 0) {
|
||||
|
@ -4,12 +4,12 @@ namespace Amp\ParallelFunctions\Test;
|
||||
|
||||
use Amp\MultiReasonException;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
use function Amp\ParallelFunctions\map;
|
||||
use function Amp\ParallelFunctions\parallelMap;
|
||||
use function Amp\Promise\wait;
|
||||
|
||||
class MapTest extends TestCase {
|
||||
public function testValidInput() {
|
||||
$this->assertSame([3, 4, 5], wait(map([1, 2, 3], function ($input) {
|
||||
$this->assertSame([3, 4, 5], wait(parallelMap([1, 2, 3], function ($input) {
|
||||
return $input + 2;
|
||||
})));
|
||||
}
|
||||
@ -17,7 +17,7 @@ class MapTest extends TestCase {
|
||||
public function testException() {
|
||||
$this->expectException(MultiReasonException::class);
|
||||
|
||||
wait(map([1, 2, 3], function () {
|
||||
wait(parallelMap([1, 2, 3], function () {
|
||||
throw new \Exception;
|
||||
}));
|
||||
}
|
||||
@ -30,7 +30,7 @@ class MapTest extends TestCase {
|
||||
];
|
||||
|
||||
try {
|
||||
wait(map($files, function ($args) {
|
||||
wait(parallelMap($files, function ($args) {
|
||||
list($id, $filename) = $args;
|
||||
|
||||
if ($id === 0) {
|
||||
|
Loading…
Reference in New Issue
Block a user