Beispiel #1
0
 public function testMap()
 {
     $a = function () {
         (yield 1);
         (yield 3);
     };
     $modulo = function ($number) {
         return $number % 2;
     };
     $modded = P::toArray(P::map($modulo, $a()));
     $this->assertEquals(2, $modded[0] + $modded[1]);
     $biasedModulo = function ($number, $index) {
         return $number % 2 + (int) $index;
     };
     $a = function () {
         (yield '4' => 1);
         (yield '5' => 2);
     };
     $modded = P::toArray(P::map($biasedModulo, $a()));
     $this->assertEquals(10, $modded[0] + $modded[1]);
 }
Beispiel #2
0
$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