println('Example 1'); \Rx\Observable::fromArray([1, 2, 3, 4])->subscribe(new \Rx\Observer\CallbackObserver(function ($x) { echo 'Next: ', $x, PHP_EOL; }, function (Exception $ex) { echo 'Error: ', $ex->getMessage(), PHP_EOL; }, function () { echo 'Completed', PHP_EOL; })); println('Example 2 Reduce Map Filter with Curry'); $isEven = function ($num) { return $num % 2 === 0; }; $add = function ($x, $y) { return $x + $y; }; $raiseTo = function ($power, $num) { return pow($num, $power); }; $computeSquare = P::curry2($raiseTo)(2); \Rx\Observable::fromArray([1, 2, 3, 4])->filter($isEven)->map($computeSquare)->reduce($add, 0)->subscribe($stdoutObserver()); //-> 20 println('Example 3 - Map'); \Rx\Observable::fromArray([1, 2, 3, 4])->map(function ($num) { return $num * $num; })->subscribe(new \Rx\Observer\CallbackObserver(function ($x) { echo 'Next: ', $x, PHP_EOL; }, function (Exception $ex) { echo 'Error: ', $ex->getMessage(), PHP_EOL; }, function () { echo 'Completed', PHP_EOL; }));
public function testCurry2() { $sum2 = function ($a, $b) { return $a + $b; }; $curriedSum2 = P::curry2($sum2); $addToFive = $curriedSum2(5); $this->assertEquals(8, $addToFive(3)); $this->assertEquals(8, $curriedSum2(3, 5)); }
$applyTax(6.0); //-> Closure $applyTax(6.0, 100); //-> 106 $applyTax(6.0)(100); //-> 106 // $total = P::compose($currency('USD'), $applyTax(6.0), 'P::sum'); // $total([10.95, 16.99, 25.99]); //-> USD 57.1658 // function findUserById($id) { // //... // return Option::fromValue($user); // } // $userOpt = Option::fromValue(findUserById(10)); //-> Option(Option(User)) // $userOpt->flatMap(P::prop('address'))->map(P::prop('country'))->get(); $a = function () { (yield 1); (yield 3); }; $modulo = function ($number) { return $number % 2; }; $modded = P::toArray(P::map($modulo, $a())); print_r($modded); // composition with currying $input = 'A complex system that works is invariably found to have evolved from a simple system that worked'; $explodeOnSpace = P::curry2('explode')(' '); $countWords = P::compose('count', $explodeOnSpace); $countWords($input); //-> 17