-
-
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
241 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\Examples\Data; | ||
|
||
class YFlowData | ||
{ | ||
public function __construct(public int $id, public ?int $number, public ?int $result = null) {} | ||
} |
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,203 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use Flow\Driver\AmpDriver; | ||
use Flow\Driver\FiberDriver; | ||
use Flow\Driver\ReactDriver; | ||
use Flow\Driver\SpatieDriver; | ||
use Flow\Driver\SwooleDriver; | ||
use Flow\Examples\Data\YFlowData; | ||
use Flow\Flow\Flow; | ||
use Flow\Flow\YFlow; | ||
use Flow\Ip; | ||
|
||
$driver = match (random_int(1, 4)) { | ||
1 => new AmpDriver(), | ||
2 => new FiberDriver(), | ||
3 => new ReactDriver(), | ||
4 => new SwooleDriver(), | ||
// 5 => new SpatieDriver(), | ||
}; | ||
printf("Use %s\n", $driver::class); | ||
|
||
function factorial(int $n): int | ||
{ | ||
return ($n <= 1) ? 1 : $n * factorial($n - 1); | ||
} | ||
|
||
function Ywrap(callable $f, callable $wrapperFunc): Closure | ||
{ | ||
$U = static fn ($x) => $x($x); | ||
|
||
return $U(static fn ($x) => $f($wrapperFunc(static fn ($y) => $U($x)($y)))); | ||
} | ||
|
||
function memoWrapperGenerator(callable $f): Closure | ||
{ | ||
static $cache = []; | ||
|
||
return static function ($y) use ($f, &$cache) { | ||
if (!isset($cache[$y])) { | ||
$cache[$y] = $f($y); | ||
} | ||
|
||
return $cache[$y]; | ||
}; | ||
} | ||
|
||
function Ymemo(callable $f): Closure | ||
{ | ||
return Ywrap($f, 'memoWrapperGenerator'); | ||
} | ||
|
||
function factorialGen(callable $func): Closure | ||
{ | ||
return static function (int $n) use ($func): int { | ||
return ($n <= 1) ? 1 : $n * $func($n - 1); | ||
}; | ||
} | ||
|
||
function factorialYMemo(int $n): int | ||
{ | ||
return Ymemo('factorialGen')($n); | ||
} | ||
|
||
/* | ||
use Amp\Promise; | ||
use Amp\Deferred; | ||
function Ywrap(callable $f, callable $wrapperFunc) { | ||
$U = fn($x) => $x($x); | ||
return $U(fn($x) => $f($wrapperFunc(fn($y) => Promise\wait($U($x)($y))))); | ||
} | ||
function asyncWrapper(callable $f) { | ||
return function($y) use ($f) { | ||
$deferred = new Deferred(); | ||
$deferred->resolve($f($y)); // Resolve immediately | ||
return $deferred->promise(); | ||
}; | ||
} | ||
function Ymemo($f) { | ||
return Ywrap($f, 'asyncWrapper'); | ||
} | ||
function factorialGen(callable $func) { | ||
return function (int $n) use ($func) { | ||
$deferred = new Deferred(); | ||
$result = ($n <= 1) ? 1 : $n * Promise\wait($func($n - 1)); | ||
$deferred->resolve($result); // Resolve immediately | ||
return $deferred->promise(); | ||
}; | ||
} | ||
function factorialYMemo(int $n) { | ||
return Promise\wait(Ymemo('factorialGen')($n)); | ||
} | ||
// Usage | ||
Amp\Loop::run(function() { | ||
$result = factorialYMemo(5); | ||
echo $result; // Expected: 120 | ||
}); | ||
*/ | ||
|
||
/* | ||
use Amp\Promise; | ||
class Flow { | ||
// ... other combinators ... | ||
public static function Y($f) { | ||
return (function ($x) use ($f) { | ||
return $f(function ($y) use ($x) { | ||
return Promise\wait($x($x)($y)); | ||
}); | ||
})(function ($x) { | ||
return $f(function ($y) use ($x) { | ||
return Promise\wait($x($x)($y)); | ||
}); | ||
}); | ||
} | ||
// ... rest of the class ... | ||
} | ||
use Amp\Loop; | ||
Loop::run(function() { | ||
$factorial = Flow::Y(function ($f) { | ||
return function ($x) use ($f) { | ||
return $x == 0 ? 1 : $x * $f($x - 1); | ||
}; | ||
}); | ||
echo $factorial(5); // Outputs: 120 | ||
}); | ||
*/ | ||
|
||
// factorialYMemo(6) . ' ' . factorialYMemo(5); | ||
|
||
$factorialJob = static function (YFlowData $data): YFlowData { | ||
printf("*.. #%d - Job 1 : Calculating factorial(%d)\n", $data->id, $data->number); | ||
|
||
// raw factorial calculation | ||
$result = factorial($data->number); | ||
|
||
printf("*.. #%d - Job 1 : Result for factorial(%d) = %d\n", $data->id, $data->number, $result); | ||
|
||
return new YFlowData($data->id, $data->number); | ||
}; | ||
|
||
$factorialYJobBefore = static function (YFlowData $data): YFlowData { | ||
printf(".*. #%d - Job 2 : Calculating factorial(%d)\n", $data->id, $data->number); | ||
|
||
return new YFlowData($data->id, $data->number, $data->number); | ||
}; | ||
|
||
$factorialYJob = static function ($factorial) { | ||
return static function (YFlowData $data) use ($factorial): YFlowData { | ||
return new YFlowData( | ||
$data->id, | ||
$data->number, | ||
($data->result <= 1) ? 1 : $data->result * $factorial(new YFlowData($data->id, $data->number, $data->result - 1))->result | ||
); | ||
}; | ||
}; | ||
|
||
$factorialYJobAfter = static function (YFlowData $data): YFlowData { | ||
printf(".*. #%d - Job 2 : Result for factorial(%d) = %d\n", $data->id, $data->number, $data->result); | ||
|
||
return new YFlowData(5, 5); | ||
}; | ||
|
||
$factorialYMemoJob = static function (YFlowData $data): YFlowData { | ||
printf("..* #%d - Job 3 : Calculating factorialYMemo(%d)\n", $data->id, $data->number); | ||
|
||
$result = factorialYMemo($data->number); | ||
|
||
printf("..* #%d - Job 3 : Result for factorialYMemo(%d) = %d\n", $data->id, $data->number, $result); | ||
|
||
return new YFlowData($data->id, $data->number); | ||
}; | ||
|
||
$flow = Flow::do(static function () use ($factorialJob, $factorialYJobBefore, $factorialYJob, $factorialYJobAfter, $factorialYMemoJob) { | ||
yield [$factorialJob]; | ||
yield [$factorialYJobBefore]; | ||
yield new YFlow($factorialYJob); | ||
yield [$factorialYJobAfter]; | ||
yield [$factorialYMemoJob]; | ||
}, ['driver' => $driver]); | ||
|
||
$ipPool = new SplObjectStorage(); | ||
|
||
for ($i = 5; $i <= 5; $i++) { | ||
$ip = new Ip(new YFlowData($i, $i)); | ||
$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