|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Underscore; |
|
4
|
|
|
|
|
5
|
|
|
class Functions |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Creates a function that memoizes the result of the $payload |
|
9
|
|
|
* |
|
10
|
|
|
* @param callable $payload |
|
11
|
|
|
* |
|
12
|
|
|
* @return \Closure |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function memoize($payload) |
|
15
|
|
|
{ |
|
16
|
|
|
$function = function () use ($payload) { |
|
17
|
|
|
static $cache = []; |
|
18
|
|
|
$args = func_get_args(); |
|
19
|
|
|
$hash = md5(serialize($args)); |
|
20
|
|
|
|
|
21
|
|
|
if (!array_key_exists($hash, $cache)) { |
|
22
|
|
|
$cache[$hash] = call_user_func_array($payload, $args); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
return $cache[$hash]; |
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
|
|
return $function; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates function that simply returns given argument |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Closure |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function nop() |
|
37
|
|
|
{ |
|
38
|
|
|
return function ($item) { |
|
39
|
|
|
return $item; |
|
40
|
|
|
}; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates a version of the function that can only be called one time. |
|
45
|
|
|
* |
|
46
|
|
|
* Repeated calls to the modified function will have no effect, returning |
|
47
|
|
|
* the value from the original call. Useful for initialization functions, |
|
48
|
|
|
* instead of having to set a boolean flag and then check it later. |
|
49
|
|
|
* |
|
50
|
|
|
* @param callable $function |
|
51
|
|
|
* @return \Closure |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function once($function) |
|
54
|
|
|
{ |
|
55
|
|
|
$called = false; |
|
56
|
|
|
$value = null; |
|
57
|
|
|
|
|
58
|
|
|
return function () use ($function, &$called, &$value) { |
|
59
|
|
|
if (!$called) { |
|
60
|
|
|
$args = func_get_args(); |
|
61
|
|
|
$value = call_user_func_array($function, $args); |
|
62
|
|
|
$called = true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $value; |
|
66
|
|
|
}; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Provides a constant value that is only used for partial(). |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Closure |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function p() |
|
75
|
|
|
{ |
|
76
|
|
|
static $function; |
|
77
|
|
|
if (!$function) { |
|
78
|
|
|
$function = function () { |
|
79
|
|
|
return 'placeholder for partial'; |
|
80
|
|
|
}; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $function; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Partially apply a function by filling in any number of its arguments. |
|
88
|
|
|
* |
|
89
|
|
|
* You may pass p() in your list of arguments to specify an argument that |
|
90
|
|
|
* should not be pre-filled, but left open to supply at call-time. |
|
91
|
|
|
* |
|
92
|
|
|
* @param callable $function |
|
93
|
|
|
* @param mixed $arg |
|
94
|
|
|
* @param mixed ... |
|
95
|
|
|
* @return \Closure |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function partial($function, $arg) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$bound = func_get_args(); |
|
100
|
|
|
array_shift($bound); // remove $function |
|
101
|
|
|
|
|
102
|
|
|
$placeholder = static::p(); |
|
103
|
|
|
|
|
104
|
|
|
return function () use ($function, $bound, $placeholder) { |
|
105
|
|
|
$inject = func_get_args(); |
|
106
|
|
|
|
|
107
|
|
|
$args = []; |
|
108
|
|
|
foreach ($bound as $value) { |
|
109
|
|
|
if ($value === $placeholder) { |
|
110
|
|
|
$args[] = array_shift($inject); |
|
111
|
|
|
} else { |
|
112
|
|
|
$args[] = $value; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Append any remaining arguments |
|
117
|
|
|
$args = array_merge($args, $inject); |
|
118
|
|
|
|
|
119
|
|
|
return call_user_func_array($function, $args); |
|
120
|
|
|
}; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the composition of a list of functions. |
|
125
|
|
|
* |
|
126
|
|
|
* Each function consumes the return value of the function that follows. |
|
127
|
|
|
* In math terms, composing the functions f(), g(), and h() produces f(g(h())). |
|
128
|
|
|
* |
|
129
|
|
|
* @param callable $function |
|
130
|
|
|
* @param callable ... |
|
131
|
|
|
* @return \Closure |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function compose($function) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$functions = array_reverse(func_get_args()); |
|
136
|
|
|
|
|
137
|
|
|
return function ($value) use ($functions) { |
|
138
|
|
|
foreach ($functions as $function) { |
|
139
|
|
|
$value = $function($value); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $value; |
|
143
|
|
|
}; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Wraps the first function inside of the wrapper function, passing it as the first argument. |
|
148
|
|
|
* |
|
149
|
|
|
* This allows the wrapper to execute code before and after the function runs, |
|
150
|
|
|
* adjust the arguments, and execute it conditionally. |
|
151
|
|
|
* |
|
152
|
|
|
* @param callable $function |
|
153
|
|
|
* @param callable $wrapper |
|
154
|
|
|
* @return \Closure |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function wrap($function, $wrapper) |
|
157
|
|
|
{ |
|
158
|
|
|
return function () use ($function, $wrapper) { |
|
159
|
|
|
$args = func_get_args(); |
|
160
|
|
|
array_unshift($args, $function); // make $function the first argument |
|
161
|
|
|
return call_user_func_array($wrapper, $args); |
|
162
|
|
|
}; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Creates and returns a new, throttled version of the passed function. |
|
167
|
|
|
* |
|
168
|
|
|
* When invoked repeatedly, will only actually call the original function at |
|
169
|
|
|
* most once per every wait milliseconds. Useful for rate-limiting events that |
|
170
|
|
|
* occur faster than you can keep up with. |
|
171
|
|
|
* |
|
172
|
|
|
* By default, throttle will execute the function as soon as you call it |
|
173
|
|
|
* for the first time, and, if you call it again any number of times during |
|
174
|
|
|
* the wait period, as soon as that period is over. If you'd like to disable |
|
175
|
|
|
* the leading-edge call, pass `leading => false`. |
|
176
|
|
|
* |
|
177
|
|
|
* NOTE: Does not support the `trailing` option because there is no timeout |
|
178
|
|
|
* functionality in PHP (without using threads). |
|
179
|
|
|
* |
|
180
|
|
|
* NOTE: No arguments are passed to the function on the leading edge call! |
|
181
|
|
|
* |
|
182
|
|
|
* @param callable $function |
|
183
|
|
|
* @param integer $wait |
|
184
|
|
|
* @param array $options |
|
185
|
|
|
* @return \Closure |
|
186
|
|
|
*/ |
|
187
|
|
|
public static function throttle($function, $wait, array $options = []) |
|
188
|
|
|
{ |
|
189
|
|
|
$options += [ |
|
190
|
|
|
'leading' => true, |
|
191
|
|
|
]; // @codeCoverageIgnore |
|
192
|
|
|
|
|
193
|
|
|
$previous = 0; |
|
194
|
|
|
|
|
195
|
|
|
$callback = function () use ($function, $wait, &$previous) { |
|
196
|
|
|
$now = floor(microtime(true) * 1000); // convert float to integer |
|
197
|
|
|
|
|
198
|
|
|
if (($wait - ($now - $previous)) <= 0) { |
|
199
|
|
|
$args = func_get_args(); |
|
200
|
|
|
call_user_func_array($function, $args); |
|
201
|
|
|
$previous = $now; |
|
202
|
|
|
} |
|
203
|
|
|
}; |
|
204
|
|
|
|
|
205
|
|
|
if ($options['leading'] !== false) { |
|
206
|
|
|
$callback(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $callback; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Creates a version of the function that will only be run after first being called count times. |
|
214
|
|
|
* |
|
215
|
|
|
* Useful for grouping asynchronous responses, where you want to be sure that |
|
216
|
|
|
* all the async calls have finished, before proceeding. |
|
217
|
|
|
* |
|
218
|
|
|
* @param integer $count |
|
219
|
|
|
* @param callable $function |
|
220
|
|
|
* @return \Closure |
|
221
|
|
|
*/ |
|
222
|
|
|
public static function after($count, $function) |
|
223
|
|
|
{ |
|
224
|
|
|
return function () use ($function, &$count) { |
|
225
|
|
|
if (--$count < 1) { |
|
226
|
|
|
$args = func_get_args(); |
|
227
|
|
|
|
|
228
|
|
|
return call_user_func_array($function, $args); |
|
229
|
|
|
} |
|
230
|
|
|
}; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Creates a version of the function that can be called no more than count times. |
|
235
|
|
|
* |
|
236
|
|
|
* The result of the last function call is memoized and returned when count has been reached. |
|
237
|
|
|
* |
|
238
|
|
|
* @param integer $count |
|
239
|
|
|
* @param callable $function |
|
240
|
|
|
* @return \Closure |
|
241
|
|
|
*/ |
|
242
|
|
|
public static function before($count, $function) |
|
243
|
|
|
{ |
|
244
|
|
|
$memo = null; |
|
245
|
|
|
|
|
246
|
|
|
return function () use ($function, &$count, &$memo) { |
|
247
|
|
|
if (--$count > 0) { |
|
248
|
|
|
$args = func_get_args(); |
|
249
|
|
|
$memo = call_user_func_array($function, $args); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return $memo; |
|
253
|
|
|
}; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.