diff --git a/examples/1-simple-function.php b/examples/1-simple-function.php index ecfdec5..419541d 100644 --- a/examples/1-simple-function.php +++ b/examples/1-simple-function.php @@ -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; diff --git a/examples/2-echo.php b/examples/2-echo.php index 30076d9..ece2ac2 100644 --- a/examples/2-echo.php +++ b/examples/2-echo.php @@ -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')); diff --git a/examples/3-return-value.php b/examples/3-return-value.php index ba79557..5844578 100644 --- a/examples/3-return-value.php +++ b/examples/3-return-value.php @@ -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'))); diff --git a/examples/4-closure.php b/examples/4-closure.php index c97fbbd..2038872 100644 --- a/examples/4-closure.php +++ b/examples/4-closure.php @@ -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; diff --git a/src/functions.php b/src/functions.php index 97e5c4c..9518229 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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) { diff --git a/test/FilterTest.php b/test/FilterTest.php index 581640d..03904f7 100644 --- a/test/FilterTest.php +++ b/test/FilterTest.php @@ -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) { diff --git a/test/MapTest.php b/test/MapTest.php index be2f301..6102aa4 100644 --- a/test/MapTest.php +++ b/test/MapTest.php @@ -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) {