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