| 1 |  |  | <?php namespace Tarsana\Functional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file contains common internal functions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Caution: Code written here may seems stupid because it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * contains a lot of duplications and low level optimisation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * but this is needed to make the library as efficient as possible. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Adds the `Tarsana\Functional` namespace to a function name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This is useful to pass non-curried functions as parameter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * F\_f('foo'); //=> 'Tarsana\Functional\foo' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @signature String -> Sring | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @param  string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | function _f($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     $name = "Tarsana\\Functional\\{$name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     return $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Gets the number of arguments of a function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * F\_number_of_args(function($x, $y){}); //=> 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @signature (* -> *) -> Number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | function _number_of_args($fn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     $reflector = is_array($fn) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         new \ReflectionMethod($fn[0], $fn[1]) : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         new \ReflectionFunction($fn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     return $reflector->getNumberOfRequiredParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  * Non curried version of apply for internal use. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * $sum = function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  *     return F\sum(func_get_args()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * F\_apply($sum, [1, 2, 3, 4, 5]); //=> 15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * F\_apply($sum, [1, 2, 3, 4, 5, 6]); //=> 21 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * @param  array    $args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | function _apply($fn, $args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     switch (count($args)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         case 0: return $fn(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         case 1: return $fn($args[0]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         case 2: return $fn($args[0], $args[1]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         case 3: return $fn($args[0], $args[1], $args[2]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |         case 4: return $fn($args[0], $args[1], $args[2], $args[3]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 65 |  | View Code Duplication |         case 5: return $fn($args[0], $args[1], $args[2], $args[3], $args[4]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     return call_user_func_array($fn, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  * Checks if `$a` is an argument placeholder. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * F\_is_placeholder(F\__()); //=> true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * F\_is_placeholder('other thing'); //=> false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  * @signature * -> Boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  * @param  mixed  $a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | function _is_placeholder($a) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     return $a instanceof Placeholder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  * Curry an unary function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  * @ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  * @signature (a -> b) -> (a -> b) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  * @return callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | function _curry_one($fn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     return function() use($fn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return (count($args) > 0 && ! _is_placeholder($args[0])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ? $fn($args[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             : _curry_one($fn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  * Curry an binary function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  * $add = F\_curry_two(function($x, $y) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  *     return $x + $y; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  * }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  * $addOne = $add(1, F\__()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  * $addOne(2); //=> 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  * @signature (a,b -> c) -> (a -> b -> c) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  * @return callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | function _curry_two($fn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     return function() use($fn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $n = count($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         while ($n > 0 && _is_placeholder($args[$n - 1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $n --; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if ($n == 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             return _curry_two($fn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if ($n == 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $a = &$args[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             if (_is_placeholder($a)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 return _curry_two($fn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return _curry_one(function($b) use($fn, &$a) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 return $fn($a, $b); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $a = &$args[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $b = &$args[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         if (_is_placeholder($a) && _is_placeholder($b)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             return _curry_two($fn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         if (_is_placeholder($a)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             return _curry_one(function($_a) use($fn, &$b) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 return $fn($_a, $b); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $fn($args[0], $args[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  * Curry a function with 3 arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  * $add = F\_curry_three(function($x, $y, $z) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  *     return $x + $y + $z; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  * }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  * $add(1, 2, 3); //=> 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  * $f = $add(F\__(), 2, F\__()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  * $f(1, 3); //=> 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  * $g = $add(1, F\__(), 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  * $g(2); //=> 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  * $h = $add(F\__(), F\__(), 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  * $h(1, 2); //=> 6 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  * @signature (a,b,c -> d) -> (a -> b -> c -> d) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  * @return callable | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 166 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | function _curry_three($fn) { | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     return function() use($fn) { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |         $args = func_get_args(); | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |         $n = count($args); | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         while ($n > 0 && _is_placeholder($args[$n - 1])) | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |             $n --; | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         if ($n == 0) | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |             return _curry_three($fn); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |         if ($n == 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |             $a = &$args[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |             return _curry_two(function($b, $c) use($fn, &$a) { | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |                 return $fn($a, $b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |         if ($n == 2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |             $a = &$args[0]; $b = &$args[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |             if (_is_placeholder($a)) | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |                 return _curry_two(function($_a, $c) use($fn, &$b) { | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |                     return $fn($_a, $b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |                 }); | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |             return _curry_one(function($c) use($fn, &$a, &$b) { | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |                 return $fn($a, $b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |         $a = &$args[0]; $b = &$args[1]; $c = &$args[2]; | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 195 |  | View Code Duplication |         if (_is_placeholder($a) && _is_placeholder($b)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |             return _curry_two(function($_a, $_b) use($fn, &$c) { | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |                 return $fn($_a, $_b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |             }); | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 199 |  | View Code Duplication |         if (_is_placeholder($a)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |             return _curry_one(function($_a) use($fn, &$b, &$c) { | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |                 return $fn($_a, $b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |             }); | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 203 |  | View Code Duplication |         if (_is_placeholder($b)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |             return _curry_one(function($_b) use($fn, &$a, &$c) { | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |                 return $fn($a, $_b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |         return $fn($a, $b, $c); | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |     }; | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  * Curry a function with `$n` arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  * $polynomial = F\_curry_n(function($a, $b, $c, $x) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  *     return $a * $x * $x + $b * $x + $c; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  * }, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  * $linear = $polynomial(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  * $linear(2, 1, 5); //=> 11 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  * @signature (*... -> *) -> Number -> (* -> ... -> *) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  * @param  callable $fn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  * @param  int $n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  * @param  array $given | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |  * @return callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | function _curry_n($fn, $n, $given = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     return function() use($fn, $n, $given) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         $args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         $merged = _merge_args($given, $args, $n); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         $args = $merged->args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         switch ($merged->placeholders) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             case 0: return _apply($fn, $args); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             case 1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                 return _curry_one(function($a) use($fn, &$args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |                     return _apply($fn, _fill_placeholders($args, [$a])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             case 2: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |                 return _curry_two(function($a, $b) use($fn, &$args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |                     return _apply($fn, _fill_placeholders($args, [$a, $b])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |             case 3: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |                 return _curry_three(function($a, $b, $c) use($fn, &$args) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |                     return _apply($fn, _fill_placeholders($args, [$a, $b, $c])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         return _curry_n($fn, $n, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  * Merges already given with new arguments, filling placeholders in the process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |  * Returns an object holding the resulting args and the number of placeholders left. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  * ```php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |  * $given = [F\__(), 2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  * $args = [1, 3]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  * $newArgs = F\_merge_args($given, $args, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |  * $newArgs; //=> (object) ['args' => [1, 2, 3, F\__()], 'placeholders' => 1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |  * @param  array &$given | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  * @param  array &$args | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |  * @param  int $n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |  * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | function _merge_args(&$given, &$args, $n) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |     $merged = (object) [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         'args' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |         'placeholders' => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |     $givenIndex = 0; $argsIndex = 0; $mergedIndex = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     $givenCount = count($given); $argsCount = count($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |     while ($mergedIndex < $n && ($givenIndex < $givenCount || $argsIndex < $argsCount)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         if ($givenIndex < $givenCount && !_is_placeholder($given[$givenIndex])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |             $merged->args[$mergedIndex] = $given[$givenIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         } else if ($argsIndex < $argsCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |             $merged->args[$mergedIndex] = $args[$argsIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             $argsIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |             $merged->args[$mergedIndex] = $given[$givenIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |         if (_is_placeholder($merged->args[$mergedIndex])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |             $merged->placeholders ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         $givenIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         $mergedIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |     while ($mergedIndex < $n) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |         $merged->args[$mergedIndex] = Placeholder::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |         $mergedIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |         $merged->placeholders ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |     return $merged; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | function _fill_placeholders($args, $fillers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     $argsIndex = 0; $fillersIndex = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     $argsCount = count($args); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |     $fillersCount = count($fillers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     while ($fillersIndex < $fillersCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |         while (!_is_placeholder($args[$argsIndex])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |             $argsIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |         $args[$argsIndex] = $fillers[$fillersIndex]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |         $fillersIndex ++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |     return $args; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 312 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 313 |  |  |  | 
            
                        
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.