|
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 = _curry_two(function($x, $y) { |
|
107
|
|
|
* return $x + $y; |
|
108
|
|
|
* }); |
|
109
|
|
|
* |
|
110
|
|
|
* $addOne = $add(1, F\__()); |
|
111
|
|
|
* $addOne(2); //=> 3 |
|
112
|
|
|
* ``` |
|
113
|
|
|
* |
|
114
|
|
|
* @ignore |
|
115
|
|
|
* @signature (a,b -> c) -> (a -> b -> c) |
|
116
|
|
|
* @param callable $fn |
|
117
|
|
|
* @return callable |
|
118
|
|
|
*/ |
|
119
|
|
|
function _curry_two($fn) { |
|
120
|
|
|
return function() use($fn) { |
|
121
|
|
|
$args = func_get_args(); |
|
122
|
|
|
$n = count($args); |
|
123
|
|
|
while ($n > 0 && _is_placeholder($args[$n - 1])) |
|
124
|
|
|
$n --; |
|
125
|
|
|
if ($n == 0) |
|
126
|
|
|
return _curry_two($fn); |
|
127
|
|
|
if ($n == 1) { |
|
128
|
|
|
$a = &$args[0]; |
|
129
|
|
|
if (_is_placeholder($a)) |
|
130
|
|
|
return _curry_two($fn); |
|
131
|
|
|
return _curry_one(function($b) use($fn, &$a) { |
|
132
|
|
|
return $fn($a, $b); |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
$a = &$args[0]; |
|
136
|
|
|
$b = &$args[1]; |
|
137
|
|
|
if (_is_placeholder($a) && _is_placeholder($b)) |
|
138
|
|
|
return _curry_two($fn); |
|
139
|
|
|
if (_is_placeholder($a)) |
|
140
|
|
|
return _curry_one(function($_a) use($fn, &$b) { |
|
141
|
|
|
return $fn($_a, $b); |
|
142
|
|
|
}); |
|
143
|
|
|
return $fn($args[0], $args[1]); |
|
144
|
|
|
}; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Curry a function with 3 arguments. |
|
149
|
|
|
* |
|
150
|
|
|
* ```php |
|
151
|
|
|
* $add = _curry_three(function($x, $y, $z) { |
|
152
|
|
|
* return $x + $y + $z; |
|
153
|
|
|
* }); |
|
154
|
|
|
* |
|
155
|
|
|
* $add(1, 2, 3); //=> 6 |
|
156
|
|
|
* $f = $add(F\__(), 2, F\__()); |
|
157
|
|
|
* $f(1, 3); //=> 6 |
|
158
|
|
|
* $g = $add(1, F\__(), 3); |
|
159
|
|
|
* $g(2); //=> 6 |
|
160
|
|
|
* $h = $add(F\__(), F\__(), 3); |
|
161
|
|
|
* $h(1, 2); //=> 6 |
|
162
|
|
|
* ``` |
|
163
|
|
|
* |
|
164
|
|
|
* @ignore |
|
165
|
|
|
* @signature (a,b,c -> d) -> (a -> b -> c -> d) |
|
166
|
|
|
* @param callable $fn |
|
167
|
|
|
* @return callable |
|
168
|
|
|
*/ |
|
169
|
|
|
function _curry_three($fn) { |
|
170
|
|
|
return function() use($fn) { |
|
171
|
|
|
$args = func_get_args(); |
|
172
|
|
|
$n = count($args); |
|
173
|
|
|
while ($n > 0 && _is_placeholder($args[$n - 1])) |
|
174
|
|
|
$n --; |
|
175
|
|
|
if ($n == 0) |
|
176
|
|
|
return _curry_three($fn); |
|
177
|
|
|
if ($n == 1) { |
|
178
|
|
|
$a = &$args[0]; |
|
179
|
|
|
return _curry_two(function($b, $c) use($fn, &$a) { |
|
180
|
|
|
return $fn($a, $b, $c); |
|
181
|
|
|
}); |
|
182
|
|
|
} |
|
183
|
|
|
if ($n == 2) { |
|
184
|
|
|
$a = &$args[0]; $b = &$args[1]; |
|
185
|
|
|
|
|
186
|
|
|
if (_is_placeholder($a)) |
|
187
|
|
|
return _curry_one(function($_a, $c) use(&$b) { |
|
188
|
|
|
return $fn($_a, $b, $c); |
|
|
|
|
|
|
189
|
|
|
}); |
|
190
|
|
|
return _curry_one(function($c) use($fn, &$a, &$b) { |
|
191
|
|
|
return $fn($a, $b, $c); |
|
192
|
|
|
}); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$a = &$args[0]; $b = &$args[1]; $c = &$args[2]; |
|
196
|
|
|
|
|
197
|
|
View Code Duplication |
if (_is_placeholder($a) && _is_placeholder($b)) |
|
|
|
|
|
|
198
|
|
|
return _cuury_two(function($_a, $_b) use(&$c) { |
|
199
|
|
|
return $fn($_a, $_b, $c); |
|
|
|
|
|
|
200
|
|
|
}); |
|
201
|
|
View Code Duplication |
if (_is_placeholder($a)) |
|
|
|
|
|
|
202
|
|
|
return _cuury_one(function($_a) use(&$b, &$c) { |
|
203
|
|
|
return $fn($_a, $b, $c); |
|
|
|
|
|
|
204
|
|
|
}); |
|
205
|
|
View Code Duplication |
if (_is_placeholder($b)) |
|
|
|
|
|
|
206
|
|
|
return _cuury_one(function($_b) use(&$a, &$c) { |
|
207
|
|
|
return $fn($a, $_b, $c); |
|
|
|
|
|
|
208
|
|
|
}); |
|
209
|
|
|
|
|
210
|
|
|
return $fn($a, $b, $c); |
|
211
|
|
|
}; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Curry a function with `$n` arguments. |
|
216
|
|
|
* |
|
217
|
|
|
* ```php |
|
218
|
|
|
* $polynomial = F\_curry_n(function($a, $b, $c, $x) { |
|
219
|
|
|
* return $a * $x * $x + $b * $x + $c; |
|
220
|
|
|
* }, 4); |
|
221
|
|
|
* |
|
222
|
|
|
* $linear = $polynomial(0); |
|
223
|
|
|
* $linear(2, 1, 5); //=> 11 |
|
224
|
|
|
* ``` |
|
225
|
|
|
* |
|
226
|
|
|
* @ignore |
|
227
|
|
|
* @signature (*... -> *) -> Number -> (* -> ... -> *) |
|
228
|
|
|
* @param callable $fn |
|
229
|
|
|
* @param int $n |
|
230
|
|
|
* @param array $given |
|
231
|
|
|
* @return callable |
|
232
|
|
|
*/ |
|
233
|
|
|
function _curry_n($fn, $n, $given = []) { |
|
234
|
|
|
return function() use($fn, $n, $given) { |
|
235
|
|
|
$args = func_get_args(); |
|
236
|
|
|
$merged = _merge_args($given, $args, $n); |
|
237
|
|
|
$args = $merged->args; |
|
238
|
|
|
switch ($merged->placeholders) { |
|
239
|
|
|
case 0: return _apply($fn, $args); |
|
|
|
|
|
|
240
|
|
|
case 1: |
|
241
|
|
|
return _curry_one(function($a) use($fn, &$args) { |
|
242
|
|
|
return _apply($fn, _fill_placeholders($args, [$a])); |
|
243
|
|
|
}); |
|
244
|
|
|
case 2: |
|
245
|
|
|
return _curry_two(function($a, $b) use($fn, &$args) { |
|
246
|
|
|
return _apply($fn, _fill_placeholders($args, [$a, $b])); |
|
247
|
|
|
}); |
|
248
|
|
|
case 3: |
|
249
|
|
|
return _curry_three(function($a, $b, $c) use($fn, &$args) { |
|
250
|
|
|
return _apply($fn, _fill_placeholders($args, [$a, $b, $c])); |
|
251
|
|
|
}); |
|
252
|
|
|
} |
|
253
|
|
|
return _curry_n($fn, $n, $args); |
|
254
|
|
|
}; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Merges already given with new arguments, filling placeholders in the process. |
|
259
|
|
|
* Returns an object holding the resulting args and the number of placeholders left. |
|
260
|
|
|
* |
|
261
|
|
|
* ```php |
|
262
|
|
|
* $given = [F\__(), 2]; |
|
263
|
|
|
* $args = [1, 3]; |
|
264
|
|
|
* $newArgs = F\_merge_args($given, $args, 4); |
|
265
|
|
|
* $newArgs; //=> (object) ['args' => [1, 2, 3, F\__()], 'placeholders' => 1] |
|
266
|
|
|
* ``` |
|
267
|
|
|
* |
|
268
|
|
|
* @param array &$given |
|
269
|
|
|
* @param array &$args |
|
270
|
|
|
* @param int $n |
|
271
|
|
|
* @return object |
|
272
|
|
|
*/ |
|
273
|
|
|
function _merge_args(&$given, &$args, $n) { |
|
274
|
|
|
$merged = (object) [ |
|
275
|
|
|
'args' => [], |
|
276
|
|
|
'placeholders' => 0 |
|
277
|
|
|
]; |
|
278
|
|
|
$givenIndex = 0; $argsIndex = 0; $mergedIndex = 0; |
|
279
|
|
|
$givenCount = count($given); $argsCount = count($args); |
|
280
|
|
|
while ($mergedIndex < $n && ($givenIndex < $givenCount || $argsIndex < $argsCount)) { |
|
281
|
|
|
if ($givenIndex < $givenCount && !_is_placeholder($given[$givenIndex])) { |
|
282
|
|
|
$merged->args[$mergedIndex] = $given[$givenIndex]; |
|
283
|
|
|
} else if ($argsIndex < $argsCount) { |
|
284
|
|
|
$merged->args[$mergedIndex] = $args[$argsIndex]; |
|
285
|
|
|
$argsIndex ++; |
|
286
|
|
|
} else { |
|
287
|
|
|
$merged->args[$mergedIndex] = $given[$givenIndex]; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
if (_is_placeholder($merged->args[$mergedIndex])) |
|
291
|
|
|
$merged->placeholders ++; |
|
292
|
|
|
|
|
293
|
|
|
$givenIndex ++; |
|
294
|
|
|
$mergedIndex ++; |
|
295
|
|
|
} |
|
296
|
|
|
while ($mergedIndex < $n) { |
|
297
|
|
|
$merged->args[$mergedIndex] = Placeholder::get(); |
|
298
|
|
|
$mergedIndex ++; |
|
299
|
|
|
$merged->placeholders ++; |
|
300
|
|
|
} |
|
301
|
|
|
return $merged; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
function _fill_placeholders($args, $fillers) { |
|
305
|
|
|
$argsIndex = 0; $fillersIndex = 0; |
|
306
|
|
|
$argsCount = count($args); |
|
|
|
|
|
|
307
|
|
|
$fillersCount = count($fillers); |
|
308
|
|
|
while ($fillersIndex < $fillersCount) { |
|
309
|
|
|
while (!_is_placeholder($args[$argsIndex])) |
|
310
|
|
|
$argsIndex ++; |
|
311
|
|
|
$args[$argsIndex] = $fillers[$fillersIndex]; |
|
312
|
|
|
$fillersIndex ++; |
|
313
|
|
|
} |
|
314
|
|
|
return $args; |
|
315
|
|
|
} |
|
316
|
|
|
|
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.