예제 #1
0
 public function testCurry()
 {
     $rest = 0;
     $adder = function ($a, $b, $c, $d) {
         return $a + $b + $c + $d;
     };
     $variadicAdder = function (...$args) {
         return Std::foldl(function ($acc, $cur) {
             return $acc + $cur;
         }, 0, $args);
     };
     $partiallyVariadicAdder = function ($a, $b, $c, ...$args) use(&$rest) {
         $rest = Std::foldl(function ($acc, $cur) {
             return $acc + $cur;
         }, 0, $args);
         return $a + $b + $c;
     };
     $one = Std::curry($adder, 2, 5);
     $two = $one(6);
     // Here we test adding one additional parameter that was not expected.
     // This should allow us to support partially variadic functions.
     $three = $two(10, 1000);
     $this->assertEquals(23, $three);
     // Variadic functions will return immediately since we can't determine
     // when they have been fulfilled.
     $four = Std::curry($variadicAdder, 2, 5);
     $this->assertEquals(ScalarTypes::SCALAR_INTEGER, TypeHound::fetch($four));
     $this->assertEquals(7, $four);
     $seven = Std::curry($partiallyVariadicAdder, 8, 5);
     $eight = $seven(9, 102, 20);
     $this->assertEquals(ScalarTypes::SCALAR_INTEGER, TypeHound::fetch($eight));
     $this->assertEquals(22, $eight);
     $this->assertEquals(122, $rest);
 }