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
|
|
|
* @param callable $fn |
47
|
|
|
* @param array $args |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
function _apply($fn, $args) { |
51
|
|
|
switch (count($args)) { |
52
|
|
|
case 0: return $fn(); |
|
|
|
|
53
|
|
|
case 1: return $fn($args[0]); |
|
|
|
|
54
|
|
|
case 2: return $fn($args[0], $args[1]); |
|
|
|
|
55
|
|
|
case 3: return $fn($args[0], $args[1], $args[2]); |
|
|
|
|
56
|
|
View Code Duplication |
case 4: return $fn($args[0], $args[1], $args[2], $args[3]); |
|
|
|
|
57
|
|
View Code Duplication |
case 5: return $fn($args[0], $args[1], $args[2], $args[3], $args[4]); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
return call_user_func_array($fn, $args); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if `$a` is an argument placeholder. |
64
|
|
|
* ```php |
65
|
|
|
* F\_is_placeholder(F\__()); //=> true |
66
|
|
|
* F\_is_placeholder('other thing'); //=> false |
67
|
|
|
* ``` |
68
|
|
|
* |
69
|
|
|
* @signature * -> Boolean |
70
|
|
|
* @param mixed $a |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
function _is_placeholder($a) { |
74
|
|
|
return $a instanceof Placeholder; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Curry an unary function. |
79
|
|
|
* |
80
|
|
|
* @ignore |
81
|
|
|
* @signature (a -> b) -> (a -> b) |
82
|
|
|
* @param callable $fn |
83
|
|
|
* @return callable |
84
|
|
|
*/ |
85
|
|
|
function _curry_one($fn) { |
86
|
|
|
return function() use($fn) { |
87
|
|
|
$args = func_get_args(); |
88
|
|
|
return (count($args) > 0 && ! _is_placeholder($args[0])) |
89
|
|
|
? $fn($args[0]) |
90
|
|
|
: _curry_one($fn); |
91
|
|
|
}; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Curry an binary function. |
96
|
|
|
* |
97
|
|
|
* @ignore |
98
|
|
|
* @signature (a,b -> c) -> (a -> b -> c) |
99
|
|
|
* @param callable $fn |
100
|
|
|
* @return callable |
101
|
|
|
*/ |
102
|
|
|
function _curry_two($fn) { |
103
|
|
|
return function() use($fn) { |
104
|
|
|
$args = func_get_args(); |
105
|
|
|
$n = count($args); |
106
|
|
|
if ($n == 0) |
107
|
|
|
return _curry_two($fn); |
108
|
|
|
if ($n == 1) { |
109
|
|
|
$a = &$args[0]; |
110
|
|
|
if (_is_placeholder($a)) |
111
|
|
|
return _curry_two($fn); |
112
|
|
|
return _curry_one(function($b) use($fn, &$a) { |
113
|
|
|
return $fn($a, $b); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
$a = &$args[0]; |
117
|
|
|
$b = &$args[1]; |
118
|
|
|
if (_is_placeholder($a) && _is_placeholder($b)) |
119
|
|
|
return _curry_two($fn); |
120
|
|
|
if (_is_placeholder($a)) |
121
|
|
|
return _curry_one(function($_a) use($fn, &$b) { |
122
|
|
|
return $fn($_a, $b); |
123
|
|
|
}); |
124
|
|
|
if (_is_placeholder($b)) |
125
|
|
|
return _curry_one(function($_b) use($fn, &$a) { |
126
|
|
|
return $fn($a, $_b); |
127
|
|
|
}); |
128
|
|
|
return $fn($args[0], $args[1]); |
129
|
|
|
}; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Curry a function with 3 arguments. |
134
|
|
|
* |
135
|
|
|
* @ignore |
136
|
|
|
* @signature (a,b,c -> d) -> (a -> b -> c -> d) |
137
|
|
|
* @param callable $fn |
138
|
|
|
* @return callable |
139
|
|
|
*/ |
140
|
|
|
function _curry_three($fn) { |
141
|
|
|
return function() use($fn) { |
142
|
|
|
$args = func_get_args(); |
143
|
|
|
$n = count($args); |
144
|
|
|
while ($n > 0 && _is_placeholder($args[$n - 1])) |
145
|
|
|
$n --; |
146
|
|
|
if ($n == 0) |
147
|
|
|
return _curry_three($fn); |
148
|
|
|
if ($n == 1) { |
149
|
|
|
$a = &$args[0]; |
150
|
|
|
return _curry_two(function($b, $c) use($fn, &$a) { |
151
|
|
|
return $fn($a, $b, $c); |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
if ($n == 2) { |
155
|
|
|
$a = &$args[0]; $b = &$args[1]; |
156
|
|
|
|
157
|
|
|
if (_is_placeholder($a)) |
158
|
|
|
return _curry_one(function($_a, $c) use(&$b) { |
159
|
|
|
return $fn($_a, $b, $c); |
|
|
|
|
160
|
|
|
}); |
161
|
|
|
return _curry_one(function($c) use($fn, &$a, &$b) { |
162
|
|
|
return $fn($a, $b, $c); |
163
|
|
|
}); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$a = &$args[0]; $b = &$args[1]; $c = &$args[2]; |
167
|
|
|
|
168
|
|
View Code Duplication |
if (_is_placeholder($a) && _is_placeholder($b)) |
|
|
|
|
169
|
|
|
return _cuury_two(function($_a, $_b) use(&$c) { |
170
|
|
|
return $fn($_a, $_b, $c); |
|
|
|
|
171
|
|
|
}); |
172
|
|
View Code Duplication |
if (_is_placeholder($a)) |
|
|
|
|
173
|
|
|
return _cuury_one(function($_a) use(&$b, &$c) { |
174
|
|
|
return $fn($_a, $b, $c); |
|
|
|
|
175
|
|
|
}); |
176
|
|
View Code Duplication |
if (_is_placeholder($b)) |
|
|
|
|
177
|
|
|
return _cuury_one(function($_b) use(&$a, &$c) { |
178
|
|
|
return $fn($a, $_b, $c); |
|
|
|
|
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
return $fn($a, $b, $c); |
182
|
|
|
}; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Curry a function with `$n` arguments. |
187
|
|
|
* |
188
|
|
|
* @ignore |
189
|
|
|
* @signature (*... -> *) -> Number -> (* -> ... -> *) |
190
|
|
|
* @param callable $fn |
191
|
|
|
* @param int $n |
192
|
|
|
* @param array $given |
193
|
|
|
* @return callable |
194
|
|
|
*/ |
195
|
|
|
function _curry_n($fn, $n, $given = []) { |
196
|
|
|
return function() use($fn, $n, $given) { |
197
|
|
|
$args = func_get_args(); |
198
|
|
|
$merged = _merge_args($given, $args, $n); |
199
|
|
|
$args = $merged->args; |
200
|
|
|
switch ($merged->placeholders) { |
201
|
|
|
case 0: return _apply($fn, $args); |
|
|
|
|
202
|
|
|
case 1: |
203
|
|
|
return _curry_one(function($a) use($fn, &$args) { |
204
|
|
|
return _apply($fn, _fill_placeholders($args, [$a])); |
205
|
|
|
}); |
206
|
|
|
case 2: |
207
|
|
|
return _curry_two(function($a, $b) use($fn, &$args) { |
208
|
|
|
return _apply($fn, _fill_placeholders($args, [$a, $b])); |
209
|
|
|
}); |
210
|
|
|
case 3: |
211
|
|
|
return _curry_three(function($a, $b, $c) use($fn, &$args) { |
212
|
|
|
return _apply($fn, _fill_placeholders($args, [$a, $b, $c])); |
213
|
|
|
}); |
214
|
|
|
} |
215
|
|
|
return _curry_n($fn, $n, $args); |
216
|
|
|
}; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
function _merge_args(&$given, &$args, $n) { |
220
|
|
|
$merged = (object) [ |
221
|
|
|
'args' => [], |
222
|
|
|
'placeholders' => 0 |
223
|
|
|
]; |
224
|
|
|
$givenIndex = 0; $argsIndex = 0; $mergedIndex = 0; |
225
|
|
|
$givenCount = count($given); $argsCount = count($args); |
226
|
|
|
while ($mergedIndex < $n && ($givenIndex < $givenCount || $argsIndex < $argsCount)) { |
227
|
|
|
if ($givenIndex < $givenCount && !_is_placeholder($given[$givenIndex])) { |
228
|
|
|
$merged->args[$mergedIndex] = $given[$givenIndex]; |
229
|
|
|
} else if ($argsIndex < $argsCount) { |
230
|
|
|
$merged->args[$mergedIndex] = $args[$argsIndex]; |
231
|
|
|
$argsIndex ++; |
232
|
|
|
} else { |
233
|
|
|
$merged->args[$mergedIndex] = $given[$givenIndex]; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (_is_placeholder($merged->args[$mergedIndex])) |
237
|
|
|
$merged->placeholders ++; |
238
|
|
|
|
239
|
|
|
$givenIndex ++; |
240
|
|
|
$mergedIndex ++; |
241
|
|
|
} |
242
|
|
|
while ($mergedIndex < $n) { |
243
|
|
|
$merged->args[$mergedIndex] = Placeholder::get(); |
244
|
|
|
$mergedIndex ++; |
245
|
|
|
$merged->placeholders ++; |
246
|
|
|
} |
247
|
|
|
return $merged; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
function _fill_placeholders($args, $fillers) { |
251
|
|
|
$argsIndex = 0; $fillersIndex = 0; |
252
|
|
|
$argsCount = count($args); |
|
|
|
|
253
|
|
|
$fillersCount = count($fillers); |
254
|
|
|
while ($fillersIndex < $fillersCount) { |
255
|
|
|
while (!_is_placeholder($args[$argsIndex])) |
256
|
|
|
$argsIndex ++; |
257
|
|
|
$args[$argsIndex] = $fillers[$fillersIndex]; |
258
|
|
|
$fillersIndex ++; |
259
|
|
|
} |
260
|
|
|
return $args; |
261
|
|
|
} |
262
|
|
|
|
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.