-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
303 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Amp\DeferredFuture; | ||
use Amp\Future; | ||
use Revolt\EventLoop; | ||
|
||
use function Amp\async; | ||
|
||
// Define the Y-Combinator | ||
$U = static fn (Closure $f) => $f($f); | ||
$Y = static fn (Closure $f) => $U(static fn (Closure $x) => $f(static fn ($y) => $U($x)($y))); | ||
|
||
function wrapWithDeferred(Closure $job): Future | ||
{ | ||
$deferred = new DeferredFuture(); | ||
|
||
// Queue the operation to be executed in the event loop | ||
EventLoop::queue(static function () use ($job, $deferred) { | ||
$job(static function ($value) use ($deferred) { | ||
$deferred->complete($value); | ||
}, static function (Future $future, $next) { | ||
$future->map($next); | ||
}); | ||
}); | ||
|
||
return $deferred->getFuture(); | ||
} | ||
|
||
/*function factorialGen(callable $func): Closure | ||
{ | ||
return static function (int $n) use ($func): int { | ||
return ($n <= 1) ? 1 : $n * $func($n - 1); | ||
}; | ||
}*/ | ||
|
||
// Define the async factorial function using the Y-Combinator | ||
$asyncFactorial = $Y(static function ($factorial) { | ||
return static function ($n) use ($factorial): Future { | ||
return wrapWithDeferred(static function ($complete, $async) use ($n, $factorial) { | ||
if ($n <= 1) { | ||
$complete(1); | ||
} else { | ||
$async($factorial($n - 1), static function ($result) use ($n, $complete) { | ||
$complete($n * $result); | ||
}); | ||
} | ||
}); | ||
}; | ||
}); | ||
|
||
// The main loop that runs the async task | ||
$loop = static function () use ($asyncFactorial) { | ||
$future = $asyncFactorial(5); | ||
|
||
// Use the map method to handle the result when it's ready | ||
$future->map(static function ($result) { | ||
echo 'Factorial: ' . $result . PHP_EOL; | ||
}); | ||
}; | ||
|
||
// Defer the loop execution to run after the event loop starts | ||
EventLoop::defer($loop); | ||
|
||
// Run the event loop to process async tasks | ||
EventLoop::run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Amp\DeferredFuture; | ||
use Amp\Future; | ||
use Flow\AsyncHandler\AsyncHandler; | ||
use Flow\AsyncHandler\YAsyncHandler; | ||
use Flow\Driver\AmpDriver; | ||
use Flow\Event; | ||
use Flow\Examples\Data\YFlowData; | ||
use Flow\Flow\Flow; | ||
use Flow\Ip; | ||
use Revolt\EventLoop; | ||
|
||
// Define the Y-Combinator | ||
$U = static fn (Closure $f) => $f($f); | ||
$Y = static fn (Closure $f) => $U(static fn (Closure $x) => $f(static fn ($y) => $U($x)($y))); | ||
|
||
function wrapWithDeferred(Closure $job): Future | ||
{ | ||
$deferred = new DeferredFuture(); | ||
|
||
// Queue the operation to be executed in the event loop | ||
EventLoop::queue(static function () use ($job, $deferred) { | ||
try { | ||
$job(static function ($return) use ($deferred) { | ||
$deferred->complete($return); | ||
}, static function (Future $future, $next) { | ||
$future->map($next); | ||
}); | ||
} catch (Throwable $exception) { | ||
$deferred->complete(new RuntimeException($exception->getMessage(), $exception->getCode(), $exception)); | ||
} | ||
}); | ||
|
||
return $deferred->getFuture(); | ||
} | ||
|
||
$asyncFactorial = $Y(static function ($factorial) { | ||
return static function (YFlowData $data) use ($factorial) { | ||
return wrapWithDeferred(static function ($complete, $async) use ($data, $factorial) { | ||
if ($data->result <= 1) { | ||
$complete(new YFlowData($data->id, $data->number, 1)); | ||
} else { | ||
$async($factorial(new YFlowData($data->id, $data->number, $data->result - 1)), static function ($resultData) use ($data, $complete) { | ||
$complete(new YFlowData($data->id, $data->number, $data->result * $resultData->result)); | ||
}); | ||
} | ||
}); | ||
}; | ||
}); | ||
|
||
$factorialYJobAfter = static function (YFlowData $data): Future { | ||
return wrapWithDeferred(static function ($complete) use ($data) { | ||
printf("* #%d - Job : Result for factorial(%d) = %d\n", $data->id, $data->number, $data->result); | ||
|
||
$complete(new YFlowData($data->id, $data->number)); | ||
}); | ||
}; | ||
|
||
$driver = new AmpDriver(); | ||
|
||
$flow = (new Flow($asyncFactorial, null, null, null, new YAsyncHandler(), $driver)) | ||
->fn(new Flow($factorialYJobAfter, null, null, null, new AsyncHandler(), $driver)) | ||
; | ||
|
||
$ip = new Ip(new YFlowData(5, 5, 5)); | ||
$flow($ip); | ||
|
||
$flow->await(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.