1 | <?php |
||||
2 | namespace Aerophant\Ramda; |
||||
3 | |||||
4 | /** |
||||
5 | * @param callable $predicateFunction |
||||
6 | * @param array $array |
||||
7 | * @return mixed |
||||
8 | */ |
||||
9 | function all() |
||||
10 | { |
||||
11 | $all = function (callable $predicateFunction, array $array): bool { |
||||
12 | 3 | if (empty($array)) { |
|||
13 | 1 | return false; |
|||
14 | } |
||||
15 | 2 | foreach ($array as $it) { |
|||
16 | 2 | if (!$predicateFunction($it)) { |
|||
17 | 2 | return false; |
|||
18 | } |
||||
19 | } |
||||
20 | 1 | return true; |
|||
21 | 3 | }; |
|||
22 | 3 | $arguments = func_get_args(); |
|||
23 | 3 | $curried = curryN($all, 2); |
|||
24 | 3 | return call_user_func_array($curried, $arguments); |
|||
25 | } |
||||
26 | |||||
27 | /** |
||||
28 | * @param callable $predicateFunction |
||||
29 | * @param array $array |
||||
30 | * @return bool |
||||
31 | */ |
||||
32 | function any() |
||||
33 | { |
||||
34 | $any = function (callable $predicateFunction, array $array) { |
||||
35 | 3 | if (empty($array)) { |
|||
36 | 1 | return false; |
|||
37 | } |
||||
38 | 2 | foreach ($array as $it) { |
|||
39 | 2 | if ($predicateFunction($it)) { |
|||
40 | 2 | return true; |
|||
41 | } |
||||
42 | } |
||||
43 | 1 | return false; |
|||
44 | 3 | }; |
|||
45 | 3 | $arguments = func_get_args(); |
|||
46 | 3 | $curriedAny = curryN($any, 2); |
|||
47 | 3 | return call_user_func_array($curriedAny, $arguments); |
|||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * @param callable $fn he function to invoke. Receives one argument |
||||
52 | * @param array $array The list to iterate over. |
||||
53 | * @return null|\Closure |
||||
54 | */ |
||||
55 | function arrayForEach() |
||||
56 | { |
||||
57 | $arrayForEach = function (callable $fn, array $array) { |
||||
58 | 1 | foreach ($array as $item) { |
|||
59 | 1 | $fn($item); |
|||
60 | } |
||||
61 | 1 | }; |
|||
62 | 1 | $arguments = func_get_args(); |
|||
63 | 1 | $curriedAny = curryN($arrayForEach, 2); |
|||
64 | 1 | return call_user_func_array($curriedAny, $arguments); |
|||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * a → [a] → [a] |
||||
69 | * @param mixed $item |
||||
70 | * @param array $array |
||||
71 | * @return array|\Closure |
||||
72 | */ |
||||
73 | function append() |
||||
74 | { |
||||
75 | $append = function ($item, array $array) { |
||||
76 | 2 | return array_merge($array, [$item]); |
|||
77 | 2 | }; |
|||
78 | 2 | $arguments = func_get_args(); |
|||
79 | 2 | $curriedAny = curryN($append, 2); |
|||
80 | 2 | return call_user_func_array($curriedAny, $arguments); |
|||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * [a] → [a] → [a] |
||||
85 | * @param mixed $item |
||||
86 | * @param array $array |
||||
87 | * @return array|\Closure |
||||
88 | */ |
||||
89 | function concat() |
||||
90 | { |
||||
91 | $concat = function (array $firstArray, array $secondArray) { |
||||
92 | 1 | return array_merge($firstArray, $secondArray); |
|||
93 | 1 | }; |
|||
94 | 1 | $arguments = func_get_args(); |
|||
95 | 1 | $curriedAny = curryN($concat, 2); |
|||
96 | 1 | return call_user_func_array($curriedAny, $arguments); |
|||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * a → [a] → Boolean |
||||
101 | * @param mixed $item item to compare against |
||||
102 | * @param array $array The array to consider |
||||
103 | * @return boolean|\Closure |
||||
104 | */ |
||||
105 | function contains() |
||||
106 | { |
||||
107 | 2 | $contains = partialRight('in_array', [true]); |
|||
108 | 2 | $arguments = func_get_args(); |
|||
109 | 2 | $curriedAny = curryN($contains, 2); |
|||
110 | 2 | return call_user_func_array($curriedAny, $arguments); |
|||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * @param int $index |
||||
115 | * @param array $array |
||||
116 | * @return mixed |
||||
117 | */ |
||||
118 | function drop() |
||||
119 | { |
||||
120 | $drop = function (int $index, array $array) { |
||||
121 | 12 | return array_merge(array_slice($array, 0, $index), array_slice($array, $index + 1)); |
|||
122 | 12 | }; |
|||
123 | 12 | $arguments = func_get_args(); |
|||
124 | 12 | $curried = curryN($drop, 2); |
|||
125 | 12 | return call_user_func_array($curried, $arguments); |
|||
126 | } |
||||
127 | |||||
128 | /** |
||||
129 | * @param array $array |
||||
130 | * @return mixed |
||||
131 | */ |
||||
132 | function dropFirst() |
||||
133 | { |
||||
134 | 2 | $dropFirst = drop(0); |
|||
135 | 2 | $arguments = func_get_args(); |
|||
136 | 2 | $curried = curryN($dropFirst, 1); |
|||
137 | 2 | return call_user_func_array($curried, $arguments); |
|||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * @param array $array |
||||
142 | * @return mixed |
||||
143 | */ |
||||
144 | function dropLast() |
||||
145 | { |
||||
146 | $dropLast = function (array $array) { |
||||
147 | 1 | $index = count($array)-1; |
|||
148 | 1 | return array_merge(array_slice($array, 0, $index), array_slice($array, $index + 1)); |
|||
149 | 1 | }; |
|||
150 | |||||
151 | 1 | $arguments = func_get_args(); |
|||
152 | 1 | $curried = curryN($dropLast, 1); |
|||
153 | 1 | return call_user_func_array($curried, $arguments); |
|||
154 | } |
||||
155 | |||||
156 | /** |
||||
157 | * @param string|array $suffix |
||||
158 | * @param string|array $list |
||||
159 | * @return boolean|\Closure |
||||
160 | */ |
||||
161 | function endsWith() |
||||
162 | { |
||||
163 | $endsWith = function ($suffix, $list) { |
||||
164 | 7 | if (is_string($suffix) && is_string($list)) { |
|||
165 | 3 | return $suffix === '' || (($temp = strlen($list) - strlen($suffix)) >= 0 && strpos($list, $suffix) !== false); |
|||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||||
166 | } |
||||
167 | 4 | if (is_array($suffix) && is_array($list)) { |
|||
168 | 3 | $index = count($list) - count($suffix); |
|||
169 | 3 | foreach ($suffix as $it) { |
|||
170 | 2 | if ($it != $list[$index]) { |
|||
171 | 1 | return false; |
|||
172 | } |
||||
173 | 1 | $index++; |
|||
174 | } |
||||
175 | 2 | return true; |
|||
176 | } |
||||
177 | 1 | throw new \InvalidArgumentException(__FUNCTION__ . 'accepts only string or array as it arguments'); |
|||
178 | 7 | }; |
|||
179 | 7 | $arguments = func_get_args(); |
|||
180 | 7 | $curried = curryN($endsWith, 2); |
|||
181 | 7 | return call_user_func_array($curried, $arguments); |
|||
182 | } |
||||
183 | |||||
184 | /** |
||||
185 | * @param callable $predicateFunction |
||||
186 | * @param array $array |
||||
187 | * @return mixed |
||||
188 | */ |
||||
189 | function filter() |
||||
190 | { |
||||
191 | $filter = function (callable $predicateFunction, array $array) { |
||||
192 | 1 | return array_values(array_filter($array, $predicateFunction)); |
|||
193 | 1 | }; |
|||
194 | 1 | $arguments = func_get_args(); |
|||
195 | 1 | $curried = curryN($filter, 2); |
|||
196 | 1 | return call_user_func_array($curried, $arguments); |
|||
197 | } |
||||
198 | |||||
199 | /** |
||||
200 | * @param callable $predicateFunction |
||||
201 | * @param array $array |
||||
202 | * @return mixed |
||||
203 | */ |
||||
204 | function filterPreserveKey() |
||||
205 | { |
||||
206 | $filter = function (callable $predicateFunction, array $array) { |
||||
207 | 1 | return array_filter($array, $predicateFunction); |
|||
208 | 1 | }; |
|||
209 | 1 | $arguments = func_get_args(); |
|||
210 | 1 | $curried = curryN($filter, 2); |
|||
211 | 1 | return call_user_func_array($curried, $arguments); |
|||
212 | } |
||||
213 | |||||
214 | /** |
||||
215 | * @param callable $predicateFunction |
||||
216 | * @param array $list |
||||
217 | * @return mixed |
||||
218 | */ |
||||
219 | function find() |
||||
220 | { |
||||
221 | $find = function (callable $predicateFunction, array $list) { |
||||
222 | 2 | foreach ($list as $item) { |
|||
223 | 2 | if ($predicateFunction($item)) { |
|||
224 | 2 | return $item; |
|||
225 | } |
||||
226 | } |
||||
227 | 1 | return null; |
|||
228 | 2 | }; |
|||
229 | 2 | $arguments = func_get_args(); |
|||
230 | 2 | $curried = curryN($find, 2); |
|||
231 | 2 | return call_user_func_array($curried, $arguments); |
|||
232 | } |
||||
233 | |||||
234 | function first() |
||||
235 | { |
||||
236 | $first = function (array $array) { |
||||
237 | 2 | if (empty($array)) { |
|||
238 | 1 | return null; |
|||
239 | } |
||||
240 | 1 | return $array[0]; |
|||
241 | 2 | }; |
|||
242 | 2 | $arguments = func_get_args(); |
|||
243 | 2 | $curried = curryN($first, 1); |
|||
244 | 2 | return call_user_func_array($curried, $arguments); |
|||
245 | } |
||||
246 | |||||
247 | /** |
||||
248 | * @param array $array |
||||
249 | * @return array|\Closure |
||||
250 | */ |
||||
251 | function flatten() |
||||
252 | { |
||||
253 | $flatten = function (array $array) use (&$flatten) { |
||||
254 | $accumulator = function ($acc, $item) use ($flatten) { |
||||
255 | 1 | if (is_array($item)) { |
|||
256 | 1 | return array_merge($acc, $flatten($item)); |
|||
257 | } |
||||
258 | 1 | $acc[] = $item; |
|||
259 | 1 | return $acc; |
|||
260 | 1 | }; |
|||
261 | 1 | return array_reduce($array, $accumulator, []); |
|||
262 | 1 | }; |
|||
263 | 1 | $arguments = func_get_args(); |
|||
264 | 1 | $curried = curryN($flatten, 1); |
|||
265 | 1 | return call_user_func_array($curried, $arguments); |
|||
266 | } |
||||
267 | |||||
268 | /** |
||||
269 | * @param callable $keySelector |
||||
270 | * @param array $array |
||||
271 | * @return array |
||||
272 | */ |
||||
273 | function groupBy() |
||||
274 | { |
||||
275 | $groupBy = function (callable $keySelector, array $array) { |
||||
276 | $accumulator = function ($acc, $item) use ($keySelector) { |
||||
277 | 2 | $key = $keySelector($item); |
|||
278 | 2 | $acc[$key] = array_key_exists($key, $acc) ? array_merge($acc[$key], [$item]) : [$item]; |
|||
279 | 2 | return $acc; |
|||
280 | 2 | }; |
|||
281 | 2 | return array_reduce($array, $accumulator, []); |
|||
282 | 2 | }; |
|||
283 | 2 | $arguments = func_get_args(); |
|||
284 | 2 | $curried = curryN($groupBy, 2); |
|||
285 | 2 | return call_user_func_array($curried, $arguments); |
|||
286 | } |
||||
287 | |||||
288 | /** |
||||
289 | * @param int $index |
||||
290 | * @param mixed $element |
||||
291 | * @param array $array |
||||
292 | * @return array|\Closure |
||||
293 | */ |
||||
294 | function insert() |
||||
295 | { |
||||
296 | $insert = function (int $index, $element, array $array) { |
||||
297 | 3 | return array_merge( |
|||
298 | 3 | array_slice($array, 0, $index), |
|||
299 | 3 | [$element], |
|||
300 | 3 | array_slice($array, $index) |
|||
301 | ); |
||||
302 | 3 | }; |
|||
303 | 3 | $arguments = func_get_args(); |
|||
304 | 3 | $curried = curryN($insert, 3); |
|||
305 | 3 | return call_user_func_array($curried, $arguments); |
|||
306 | } |
||||
307 | |||||
308 | /** |
||||
309 | * @param string $string |
||||
310 | * @param array $array |
||||
311 | * @return string |
||||
312 | */ |
||||
313 | function join() |
||||
314 | { |
||||
315 | 1 | $arguments = func_get_args(); |
|||
316 | 1 | $curried = curryN('implode', 2); |
|||
317 | 1 | return call_user_func_array($curried, $arguments); |
|||
318 | } |
||||
319 | |||||
320 | function last() |
||||
321 | { |
||||
322 | $last = function (array $array) { |
||||
323 | 2 | if (empty($array)) { |
|||
324 | 1 | return null; |
|||
325 | } |
||||
326 | 1 | return $array[count($array) - 1]; |
|||
327 | 2 | }; |
|||
328 | 2 | $arguments = func_get_args(); |
|||
329 | 2 | $curried = curryN($last, 1); |
|||
330 | 2 | return call_user_func_array($curried, $arguments); |
|||
331 | } |
||||
332 | |||||
333 | function length() |
||||
334 | { |
||||
335 | 1 | $arguments = func_get_args(); |
|||
336 | 1 | $curried = curryN('count', 1); |
|||
337 | 1 | return call_user_func_array($curried, $arguments); |
|||
338 | } |
||||
339 | |||||
340 | /** |
||||
341 | * @param callable $mapper |
||||
342 | * @param array $array |
||||
343 | * @return mixed |
||||
344 | */ |
||||
345 | function map() |
||||
346 | { |
||||
347 | 1 | $arguments = func_get_args(); |
|||
348 | 1 | $curried = curryN('array_map', 2); |
|||
349 | 1 | return call_user_func_array($curried, $arguments); |
|||
350 | } |
||||
351 | |||||
352 | /** |
||||
353 | * @param mixed $firstValue |
||||
354 | * @param mixed $secondValue |
||||
355 | * @return array|\Closure |
||||
356 | */ |
||||
357 | function pair() |
||||
358 | { |
||||
359 | $pair = function ($firstValue, $secondValue) { |
||||
360 | 1 | return [$firstValue, $secondValue]; |
|||
361 | 1 | }; |
|||
362 | 1 | $arguments = func_get_args(); |
|||
363 | 1 | $curried = curryN($pair, 2); |
|||
364 | 1 | return call_user_func_array($curried, $arguments); |
|||
365 | } |
||||
366 | |||||
367 | /** |
||||
368 | * a → [a] → [a] |
||||
369 | * @param mixed $item |
||||
370 | * @param array $array |
||||
371 | * @return array|\Closure |
||||
372 | */ |
||||
373 | function prepend() |
||||
374 | { |
||||
375 | $prepend = function ($item, array $array) { |
||||
376 | 2 | return array_merge([$item], $array); |
|||
377 | 2 | }; |
|||
378 | 2 | $arguments = func_get_args(); |
|||
379 | 2 | $curriedAny = curryN($prepend, 2); |
|||
380 | 2 | return call_user_func_array($curriedAny, $arguments); |
|||
381 | } |
||||
382 | |||||
383 | /** |
||||
384 | * Returns a list of numbers from from (inclusive) to to (inclusive). |
||||
385 | * @param int|float|double $from |
||||
386 | * @param int|float|double $to |
||||
387 | * @return array|\Closure |
||||
388 | */ |
||||
389 | function range() |
||||
390 | { |
||||
391 | 3 | $arguments = func_get_args(); |
|||
392 | 3 | $curriedAny = curryN('range', 2); |
|||
393 | 3 | return call_user_func_array($curriedAny, $arguments); |
|||
394 | } |
||||
395 | |||||
396 | /** |
||||
397 | * @param callable $accumulator |
||||
398 | * @param mixed $initialValue |
||||
399 | * @param array $array |
||||
400 | * @return mixed |
||||
401 | */ |
||||
402 | function reduce() |
||||
403 | { |
||||
404 | $reduce = function (callable $accumulator, $initialValue, array $array) { |
||||
405 | 12 | return array_reduce($array, $accumulator, $initialValue); |
|||
406 | 12 | }; |
|||
407 | 12 | $arguments = func_get_args(); |
|||
408 | 12 | $curried = curryN($reduce, 3); |
|||
409 | 12 | return call_user_func_array($curried, $arguments); |
|||
410 | } |
||||
411 | |||||
412 | /** |
||||
413 | * @param callable $predicate |
||||
414 | * @param array $array |
||||
415 | * @return array|\Closure |
||||
416 | */ |
||||
417 | function reject() |
||||
418 | { |
||||
419 | $reject = function ($predicate, array $array) { |
||||
420 | 1 | return array_values(array_filter($array, compose(not(), $predicate))); |
|||
421 | 1 | }; |
|||
422 | 1 | $arguments = func_get_args(); |
|||
423 | 1 | $curried = curryN($reject, 2); |
|||
424 | 1 | return call_user_func_array($curried, $arguments); |
|||
425 | } |
||||
426 | |||||
427 | /** |
||||
428 | * @param callable $predicate |
||||
429 | * @param array $array |
||||
430 | * @return array|\Closure |
||||
431 | */ |
||||
432 | function rejectPreserveKey() |
||||
433 | { |
||||
434 | $reject = function ($predicate, array $array) { |
||||
435 | 1 | return array_filter($array, compose(not(), $predicate)); |
|||
436 | 1 | }; |
|||
437 | 1 | $arguments = func_get_args(); |
|||
438 | 1 | $curried = curryN($reject, 2); |
|||
439 | 1 | return call_user_func_array($curried, $arguments); |
|||
440 | } |
||||
441 | |||||
442 | /** |
||||
443 | * Number → Number → [a] → [a] |
||||
444 | * @param int $start |
||||
445 | * @param int $count |
||||
446 | * @param array $array |
||||
447 | * @return mixed |
||||
448 | */ |
||||
449 | function remove() |
||||
450 | { |
||||
451 | $remove = function (int $start, int $count, array $array) { |
||||
452 | 1 | return array_merge(array_slice($array, 0, $start), array_slice($array, $start + $count)); |
|||
453 | 1 | }; |
|||
454 | 1 | $arguments = func_get_args(); |
|||
455 | 1 | $curried = curryN($remove, 3); |
|||
456 | 1 | return call_user_func_array($curried, $arguments); |
|||
457 | } |
||||
458 | |||||
459 | /** |
||||
460 | * @param mixed $item |
||||
461 | * @param int $count |
||||
462 | * @return array|\Closure |
||||
463 | */ |
||||
464 | function repeat() |
||||
465 | { |
||||
466 | $repeat = function ($item, int $count) { |
||||
467 | 1 | return array_reduce( |
|||
468 | 1 | range(1, $count), |
|||
0 ignored issues
–
show
It seems like
range(1, $count) can also be of type Closure ; however, parameter $input of array_reduce() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
469 | function ($acc, $unsedItem) use ($item) { |
||||
0 ignored issues
–
show
The parameter
$unsedItem is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
470 | 1 | return array_merge($acc, [$item]); |
|||
471 | 1 | }, |
|||
472 | 1 | [] |
|||
473 | ); |
||||
474 | 1 | }; |
|||
475 | 1 | $arguments = func_get_args(); |
|||
476 | 1 | $curried = curryN($repeat, 2); |
|||
477 | 1 | return call_user_func_array($curried, $arguments); |
|||
478 | } |
||||
479 | |||||
480 | /** |
||||
481 | * @param array|string $list |
||||
482 | * @return array|string|\Closure |
||||
483 | */ |
||||
484 | function reverse() |
||||
485 | { |
||||
486 | $reverse = function ($list) { |
||||
487 | 5 | if (is_array($list)) { |
|||
488 | 2 | return array_reverse($list); |
|||
489 | } |
||||
490 | 3 | if (is_string($list)) { |
|||
491 | 2 | return strrev($list); |
|||
492 | } |
||||
493 | 1 | throw new \InvalidArgumentException('reverse() only support string or array as an argument'); |
|||
494 | 5 | }; |
|||
495 | 5 | $arguments = func_get_args(); |
|||
496 | 5 | $curried = curryN($reverse, 1); |
|||
497 | 5 | return call_user_func_array($curried, $arguments); |
|||
498 | } |
||||
499 | |||||
500 | /** |
||||
501 | * @param int $fromIndex |
||||
502 | * @param int $toIndex |
||||
503 | * @param mixed $list |
||||
504 | * @return array|string|\Closure |
||||
505 | */ |
||||
506 | function slice() |
||||
507 | { |
||||
508 | $slice = function (int $fromIndex, int $toIndex, $list) { |
||||
509 | 8 | if (is_array($list)) { |
|||
510 | 4 | $listCount = count($list); |
|||
511 | 4 | $actualFromIndex = $fromIndex < 0 ? $listCount + $fromIndex : $fromIndex; |
|||
512 | 4 | $actualToIndex = $toIndex < 0 ? $listCount + $toIndex : $toIndex; |
|||
513 | 4 | return array_slice($list, $actualFromIndex, $actualToIndex - $actualFromIndex); |
|||
514 | } |
||||
515 | 4 | if (is_string($list)) { |
|||
516 | 4 | $listCount = strlen($list); |
|||
517 | 4 | $actualFromIndex = $fromIndex < 0 ? $listCount + $fromIndex : $fromIndex; |
|||
518 | 4 | $actualToIndex = $toIndex < 0 ? $listCount + $toIndex : $toIndex; |
|||
519 | 4 | return substr($list, $actualFromIndex, $actualToIndex - $actualFromIndex); |
|||
520 | } |
||||
521 | throw new \InvalidArgumentException('slice() only support array and string'); |
||||
522 | 8 | }; |
|||
523 | 8 | $arguments = func_get_args(); |
|||
524 | 8 | $curried = curryN($slice, 3); |
|||
525 | 8 | return call_user_func_array($curried, $arguments); |
|||
526 | } |
||||
527 | |||||
528 | /** |
||||
529 | * @param callable $comparator |
||||
530 | * @param array $array |
||||
531 | * @return array|\Closure |
||||
532 | */ |
||||
533 | function sort() |
||||
534 | { |
||||
535 | $sort = function (callable $comparator, array $array) { |
||||
536 | 1 | usort($array, $comparator); |
|||
537 | 1 | return $array; |
|||
538 | 1 | }; |
|||
539 | 1 | $arguments = func_get_args(); |
|||
540 | 1 | $curried = curryN($sort, 2); |
|||
541 | 1 | return call_user_func_array($curried, $arguments); |
|||
542 | } |
||||
543 | |||||
544 | /** |
||||
545 | * @param callable $keySelector |
||||
546 | * @param array $array |
||||
547 | * @return array|\Closure |
||||
548 | */ |
||||
549 | function sortBy() |
||||
550 | { |
||||
551 | $sortBy = function (callable $keySelector, array $array) { |
||||
552 | 1 | $groupedArray = groupBy($keySelector, $array); |
|||
553 | 1 | ksort($groupedArray); |
|||
554 | 1 | return apply('array_merge', array_values($groupedArray)); |
|||
555 | 1 | }; |
|||
556 | 1 | $arguments = func_get_args(); |
|||
557 | 1 | $curried = curryN($sortBy, 2); |
|||
558 | 1 | return call_user_func_array($curried, $arguments); |
|||
559 | } |
||||
560 | |||||
561 | /** |
||||
562 | * @param string|array $prefix |
||||
563 | * @param string|array $list |
||||
564 | * @return boolean|\Closure |
||||
565 | */ |
||||
566 | function startsWith($prefix = null, $list = null) |
||||
567 | { |
||||
568 | $startsWith = function ($prefix, $list) { |
||||
569 | 7 | if (is_string($prefix) && is_string($list)) { |
|||
570 | 3 | return $prefix === '' || strpos($list, $prefix) === 0; |
|||
571 | } |
||||
572 | 4 | if (is_array($prefix) && is_array($list)) { |
|||
573 | 3 | foreach ($prefix as $key => $it) { |
|||
574 | 2 | if ($it != $list[$key]) { |
|||
575 | 2 | return false; |
|||
576 | }; |
||||
577 | } |
||||
578 | 2 | return true; |
|||
579 | } |
||||
580 | 1 | throw new \InvalidArgumentException(__FUNCTION__ . 'accepts only string or array as it arguments'); |
|||
581 | 7 | }; |
|||
582 | 7 | $arguments = func_get_args(); |
|||
583 | 7 | $curried = curryN($startsWith, 2); |
|||
584 | 7 | return call_user_func_array($curried, $arguments); |
|||
585 | } |
||||
586 | |||||
587 | function tail() |
||||
588 | { |
||||
589 | $tail = function (array $array) { |
||||
590 | 2 | if (empty($array)) { |
|||
591 | 1 | return null; |
|||
592 | } |
||||
593 | 1 | return $array[count($array) - 1]; |
|||
594 | 2 | }; |
|||
595 | 2 | $arguments = func_get_args(); |
|||
596 | 2 | $curried = curryN($tail, 1); |
|||
597 | 2 | return call_user_func_array($curried, $arguments); |
|||
598 | } |
||||
599 |