1
|
|
|
<?php namespace Tarsana\Functional; |
2
|
|
|
/** |
3
|
|
|
* Useful functions to handle lists (arrays having only numeric keys). |
4
|
|
|
* @file |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Curried version of `array_map`. |
9
|
|
|
* |
10
|
|
|
* ```php |
11
|
|
|
* $doubles = F\map(function($x) { return 2 * $x; }); |
12
|
|
|
* $doubles([1, 2, 3, 4]); //=> [2, 4, 6, 8] |
13
|
|
|
* ``` |
14
|
|
|
* |
15
|
|
|
* @stream |
16
|
|
|
* @signature (a -> b) -> [a] -> [b] |
17
|
|
|
* @signature (a -> b) -> {k: a} -> {k: b} |
18
|
|
|
* @param callable $fn |
|
|
|
|
19
|
|
|
* @param array $list |
|
|
|
|
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
function map() { |
23
|
|
|
static $map = false; |
24
|
|
|
$map = $map ?: curry('array_map'); |
25
|
|
|
return _apply($map, func_get_args()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Applies a function to items of the array and concatenates the results. |
30
|
|
|
* |
31
|
|
|
* This is also known as `flatMap` in some libraries. |
32
|
|
|
* ```php |
33
|
|
|
* $words = F\chain(F\split(' ')); |
34
|
|
|
* $words(['Hello World', 'How are you']); //=> ['Hello', 'World', 'How', 'are', 'you'] |
35
|
|
|
* ``` |
36
|
|
|
* |
37
|
|
|
* @stream |
38
|
|
|
* @signature (a -> [b]) -> [a] -> [b] |
39
|
|
|
* @param callable $fn |
|
|
|
|
40
|
|
|
* @param array $list |
|
|
|
|
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
function chain() { |
|
|
|
|
44
|
|
|
static $chain = false; |
45
|
|
|
$chain = $chain ?: curry(function($fn, $list) { |
46
|
|
|
$result = []; |
47
|
|
|
foreach ($list as $item) { |
48
|
|
|
$result = array_merge($result, $fn($item)); |
49
|
|
|
} |
50
|
|
|
return $result; |
51
|
|
|
}); |
52
|
|
|
return _apply($chain, func_get_args()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Curried version of `array_filter` with modified order of arguments. |
57
|
|
|
* |
58
|
|
|
* The callback is the first argument then the list. |
59
|
|
|
* ```php |
60
|
|
|
* $list = [1, 'aa', 3, [4, 5]]; |
61
|
|
|
* $numeric = F\filter('is_numeric'); |
62
|
|
|
* $numeric($list); //=> [1, 3] |
63
|
|
|
* ``` |
64
|
|
|
* @stream |
65
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
66
|
|
|
* @param callable $fn |
|
|
|
|
67
|
|
|
* @param array $list |
|
|
|
|
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
function filter() { |
|
|
|
|
71
|
|
|
static $filter = false; |
72
|
|
|
$filter = $filter ?: curry(function($fn, $list) { |
73
|
|
|
$result = []; |
74
|
|
|
foreach ($list as $item) { |
75
|
|
|
if ($fn($item)) |
76
|
|
|
$result[] = $item; |
77
|
|
|
} |
78
|
|
|
return $result; |
79
|
|
|
}); |
80
|
|
|
return _apply($filter, func_get_args()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Curried version of `array_reduce` with modified order of |
85
|
|
|
* arguments ($callback, $initial, $list). |
86
|
|
|
* |
87
|
|
|
* ```php |
88
|
|
|
* $list = [1, 2, 3, 4]; |
89
|
|
|
* $sum = F\reduce('Tarsana\Functional\plus', 0); |
90
|
|
|
* $sum($list); //=> 10 |
91
|
|
|
* ``` |
92
|
|
|
* |
93
|
|
|
* @stream |
94
|
|
|
* @signature (* -> a -> *) -> * -> [a] -> * |
95
|
|
|
* @param callable $fn |
|
|
|
|
96
|
|
|
* @param mixed $initial |
|
|
|
|
97
|
|
|
* @param array $list |
|
|
|
|
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
function reduce() { |
101
|
|
|
static $reduce = false; |
102
|
|
|
$reduce = $reduce ?: curry(function($fn, $initial, $list){ |
103
|
|
|
return array_reduce($list, $fn, $initial); |
104
|
|
|
}); |
105
|
|
|
return _apply($reduce, func_get_args()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Applies the callback to each item and returns the original list. |
110
|
|
|
* |
111
|
|
|
* ```php |
112
|
|
|
* $list = [1, 2, 3, 4]; |
113
|
|
|
* $s = 0; |
114
|
|
|
* F\each(function($item) use(&$s){ |
115
|
|
|
* $s += $item; |
116
|
|
|
* }, $list); |
117
|
|
|
* |
118
|
|
|
* $s; //=> 10 |
119
|
|
|
* ``` |
120
|
|
|
* |
121
|
|
|
* @stream |
122
|
|
|
* @signature (a -> *) -> [a] -> [a] |
123
|
|
|
* @param callable $fn |
|
|
|
|
124
|
|
|
* @param array $list |
|
|
|
|
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
function each() { |
128
|
|
|
static $each = false; |
129
|
|
|
$each = $each ?: curry(function($fn, $list){ |
130
|
|
|
foreach ($list as $item) { |
131
|
|
|
$fn($item); |
132
|
|
|
} |
133
|
|
|
return $list; |
134
|
|
|
}); |
135
|
|
|
return _apply($each, func_get_args()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the first item of the given array or string. |
140
|
|
|
* |
141
|
|
|
* ```php |
142
|
|
|
* F\head([1, 2, 3, 4]); //=> 1 |
143
|
|
|
* F\head('Hello'); //=> 'H' |
144
|
|
|
* F\head([]); //=> null |
145
|
|
|
* F\head(''); //=> '' |
146
|
|
|
* ``` |
147
|
|
|
* |
148
|
|
|
* @stream |
149
|
|
|
* @signature [a] -> a |
150
|
|
|
* @signature String -> String |
151
|
|
|
* @param array|string $list |
|
|
|
|
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
function head() { |
155
|
|
|
static $head = false; |
156
|
|
|
$head = $head ?: curry(function($list) { |
157
|
|
|
if (isset($list[0])) return $list[0]; |
158
|
|
|
return is_string($list) ? '' : null; |
159
|
|
|
}); |
160
|
|
|
return _apply($head, func_get_args()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the last item of the given array or string. |
165
|
|
|
* |
166
|
|
|
* ```php |
167
|
|
|
* F\last([1, 2, 3, 4]); //=> 4 |
168
|
|
|
* F\last('Hello'); //=> 'o' |
169
|
|
|
* F\last([]); //=> null |
170
|
|
|
* F\last(''); //=> '' |
171
|
|
|
* ``` |
172
|
|
|
* |
173
|
|
|
* @stream |
174
|
|
|
* @signature [a] -> a |
175
|
|
|
* @signature String -> String |
176
|
|
|
* @param array|string $list |
|
|
|
|
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
function last () { |
180
|
|
|
static $last = false; |
181
|
|
|
$last = $last ?: curry(function($list) { |
182
|
|
|
if(is_string($list)) |
183
|
|
|
return substr($list, -1); |
184
|
|
|
$size = count($list); |
185
|
|
|
return ($size > 0) |
186
|
|
|
? $list[$size - 1] |
187
|
|
|
: null; |
188
|
|
|
}); |
189
|
|
|
return _apply($last, func_get_args()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns all but the last element of the given array or string. |
194
|
|
|
* |
195
|
|
|
* ```php |
196
|
|
|
* F\init([1, 2, 3, 4]); //=> [1, 2, 3] |
197
|
|
|
* F\init('Hello'); //=> 'Hell' |
198
|
|
|
* F\init([7]); //=> [] |
199
|
|
|
* F\init([]); //=> [] |
200
|
|
|
* F\init(''); //=> '' |
201
|
|
|
* ``` |
202
|
|
|
* |
203
|
|
|
* @stream |
204
|
|
|
* @signature [a] -> a |
205
|
|
|
* @signature String -> String |
206
|
|
|
* @param array|string $list |
|
|
|
|
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
function init () { |
210
|
|
|
static $init = false; |
211
|
|
|
$init = $init ?: curry(function($list) { |
212
|
|
View Code Duplication |
if(is_string($list)) { |
|
|
|
|
213
|
|
|
$size = strlen($list); |
214
|
|
|
return ($size > 1) |
215
|
|
|
? substr($list, 0, $size - 1) |
216
|
|
|
: ''; |
217
|
|
|
} |
218
|
|
|
$size = count($list); |
219
|
|
|
return ($size > 1) |
220
|
|
|
? array_slice($list, 0, $size - 1) |
221
|
|
|
: []; |
222
|
|
|
}); |
223
|
|
|
return _apply($init, func_get_args()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns all but the first element of the given array or string. |
228
|
|
|
* |
229
|
|
|
* ```php |
230
|
|
|
* F\tail([1, 2, 3, 4]); //=> [2, 3, 4] |
231
|
|
|
* F\tail('Hello'); //=> 'ello' |
232
|
|
|
* F\tail([7]); //=> [] |
233
|
|
|
* F\tail([]); //=> [] |
234
|
|
|
* F\tail(''); //=> '' |
235
|
|
|
* ``` |
236
|
|
|
* |
237
|
|
|
* @stream |
238
|
|
|
* @signature [a] -> a |
239
|
|
|
* @signature String -> String |
240
|
|
|
* @param array|string $list |
|
|
|
|
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
|
|
function tail () { |
244
|
|
|
static $tail = false; |
245
|
|
|
$tail = $tail ?: curry(function($list) { |
246
|
|
|
if(is_string($list)) |
247
|
|
|
return (strlen($list) > 1) |
248
|
|
|
? substr($list, 1) |
249
|
|
|
: ''; |
250
|
|
|
return (count($list) > 1) |
251
|
|
|
? array_slice($list, 1) |
252
|
|
|
: []; |
253
|
|
|
}); |
254
|
|
|
return _apply($tail, func_get_args()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Alias of `array_reverse()` and `strrev()`. |
259
|
|
|
* |
260
|
|
|
* ```php |
261
|
|
|
* F\reverse([1, 2, 3, 4]); //=> [4, 3, 2, 1] |
262
|
|
|
* F\reverse('Hello'); //=> 'olleH' |
263
|
|
|
* ``` |
264
|
|
|
* |
265
|
|
|
* @stream |
266
|
|
|
* @signature [a] -> [a] |
267
|
|
|
* @signature String -> String |
268
|
|
|
* @param array|string $list |
|
|
|
|
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
function reverse () { |
272
|
|
|
static $reverse = false; |
273
|
|
|
$reverse = $reverse ?: curry(function($list) { |
274
|
|
|
return is_string($list) |
275
|
|
|
? strrev($list) |
276
|
|
|
: array_reverse($list); |
277
|
|
|
}); |
278
|
|
|
return _apply($reverse, func_get_args()); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Alias for `count()` and `strlen()`. |
283
|
|
|
* |
284
|
|
|
* ```php |
285
|
|
|
* F\length([1, 2, 3, 4]); //=> 4 |
286
|
|
|
* F\length('Hello'); //=> 5 |
287
|
|
|
* ``` |
288
|
|
|
* |
289
|
|
|
* @stream |
290
|
|
|
* @signature [a] -> Number |
291
|
|
|
* @signature String -> Number |
292
|
|
|
* @param array|string $list |
|
|
|
|
293
|
|
|
* @return int |
294
|
|
|
*/ |
295
|
|
|
function length() { |
296
|
|
|
static $length = false; |
297
|
|
|
$length = $length ?: curry(function($list) { |
298
|
|
|
return is_string($list) |
299
|
|
|
? strlen($list) |
300
|
|
|
: count($list); |
301
|
|
|
}); |
302
|
|
|
return _apply($length, func_get_args()); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Checks if the `$predicate` is verified by **all** items of the array. |
307
|
|
|
* |
308
|
|
|
* ```php |
309
|
|
|
* $allNotNull = F\allSatisfies(F\pipe(F\eq(0), F\not())); |
310
|
|
|
* $allNotNull([9, 3, 2, 4]); //=> true |
311
|
|
|
* $allNotNull([9, 3, 0, 4]); //=> false |
312
|
|
|
* ``` |
313
|
|
|
* |
314
|
|
|
* @stream |
315
|
|
|
* @signature (a -> Boolean) -> [a] -> Boolean |
316
|
|
|
* @param callable $predicate |
|
|
|
|
317
|
|
|
* @param array $list |
|
|
|
|
318
|
|
|
* @return bool |
319
|
|
|
*/ |
320
|
|
View Code Duplication |
function allSatisfies() { |
|
|
|
|
321
|
|
|
static $allSatisfies = false; |
322
|
|
|
$allSatisfies = $allSatisfies ?: curry(function($predicate, $list) { |
323
|
|
|
foreach ($list as $item) { |
324
|
|
|
if (! $predicate($item)) |
325
|
|
|
return false; |
326
|
|
|
} |
327
|
|
|
return true; |
328
|
|
|
}); |
329
|
|
|
return _apply($allSatisfies, func_get_args()); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Checks if the `$predicate` is verified by **any** item of the array. |
334
|
|
|
* |
335
|
|
|
* ```php |
336
|
|
|
* $anyNumeric = F\anySatisfies('is_numeric'); |
337
|
|
|
* $anyNumeric(['Hello', '12', []]); //=> true |
338
|
|
|
* $anyNumeric(['Hello', 'Foo']); //=> false |
339
|
|
|
* ``` |
340
|
|
|
* |
341
|
|
|
* @stream |
342
|
|
|
* @signature (a -> Boolean) -> [a] -> Boolean |
343
|
|
|
* @param callable $predicate |
|
|
|
|
344
|
|
|
* @param array $list |
|
|
|
|
345
|
|
|
* @return bool |
346
|
|
|
*/ |
347
|
|
View Code Duplication |
function anySatisfies() { |
|
|
|
|
348
|
|
|
static $anySatisfies = false; |
349
|
|
|
$anySatisfies = $anySatisfies ?: curry(function($predicate, $list) { |
350
|
|
|
foreach ($list as $item) { |
351
|
|
|
if ($predicate($item)) |
352
|
|
|
return true; |
353
|
|
|
} |
354
|
|
|
return false; |
355
|
|
|
}); |
356
|
|
|
return _apply($anySatisfies, func_get_args()); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Concatenates two arrays or strings. |
361
|
|
|
* |
362
|
|
|
* ```php |
363
|
|
|
* F\concat([1, 2], [3, 4]); //=> [1, 2, 3, 4] |
364
|
|
|
* F\concat('Hello ', 'World'); //=> 'Hello World' |
365
|
|
|
* ``` |
366
|
|
|
* |
367
|
|
|
* @stream |
368
|
|
|
* @signature [*] -> [*] -> [*] |
369
|
|
|
* @signature String -> String -> String |
370
|
|
|
* @param array $list1 |
|
|
|
|
371
|
|
|
* @param array $list2 |
|
|
|
|
372
|
|
|
* @return array |
373
|
|
|
*/ |
374
|
|
|
function concat() { |
375
|
|
|
static $concat = false; |
376
|
|
|
$concat = $concat ?: curry(function($list1, $list2) { |
377
|
|
|
if (is_string($list1) && is_string($list2)) |
378
|
|
|
return $list1 . $list2; |
379
|
|
|
return array_merge($list1, $list2); |
380
|
|
|
}); |
381
|
|
|
return _apply($concat, func_get_args()); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Concatenates a list of arrays or strings. |
386
|
|
|
* |
387
|
|
|
* ```php |
388
|
|
|
* F\concatAll([[1, 2], [3, 4], [5, 6]]); //=> [1, 2, 3, 4, 5, 6] |
389
|
|
|
* F\concatAll(['Hello ', 'World', ' !']); //=> 'Hello World !' |
390
|
|
|
* ``` |
391
|
|
|
* |
392
|
|
|
* @stream |
393
|
|
|
* @signature [[a]] -> [a] |
394
|
|
|
* @param array $lists |
|
|
|
|
395
|
|
|
* @return array |
396
|
|
|
*/ |
397
|
|
|
function concatAll() { |
398
|
|
|
static $concatAll = false; |
399
|
|
|
$concatAll = $concatAll ?: curry(function($lists) { |
400
|
|
|
if (count($lists) == 0) |
401
|
|
|
return []; |
402
|
|
|
if (is_string($lists[0])) |
403
|
|
|
return implode('', $lists); |
404
|
|
|
return _apply('array_merge', $lists); |
405
|
|
|
}); |
406
|
|
|
return _apply($concatAll, func_get_args()); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Inserts an item at some position into an array or a substring into a string. |
411
|
|
|
* |
412
|
|
|
* If `$position < 0` the item or substring is inserted before the last `|$position|` elements/characters. |
413
|
|
|
* ```php |
414
|
|
|
* F\insert(2, 'x', [1, 2, 3, 4]); //=> [1, 2, 'x', 3, 4] |
415
|
|
|
* F\insert(-1, 'x', [1, 2, 3, 4]); //=> [1, 2, 3, 'x', 4] |
416
|
|
|
* F\insert(11, 'x', [1, 2, 3, 4]); //=> [1, 2, 3, 4, 'x'] |
417
|
|
|
* F\insert(0, 'x', [1, 2, 3, 4]); //=> ['x', 1, 2, 3, 4] |
418
|
|
|
* F\insert(-11, 'x', [1, 2, 3, 4]); //=> ['x', 1, 2, 3, 4] |
419
|
|
|
* F\insert(32, 'd', 'Hello Worl'); //=> 'Hello World' |
420
|
|
|
* F\insert(3, 'l', 'Helo World'); //=> 'Hello World' |
421
|
|
|
* F\insert(-7, 'l', 'Helo World'); //=> 'Hello World' |
422
|
|
|
* F\insert(0, 'H', 'ello World'); //=> 'Hello World' |
423
|
|
|
* F\insert(-70, 'H', 'ello World'); //=> 'Hello World' |
424
|
|
|
* ``` |
425
|
|
|
* |
426
|
|
|
* @stream |
427
|
|
|
* @signature Number -> a -> [a] -> [a] |
428
|
|
|
* @signature Number -> String -> String -> String |
429
|
|
|
* @param int $position |
|
|
|
|
430
|
|
|
* @param mixed $item |
|
|
|
|
431
|
|
|
* @param array $list |
|
|
|
|
432
|
|
|
* @return array |
433
|
|
|
*/ |
434
|
|
View Code Duplication |
function insert() { |
|
|
|
|
435
|
|
|
static $insert = false; |
436
|
|
|
$insert = $insert ?: curry(function($position, $item, $list) { |
437
|
|
|
return is_string($list) |
438
|
|
|
? insertAll($position, $item, $list) |
439
|
|
|
: insertAll($position, [$item], $list); |
440
|
|
|
}); |
441
|
|
|
return _apply($insert, func_get_args()); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Same as `insert` but inserts an array instead of a single item. |
446
|
|
|
* |
447
|
|
|
* ```php |
448
|
|
|
* F\insertAll(2, ['x', 'y'], [1, 2, 3, 4]); //=> [1, 2, 'x', 'y', 3, 4] |
449
|
|
|
* F\insertAll(-1, ['x', 'y'], [1, 2, 3, 4]); //=> [1, 2, 3, 'x', 'y', 4] |
450
|
|
|
* F\insertAll(11, ['x', 'y'], [1, 2, 3, 4]); //=> [1, 2, 3, 4, 'x', 'y'] |
451
|
|
|
* F\insertAll(0, ['x', 'y'], [1, 2, 3, 4]); //=> ['x', 'y', 1, 2, 3, 4] |
452
|
|
|
* F\insertAll(-11, ['x', 'y'], [1, 2, 3, 4]); //=> ['x', 'y', 1, 2, 3, 4] |
453
|
|
|
* F\insertAll(2, 'llo', 'He World'); //=> 'Hello World' |
454
|
|
|
* ``` |
455
|
|
|
* |
456
|
|
|
* @stream |
457
|
|
|
* @signature Number -> [a] -> [a] -> [a] |
458
|
|
|
* @signature Number -> String -> String -> String |
459
|
|
|
* @param int $position |
|
|
|
|
460
|
|
|
* @param mixed $items |
|
|
|
|
461
|
|
|
* @param array $list |
|
|
|
|
462
|
|
|
* @return array |
463
|
|
|
*/ |
464
|
|
|
function insertAll() { |
465
|
|
|
static $insertAll = false; |
466
|
|
|
$insertAll = $insertAll ?: curry(function($position, $items, $list) { |
467
|
|
|
$size = length($list); |
468
|
|
|
if ($position < 0) |
469
|
|
|
$position = $size + $position; |
470
|
|
|
if ($position < 0) |
471
|
|
|
$position = 0; |
472
|
|
|
if (is_string($list)) |
473
|
|
|
return ($position >= $size) |
474
|
|
|
? $list . $items |
475
|
|
|
: substr($list, 0, $position) . $items . substr($list, $position); |
476
|
|
|
return ($position >= $size) |
477
|
|
|
? array_merge($list, $items) |
478
|
|
|
: array_merge(array_slice($list, 0, $position), $items, array_slice($list, $position)); |
479
|
|
|
}); |
480
|
|
|
return _apply($insertAll, func_get_args()); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Appends an item to an array or a substring to a string. |
485
|
|
|
* |
486
|
|
|
* ```php |
487
|
|
|
* F\append(5, [1, 2, 3]); //=> [1, 2, 3, 5] |
488
|
|
|
* F\append(' World', 'Hello'); //=> 'Hello World' |
489
|
|
|
* ``` |
490
|
|
|
* |
491
|
|
|
* @stream |
492
|
|
|
* @signature * -> [*] -> [*] |
493
|
|
|
* @signature String -> String -> String |
494
|
|
|
* @param mixed $item |
|
|
|
|
495
|
|
|
* @param array $list |
|
|
|
|
496
|
|
|
* @return array |
497
|
|
|
*/ |
498
|
|
|
function append() { |
499
|
|
|
static $append = false; |
500
|
|
|
$append = $append ?: curry(function ($item, $list) { |
501
|
|
|
return is_string($list) |
502
|
|
|
? $list . $item |
503
|
|
|
: array_merge($list, [$item]); |
504
|
|
|
}); |
505
|
|
|
return _apply($append, func_get_args()); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Inserts an item at the begining of an array or a substring at the begining of a string. |
510
|
|
|
* |
511
|
|
|
* Note that this function is equivalent to `insert(0)`. |
512
|
|
|
* ```php |
513
|
|
|
* F\prepend(5, [1, 2, 3]); //=> [5, 1, 2, 3] |
514
|
|
|
* F\prepend('Hello ', 'World'); //=> 'Hello World' |
515
|
|
|
* ``` |
516
|
|
|
* |
517
|
|
|
* @stream |
518
|
|
|
* @signature a -> [a] -> [a] |
519
|
|
|
* @signature String -> String -> String |
520
|
|
|
* @param mixed $item |
|
|
|
|
521
|
|
|
* @param array $list |
|
|
|
|
522
|
|
|
* @return array |
523
|
|
|
*/ |
524
|
|
|
function prepend() { |
525
|
|
|
static $prepend = false; |
526
|
|
|
$prepend = $prepend ?: curry(function ($item, $list) { |
527
|
|
|
return is_string($list) |
528
|
|
|
? $item . $list |
529
|
|
|
: array_merge([$item], $list); |
530
|
|
|
}); |
531
|
|
|
return _apply($prepend, func_get_args()); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Takes a number of elements from an array or a number of characters from a string. |
536
|
|
|
* |
537
|
|
|
* If `$count` is negative, the elements are taken from the end of the array/string. |
538
|
|
|
* ```php |
539
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
540
|
|
|
* F\take(2, $items); //=> ['Foo', 'Bar'] |
541
|
|
|
* F\take(0, $items); //=> [] |
542
|
|
|
* F\take(7, $items); //=> ['Foo', 'Bar', 'Baz'] |
543
|
|
|
* F\take(-2, $items); //=> ['Bar', 'Baz'] |
544
|
|
|
* F\take(5, 'Hello World'); //=> 'Hello' |
545
|
|
|
* F\take(-5, 'Hello World'); //=> 'World' |
546
|
|
|
* ``` |
547
|
|
|
* |
548
|
|
|
* @stream |
549
|
|
|
* @signature Number -> [a] -> [a] |
550
|
|
|
* @signature Number -> String -> String |
551
|
|
|
* @param int $count |
|
|
|
|
552
|
|
|
* @param array $list |
|
|
|
|
553
|
|
|
* @return array |
554
|
|
|
*/ |
555
|
|
|
function take() { |
556
|
|
|
static $take = false; |
557
|
|
|
$take = $take ?: curry(function($count, $list) { |
558
|
|
|
$length = length($list); |
559
|
|
|
if ($count > $length || $count < -$length) |
560
|
|
|
return $list; |
561
|
|
View Code Duplication |
if(is_string($list)) { |
|
|
|
|
562
|
|
|
return ($count >= 0) |
563
|
|
|
? substr($list, 0, $count) |
564
|
|
|
: substr($list, $count); |
565
|
|
|
} |
566
|
|
|
return ($count >= 0) |
567
|
|
|
? array_slice($list, 0, $count) |
568
|
|
|
: array_slice($list, $count); |
569
|
|
|
}); |
570
|
|
|
return _apply($take, func_get_args()); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Takes elements from an array while they match the given predicate. |
575
|
|
|
* |
576
|
|
|
* It stops at the first element not matching the predicate and does not include it in the result. |
577
|
|
|
* ```php |
578
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
579
|
|
|
* F\takeWhile(F\startsWith('F'), $items); //=> ['Foo', 'Fun'] |
580
|
|
|
* F\takeWhile(F\startsWith('D'), $items); //=> [] |
581
|
|
|
* ``` |
582
|
|
|
* |
583
|
|
|
* @stream |
584
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
585
|
|
|
* @param callable $predicate |
|
|
|
|
586
|
|
|
* @param array $list |
|
|
|
|
587
|
|
|
* @return array |
588
|
|
|
*/ |
589
|
|
View Code Duplication |
function takeWhile() { |
|
|
|
|
590
|
|
|
static $takeWhile = false; |
591
|
|
|
$takeWhile = $takeWhile ?: curry(function($predicate, $list) { |
592
|
|
|
$index = 0; |
593
|
|
|
$size = length($list); |
594
|
|
|
while ($index < $size && $predicate($list[$index])) |
595
|
|
|
$index ++; |
596
|
|
|
return array_slice($list, 0, $index); |
597
|
|
|
}); |
598
|
|
|
return _apply($takeWhile, func_get_args()); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Same as `takeWhile` but taking elements from the end of the array. |
603
|
|
|
* |
604
|
|
|
* ```php |
605
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
606
|
|
|
* F\takeLastWhile(F\startsWith('B'), $items); //=> ['Bar', 'Baz'] |
607
|
|
|
* F\takeLastWhile(F\startsWith('D'), $items); //=> [] |
608
|
|
|
* ``` |
609
|
|
|
* |
610
|
|
|
* @stream |
611
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
612
|
|
|
* @param callable $predicate |
|
|
|
|
613
|
|
|
* @param array $list |
|
|
|
|
614
|
|
|
* @return array |
615
|
|
|
*/ |
616
|
|
View Code Duplication |
function takeLastWhile() { |
|
|
|
|
617
|
|
|
static $takeLastWhile = false; |
618
|
|
|
$takeLastWhile = $takeLastWhile ?: curry(function($predicate, $list) { |
619
|
|
|
$index = length($list) - 1; |
620
|
|
|
while ($index >= 0 && $predicate($list[$index])) |
621
|
|
|
$index --; |
622
|
|
|
return array_slice($list, $index + 1); |
623
|
|
|
}); |
624
|
|
|
return _apply($takeLastWhile, func_get_args()); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Takes elements from an array **until** the predicate |
629
|
|
|
* is satisfied, not including the satisfying element. |
630
|
|
|
* |
631
|
|
|
* ```php |
632
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
633
|
|
|
* F\takeUntil(F\startsWith('B'), $items); //=> ['Foo', 'Fun', 'Dev'] |
634
|
|
|
* F\takeUntil(F\startsWith('F'), $items); //=> [] |
635
|
|
|
* ``` |
636
|
|
|
* |
637
|
|
|
* @stream |
638
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
639
|
|
|
* @param callable $predicate |
|
|
|
|
640
|
|
|
* @param array $list |
|
|
|
|
641
|
|
|
* @return array |
642
|
|
|
*/ |
643
|
|
View Code Duplication |
function takeUntil() { |
|
|
|
|
644
|
|
|
static $takeUntil = false; |
645
|
|
|
$takeUntil = $takeUntil ?: curry(function($predicate, $list) { |
646
|
|
|
$index = 0; |
647
|
|
|
$size = length($list); |
648
|
|
|
while ($index < $size && !$predicate($list[$index])) |
649
|
|
|
$index ++; |
650
|
|
|
return array_slice($list, 0, $index); |
651
|
|
|
}); |
652
|
|
|
return _apply($takeUntil, func_get_args()); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Same as `takeUntil` but takes elements from the end of the array. |
657
|
|
|
* |
658
|
|
|
* ```php |
659
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
660
|
|
|
* F\takeLastUntil(F\startsWith('F'), $items); //=> ['Dev', 'Bar', 'Baz'] |
661
|
|
|
* F\takeLastUntil(F\startsWith('B'), $items); //=> [] |
662
|
|
|
* ``` |
663
|
|
|
* |
664
|
|
|
* @stream |
665
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
666
|
|
|
* @param callable $predicate |
|
|
|
|
667
|
|
|
* @param array $list |
|
|
|
|
668
|
|
|
* @return array |
669
|
|
|
*/ |
670
|
|
View Code Duplication |
function takeLastUntil() { |
|
|
|
|
671
|
|
|
static $takeLastUntil = false; |
672
|
|
|
$takeLastUntil = $takeLastUntil ?: curry(function($predicate, $list) { |
673
|
|
|
$index = length($list) - 1; |
674
|
|
|
while ($index >= 0 && !$predicate($list[$index])) |
675
|
|
|
$index --; |
676
|
|
|
return array_slice($list, $index + 1); |
677
|
|
|
}); |
678
|
|
|
return _apply($takeLastUntil, func_get_args()); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* Removes a number of elements from an array. |
683
|
|
|
* |
684
|
|
|
* If `$count` is negative, the elements are |
685
|
|
|
* removed from the end of the array. |
686
|
|
|
* ```php |
687
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
688
|
|
|
* F\remove(2, $items); //=> ['Baz'] |
689
|
|
|
* F\remove(-1, $items); //=> ['Foo', 'Bar'] |
690
|
|
|
* F\remove(5, $items); //=> [] |
691
|
|
|
* F\remove(6, 'Hello World'); //=> 'World' |
692
|
|
|
* F\remove(-6, 'Hello World'); //=> 'Hello' |
693
|
|
|
* F\remove(3, 'a'); //=> '' |
694
|
|
|
* ``` |
695
|
|
|
* |
696
|
|
|
* @stream |
697
|
|
|
* @signature Number -> [a] -> [a] |
698
|
|
|
* @signature Number -> String -> String |
699
|
|
|
* @param int $count |
|
|
|
|
700
|
|
|
* @param array $list |
|
|
|
|
701
|
|
|
* @return array |
702
|
|
|
*/ |
703
|
|
|
function remove() { |
704
|
|
|
static $remove = false; |
705
|
|
|
$remove = $remove ?: curry(function($count, $list) { |
706
|
|
|
$length = length($list); |
707
|
|
|
if ($count > $length || $count < -$length) |
708
|
|
|
return is_string($list) ? '' : []; |
709
|
|
|
$count = ($count > 0) |
710
|
|
|
? $count - $length |
711
|
|
|
: $count + $length; |
712
|
|
View Code Duplication |
if(is_string($list)) { |
|
|
|
|
713
|
|
|
return ($count >= 0) |
714
|
|
|
? substr($list, 0, $count) |
715
|
|
|
: substr($list, $count); |
716
|
|
|
} |
717
|
|
|
return ($count >= 0) |
718
|
|
|
? array_slice($list, 0, $count) |
719
|
|
|
: array_slice($list, $count); |
720
|
|
|
}); |
721
|
|
|
return _apply($remove, func_get_args()); |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
/** |
725
|
|
|
* Removes elements from an array while they match the given predicate. |
726
|
|
|
* |
727
|
|
|
* It stops at the first element not matching the predicate and does not remove it. |
728
|
|
|
* ```php |
729
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
730
|
|
|
* F\removeWhile(F\startsWith('F'), $items); //=> ['Dev', 'Bar', 'Baz'] |
731
|
|
|
* F\removeWhile(F\startsWith('D'), $items); //=> ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
732
|
|
|
* ``` |
733
|
|
|
* |
734
|
|
|
* @stream |
735
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
736
|
|
|
* @param callable $predicate |
|
|
|
|
737
|
|
|
* @param array $list |
|
|
|
|
738
|
|
|
* @return array |
739
|
|
|
*/ |
740
|
|
View Code Duplication |
function removeWhile() { |
|
|
|
|
741
|
|
|
static $removeWhile = false; |
742
|
|
|
$removeWhile = $removeWhile ?: curry(function($predicate, $list) { |
743
|
|
|
$index = 0; |
744
|
|
|
$size = length($list); |
745
|
|
|
while ($index < $size && $predicate($list[$index])) |
746
|
|
|
$index ++; |
747
|
|
|
return array_slice($list, $index); |
748
|
|
|
}); |
749
|
|
|
return _apply($removeWhile, func_get_args()); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Same as `removeWhile` but removes elements from the end of the array. |
754
|
|
|
* |
755
|
|
|
* ```php |
756
|
|
|
* $items = ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz']; |
757
|
|
|
* F\removeLastWhile(F\startsWith('F'), $items); //=> ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz'] |
758
|
|
|
* F\removeLastWhile(F\startsWith('B'), $items); //=> ['Foo', 'Fun', 'Bye', 'Dev'] |
759
|
|
|
* ``` |
760
|
|
|
* |
761
|
|
|
* @stream |
762
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
763
|
|
|
* @param callable $predicate |
|
|
|
|
764
|
|
|
* @param array $list |
|
|
|
|
765
|
|
|
* @return array |
766
|
|
|
*/ |
767
|
|
View Code Duplication |
function removeLastWhile() { |
|
|
|
|
768
|
|
|
static $removeLastWhile = false; |
769
|
|
|
$removeLastWhile = $removeLastWhile ?: curry(function($predicate, $list) { |
770
|
|
|
$index = length($list) - 1; |
771
|
|
|
while ($index >= 0 && $predicate($list[$index])) |
772
|
|
|
$index --; |
773
|
|
|
return array_slice($list, 0, $index + 1); |
774
|
|
|
}); |
775
|
|
|
return _apply($removeLastWhile, func_get_args()); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Removes elements from an array **until** the predicate |
780
|
|
|
* is satisfied, not removing the satisfying element. |
781
|
|
|
* |
782
|
|
|
* ```php |
783
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
784
|
|
|
* F\removeUntil(F\startsWith('B'), $items); //=> ['Bar', 'Baz'] |
785
|
|
|
* F\removeUntil(F\startsWith('F'), $items); //=> ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
786
|
|
|
* F\removeUntil(F\startsWith('A'), $items); //=> [] |
787
|
|
|
* ``` |
788
|
|
|
* |
789
|
|
|
* @stream |
790
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
791
|
|
|
* @param callable $predicate |
|
|
|
|
792
|
|
|
* @param array $list |
|
|
|
|
793
|
|
|
* @return array |
794
|
|
|
*/ |
795
|
|
View Code Duplication |
function removeUntil() { |
|
|
|
|
796
|
|
|
static $removeUntil = false; |
797
|
|
|
$removeUntil = $removeUntil ?: curry(function($predicate, $list) { |
798
|
|
|
$index = 0; |
799
|
|
|
$size = length($list); |
800
|
|
|
while ($index < $size && !$predicate($list[$index])) |
801
|
|
|
$index ++; |
802
|
|
|
return array_slice($list, $index); |
803
|
|
|
}); |
804
|
|
|
return _apply($removeUntil, func_get_args()); |
805
|
|
|
} |
806
|
|
|
|
807
|
|
|
/** |
808
|
|
|
* Same as `removeUntil` but removes elements from the end of the array. |
809
|
|
|
* |
810
|
|
|
* ```php |
811
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
812
|
|
|
* F\removeLastUntil(F\startsWith('B'), $items); //=> ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
813
|
|
|
* F\removeLastUntil(F\startsWith('F'), $items); //=> ['Foo', 'Fun'] |
814
|
|
|
* F\removeLastUntil(F\startsWith('A'), $items); //=> [] |
815
|
|
|
* ``` |
816
|
|
|
* |
817
|
|
|
* @stream |
818
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
819
|
|
|
* @param callable $predicate |
|
|
|
|
820
|
|
|
* @param array $list |
|
|
|
|
821
|
|
|
* @return array |
822
|
|
|
*/ |
823
|
|
View Code Duplication |
function removeLastUntil() { |
|
|
|
|
824
|
|
|
static $removeLastUntil = false; |
825
|
|
|
$removeLastUntil = $removeLastUntil ?: curry(function($predicate, $list) { |
826
|
|
|
$index = length($list) - 1; |
827
|
|
|
while ($index >= 0 && !$predicate($list[$index])) |
828
|
|
|
$index --; |
829
|
|
|
return array_slice($list, 0, $index + 1); |
830
|
|
|
}); |
831
|
|
|
return _apply($removeLastUntil, func_get_args()); |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* Converts an array of (key, value) pairs to an object (instance of `stdClass`). |
836
|
|
|
* |
837
|
|
|
* ```php |
838
|
|
|
* F\fromPairs([['name', 'Foo'], ['age', 11]]); //=> (object) ['name' => 'Foo', 'age' => 11] |
839
|
|
|
* ``` |
840
|
|
|
* |
841
|
|
|
* @stream |
842
|
|
|
* @signature [(k, v)] -> {k: v} |
843
|
|
|
* @param array $pairs |
|
|
|
|
844
|
|
|
* @return stdClass |
845
|
|
|
*/ |
846
|
|
|
function fromPairs() { |
847
|
|
|
static $fromPairs = false; |
848
|
|
|
$fromPairs = $fromPairs ?: curry(function(&$pairs) { |
849
|
|
|
$result = new \stdClass; |
850
|
|
|
foreach ($pairs as &$pair) { |
851
|
|
|
$result->{$pair[0]} = $pair[1]; |
852
|
|
|
} |
853
|
|
|
return $result; |
854
|
|
|
}); |
855
|
|
|
return _apply($fromPairs, func_get_args()); |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
/** |
859
|
|
|
* Gets an array of slices of size `$size` from an array. |
860
|
|
|
* |
861
|
|
|
* ```php |
862
|
|
|
* $pairs = F\slices(2); |
863
|
|
|
* $pairs([1, 2, 3, 4, 5]); //=> [[1, 2], [3, 4], [5]] |
864
|
|
|
* $pairs("Hello World"); //=> ['He', 'll', 'o ', 'Wo', 'rl', 'd'] |
865
|
|
|
* F\slices(5, [1, 2]); //=> [[1, 2]] |
866
|
|
|
* F\slices(3, []); //=> [] |
867
|
|
|
* F\slices(3, ''); //=> [''] |
868
|
|
|
* ``` |
869
|
|
|
* |
870
|
|
|
* @stream |
871
|
|
|
* @signature Number -> [a] -> [[a]] |
872
|
|
|
* @signature Number -> String -> [String] |
873
|
|
|
* @param int $size |
|
|
|
|
874
|
|
|
* @param array $list |
|
|
|
|
875
|
|
|
* @return array |
876
|
|
|
*/ |
877
|
|
|
function slices() { |
878
|
|
|
static $slices = false; |
879
|
|
|
$slices = $slices ?: curry(function($size, &$list) { |
880
|
|
|
$length = length($list); |
881
|
|
|
if ($length == 0) |
882
|
|
|
return is_string($list) ? [''] : []; |
883
|
|
|
$start = 0; |
884
|
|
|
$result = []; |
885
|
|
|
$slicer = is_string($list) ? 'substr' : 'array_slice'; |
886
|
|
|
while ($start < $length) { |
887
|
|
|
$result[] = $slicer($list, $start, $size); |
888
|
|
|
$start += $size; |
889
|
|
|
} |
890
|
|
|
return $result; |
891
|
|
|
}); |
892
|
|
|
return _apply($slices, func_get_args()); |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
/** |
896
|
|
|
* Checks if an array contains an item. |
897
|
|
|
* |
898
|
|
|
* ```php |
899
|
|
|
* F\contains('foo', ['foo', 'bar', 'baz']); //=> true |
900
|
|
|
* F\contains('hi', ['foo', 'bar', 'baz']); //=> false |
901
|
|
|
* F\contains('hi', 'Hello World'); //=> false |
902
|
|
|
* F\contains('He', 'Hello World'); //=> true |
903
|
|
|
* ``` |
904
|
|
|
* |
905
|
|
|
* @stream |
906
|
|
|
* @signature a -> [a] -> Boolean |
907
|
|
|
* @signature String -> String -> Boolean |
908
|
|
|
* @param mixed $item |
|
|
|
|
909
|
|
|
* @param array|string $list |
|
|
|
|
910
|
|
|
* @return bool |
911
|
|
|
*/ |
912
|
|
View Code Duplication |
function contains() { |
|
|
|
|
913
|
|
|
static $contains = false; |
914
|
|
|
$contains = $contains ?: curry(function($item, $list) { |
915
|
|
|
return is_string($list) |
916
|
|
|
? (strpos($list, $item) !== false) |
917
|
|
|
: in_array($item, $list); |
918
|
|
|
}); |
919
|
|
|
return _apply($contains, func_get_args()); |
920
|
|
|
} |
921
|
|
|
|
922
|
|
|
/** |
923
|
|
|
* Returns the position/key of the first item satisfying the |
924
|
|
|
* predicate in the array or null if no such element is found. |
925
|
|
|
* |
926
|
|
|
* ```php |
927
|
|
|
* F\findIndex(F\startsWith('b'), ['foo', 'bar', 'baz']); //=> 1 |
928
|
|
|
* F\findIndex(F\startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); //=> 'b' |
929
|
|
|
* F\findIndex(F\startsWith('c'), ['foo', 'bar', 'baz']); //=> null |
930
|
|
|
* ``` |
931
|
|
|
* |
932
|
|
|
* @stream |
933
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(Number) |
934
|
|
|
* @signature (v -> Boolean) -> {k: v} -> Maybe(k) |
935
|
|
|
* @param callable $predicate |
|
|
|
|
936
|
|
|
* @param array $list |
|
|
|
|
937
|
|
|
* @return mixed |
938
|
|
|
*/ |
939
|
|
View Code Duplication |
function findIndex() { |
|
|
|
|
940
|
|
|
static $findIndex = false; |
941
|
|
|
$findIndex = $findIndex ?: curry(function($predicate, $list) { |
942
|
|
|
foreach ($list as $key => &$value) { |
943
|
|
|
if ($predicate($value)) |
944
|
|
|
return $key; |
945
|
|
|
} |
946
|
|
|
return null; |
947
|
|
|
}); |
948
|
|
|
return _apply($findIndex, func_get_args()); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Returns the position/key of the last item satisfying the |
953
|
|
|
* predicate in the array or null if no such element is found. |
954
|
|
|
* |
955
|
|
|
* ```php |
956
|
|
|
* F\findLastIndex(F\startsWith('b'), ['foo', 'bar', 'baz']); //=> 2 |
957
|
|
|
* F\findLastIndex(F\startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); //=> 'c' |
958
|
|
|
* F\findLastIndex(F\startsWith('c'), ['foo', 'bar', 'baz']); //=> null |
959
|
|
|
* ``` |
960
|
|
|
* |
961
|
|
|
* @stream |
962
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(Number) |
963
|
|
|
* @signature (v -> Boolean) -> {k: v} -> Maybe(k) |
964
|
|
|
* @param callable $predicate |
|
|
|
|
965
|
|
|
* @param array $list |
|
|
|
|
966
|
|
|
* @return mixed |
967
|
|
|
*/ |
968
|
|
View Code Duplication |
function findLastIndex() { |
|
|
|
|
969
|
|
|
static $findLastIndex = false; |
970
|
|
|
$findLastIndex = $findLastIndex ?: curry(function($predicate, $list) { |
971
|
|
|
$keys = array_keys($list); |
972
|
|
|
$index = count($keys) - 1; |
973
|
|
|
while ($index >= 0) { |
974
|
|
|
if ($predicate($list[$keys[$index]])) |
975
|
|
|
return $keys[$index]; |
976
|
|
|
$index --; |
977
|
|
|
} |
978
|
|
|
return null; |
979
|
|
|
}); |
980
|
|
|
return _apply($findLastIndex, func_get_args()); |
981
|
|
|
} |
982
|
|
|
|
983
|
|
|
/** |
984
|
|
|
* Returns the first item satisfying the predicate in |
985
|
|
|
* the array or null if no such element is found. |
986
|
|
|
* |
987
|
|
|
* ```php |
988
|
|
|
* F\find(F\startsWith('b'), ['foo', 'bar', 'baz']); //=> 'bar' |
989
|
|
|
* F\find(F\startsWith('c'), ['foo', 'bar', 'baz']); //=> null |
990
|
|
|
* ``` |
991
|
|
|
* |
992
|
|
|
* @stream |
993
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(a) |
994
|
|
|
* @param callable $predicate |
|
|
|
|
995
|
|
|
* @param array $list |
|
|
|
|
996
|
|
|
* @return mixed |
997
|
|
|
*/ |
998
|
|
View Code Duplication |
function find() { |
|
|
|
|
999
|
|
|
static $find = false; |
1000
|
|
|
$find = $find ?: curry(function($predicate, $list) { |
1001
|
|
|
foreach ($list as $key => &$value) { |
1002
|
|
|
if ($predicate($value)) |
1003
|
|
|
return $value; |
1004
|
|
|
} |
1005
|
|
|
return null; |
1006
|
|
|
}); |
1007
|
|
|
return _apply($find, func_get_args()); |
1008
|
|
|
} |
1009
|
|
|
|
1010
|
|
|
/** |
1011
|
|
|
* Returns the last item satisfying the predicate in |
1012
|
|
|
* the array or null if no such element is found. |
1013
|
|
|
* |
1014
|
|
|
* ```php |
1015
|
|
|
* F\findLast(F\startsWith('b'), ['foo', 'bar', 'baz']); //=> 'baz' |
1016
|
|
|
* F\findLast(F\startsWith('c'), ['foo', 'bar', 'baz']); //=> null |
1017
|
|
|
* ``` |
1018
|
|
|
* |
1019
|
|
|
* @stream |
1020
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(a) |
1021
|
|
|
* @param callable $predicate |
|
|
|
|
1022
|
|
|
* @param array $list |
|
|
|
|
1023
|
|
|
* @return mixed |
1024
|
|
|
*/ |
1025
|
|
View Code Duplication |
function findLast() { |
|
|
|
|
1026
|
|
|
static $findLast = false; |
1027
|
|
|
$findLast = $findLast ?: curry(function($predicate, $list) { |
1028
|
|
|
$keys = array_keys($list); |
1029
|
|
|
$index = count($keys) - 1; |
1030
|
|
|
while ($index >= 0) { |
1031
|
|
|
if ($predicate($list[$keys[$index]])) |
1032
|
|
|
return $list[$keys[$index]]; |
1033
|
|
|
$index --; |
1034
|
|
|
} |
1035
|
|
|
return null; |
1036
|
|
|
}); |
1037
|
|
|
return _apply($findLast, func_get_args()); |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
/** |
1041
|
|
|
* Returns the index of an item/substring in a list/string. |
1042
|
|
|
* |
1043
|
|
|
* If `$list` is an array, it returns the **key** of the first **item** which is equal to `$item`. |
1044
|
|
|
* If `$list` is a string, it returns the **position** of the first **substring** which is equal to `$item`. |
1045
|
|
|
* If `$list` is an object, it returns the **name** of the first **attribute** which is equal to `$item`. |
1046
|
|
|
* If the searched item, substring or attribute is not found; `null` is returned. |
1047
|
|
|
* Note that elements are deeply compared using `Tarsana\Functional\equals`. |
1048
|
|
|
* ```php |
1049
|
|
|
* F\indexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); //=> 1 |
1050
|
|
|
* F\indexOf(['Hello'], [1, ['Hello', 'World'], true]); //=> -1 |
1051
|
|
|
* F\indexOf('World', 'Hello World'); //=> 6 |
1052
|
|
|
* F\indexOf('World !', 'Hello World'); //=> -1 |
1053
|
|
|
* F\indexOf('foo', (object) ['name' => 'foo', 'age' => 11]); //=> 'name' |
1054
|
|
|
* ``` |
1055
|
|
|
* |
1056
|
|
|
* @stream |
1057
|
|
|
* @signature a -> [a] -> Number |
1058
|
|
|
* @signature v -> {k: v} -> Maybe(k) |
1059
|
|
|
* @signature String -> String -> Number |
1060
|
|
|
* @param mixed $item |
|
|
|
|
1061
|
|
|
* @param array $list |
|
|
|
|
1062
|
|
|
* @return int |
1063
|
|
|
*/ |
1064
|
|
|
function indexOf() { |
1065
|
|
|
static $indexOf = false; |
1066
|
|
|
$indexOf = $indexOf ?: curry(function($item, $list) { |
1067
|
|
|
if (is_string($list)) { |
1068
|
|
|
$index = strpos($list, $item); |
1069
|
|
|
return $index === false ? -1 : $index; |
1070
|
|
|
} |
1071
|
|
|
$list = (array) $list; |
1072
|
|
|
$index = 0; |
1073
|
|
|
$keys = array_keys($list); |
1074
|
|
|
$length = count($keys); |
1075
|
|
View Code Duplication |
while ($index < $length) { |
|
|
|
|
1076
|
|
|
if (_equals($item, $list[$keys[$index]])) |
1077
|
|
|
return $keys[$index]; |
1078
|
|
|
$index ++; |
1079
|
|
|
} |
1080
|
|
|
return -1; |
1081
|
|
|
}); |
1082
|
|
|
return _apply($indexOf, func_get_args()); |
1083
|
|
|
} |
1084
|
|
|
|
1085
|
|
|
/** |
1086
|
|
|
* Same as `indexOf` but returns the key/position/name of the last item/substring/attribute. |
1087
|
|
|
* |
1088
|
|
|
* ```php |
1089
|
|
|
* F\lastIndexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); //=> 3 |
1090
|
|
|
* F\lastIndexOf(['Hello'], [1, ['Hello', 'World'], true]); //=> -1 |
1091
|
|
|
* F\lastIndexOf('World', 'Hello World'); //=> 6 |
1092
|
|
|
* F\lastIndexOf('World !', 'Hello World'); //=> -1 |
1093
|
|
|
* F\lastIndexOf('foo', (object) ['name' => 'foo', 'age' => 11]); //=> 'name' |
1094
|
|
|
* ``` |
1095
|
|
|
* |
1096
|
|
|
* @stream |
1097
|
|
|
* @signature a -> [a] -> Number |
1098
|
|
|
* @signature v -> {k: v} -> Maybe(k) |
1099
|
|
|
* @signature String -> String -> Number |
1100
|
|
|
* @param mixed $item |
|
|
|
|
1101
|
|
|
* @param array $list |
|
|
|
|
1102
|
|
|
* @return int |
1103
|
|
|
*/ |
1104
|
|
|
function lastIndexOf() { |
1105
|
|
|
static $lastIndexOf = false; |
1106
|
|
|
$lastIndexOf = $lastIndexOf ?: curry(function($item, $list) { |
1107
|
|
|
if (is_string($list)) { |
1108
|
|
|
$index = strrpos($list, $item); |
1109
|
|
|
return $index === false ? -1 : $index; |
1110
|
|
|
} |
1111
|
|
|
$list = (array) $list; |
1112
|
|
|
$keys = array_keys($list); |
1113
|
|
|
$index = count($list) - 1; |
1114
|
|
View Code Duplication |
while ($index >= 0) { |
|
|
|
|
1115
|
|
|
if (_equals($list[$keys[$index]], $item)) |
1116
|
|
|
return $keys[$index]; |
1117
|
|
|
$index --; |
1118
|
|
|
} |
1119
|
|
|
return -1; |
1120
|
|
|
}); |
1121
|
|
|
return _apply($lastIndexOf, func_get_args()); |
1122
|
|
|
} |
1123
|
|
|
|
1124
|
|
|
/** |
1125
|
|
|
* Removes duplicates from a list. |
1126
|
|
|
* |
1127
|
|
|
* keeps the first occurence of each group of items which are |
1128
|
|
|
* equivalent according to the given `$areEqual` callable. |
1129
|
|
|
* ```php |
1130
|
|
|
* F\uniqueBy(F\eq(), [1, '2', '1', 3, '3', 2, 2]); //=> [1, '2', 3] |
1131
|
|
|
* ``` |
1132
|
|
|
* |
1133
|
|
|
* @stream |
1134
|
|
|
* @signature (a -> a -> Boolean) -> [a] -> [a] |
1135
|
|
|
* @param callable $areEqual |
|
|
|
|
1136
|
|
|
* @param array $list |
|
|
|
|
1137
|
|
|
* @return array |
1138
|
|
|
*/ |
1139
|
|
|
function uniqueBy() { |
1140
|
|
|
static $uniqueBy = false; |
1141
|
|
|
$uniqueBy = $uniqueBy ?: curry(function($areEqual, $list) { |
1142
|
|
|
$result = []; |
1143
|
|
|
$size = 0; |
1144
|
|
|
foreach ($list as &$item) { |
1145
|
|
|
$found = false; |
1146
|
|
|
$index = 0; |
1147
|
|
|
while ($index < $size) { |
1148
|
|
|
if ($areEqual($result[$index], $item)) { |
1149
|
|
|
$found = true; |
1150
|
|
|
break; |
1151
|
|
|
} |
1152
|
|
|
$index ++; |
1153
|
|
|
} |
1154
|
|
|
if (! $found) { |
1155
|
|
|
$result[$size] = $item; |
1156
|
|
|
$size ++; |
1157
|
|
|
} |
1158
|
|
|
} |
1159
|
|
|
return $result; |
1160
|
|
|
}); |
1161
|
|
|
return _apply($uniqueBy, func_get_args()); |
1162
|
|
|
} |
1163
|
|
|
|
1164
|
|
|
/** |
1165
|
|
|
* Alias of `F\uniqueBy(F\equals())`. |
1166
|
|
|
* |
1167
|
|
|
* ```php |
1168
|
|
|
* F\unique([1, '1', [1, 2], 1, ['1', 2], [1, 2]]); //=> [1, '1', [1, 2], ['1', 2]] |
1169
|
|
|
* ``` |
1170
|
|
|
* |
1171
|
|
|
* @stream |
1172
|
|
|
* @signature [a] -> [a] |
1173
|
|
|
* @param array $list |
|
|
|
|
1174
|
|
|
* @return array |
1175
|
|
|
*/ |
1176
|
|
|
function unique() { |
1177
|
|
|
static $unique = false; |
1178
|
|
|
$unique = $unique ?: uniqueBy(_f('_equals')); |
1179
|
|
|
return _apply($unique, func_get_args()); |
1180
|
|
|
} |
1181
|
|
|
|
1182
|
|
|
/** |
1183
|
|
|
* Converts an array to an associative array, based on the result of calling `$fn` |
1184
|
|
|
* on each element, and grouping the results according to values returned. |
1185
|
|
|
* |
1186
|
|
|
* Note that `$fn` should take an item from the list and return a string. |
1187
|
|
|
* ```php |
1188
|
|
|
* $persons = [ |
1189
|
|
|
* ['name' => 'foo', 'age' => 11], |
1190
|
|
|
* ['name' => 'bar', 'age' => 9], |
1191
|
|
|
* ['name' => 'baz', 'age' => 16], |
1192
|
|
|
* ['name' => 'zeta', 'age' => 33], |
1193
|
|
|
* ['name' => 'beta', 'age' => 25] |
1194
|
|
|
* ]; |
1195
|
|
|
* $phase = function($person) { |
1196
|
|
|
* $age = $person['age']; |
1197
|
|
|
* if ($age < 13) return 'child'; |
1198
|
|
|
* if ($age < 19) return 'teenager'; |
1199
|
|
|
* return 'adult'; |
1200
|
|
|
* }; |
1201
|
|
|
* F\groupBy($phase, $persons); //=> ['child' => [['name' => 'foo', 'age' => 11], ['name' => 'bar', 'age' => 9]], 'teenager' => [['name' => 'baz', 'age' => 16]], 'adult' => [['name' => 'zeta', 'age' => 33], ['name' => 'beta', 'age' => 25]]] |
1202
|
|
|
* ``` |
1203
|
|
|
* |
1204
|
|
|
* @stream |
1205
|
|
|
* @signature (a -> String) -> [a] -> {String: a} |
1206
|
|
|
* @param callable $fn |
|
|
|
|
1207
|
|
|
* @param array $list |
|
|
|
|
1208
|
|
|
* @return array |
1209
|
|
|
*/ |
1210
|
|
|
function groupBy() { |
1211
|
|
|
static $groupBy = false; |
1212
|
|
|
$groupBy = $groupBy ?: curry(function($fn, $list) { |
1213
|
|
|
$result = []; |
1214
|
|
|
foreach($list as $item) { |
1215
|
|
|
$index = $fn($item); |
1216
|
|
|
if (! isset($result[$index])) |
1217
|
|
|
$result[$index] = []; |
1218
|
|
|
$result[$index][] = $item; |
1219
|
|
|
} |
1220
|
|
|
return $result; |
1221
|
|
|
}); |
1222
|
|
|
return _apply($groupBy, func_get_args()); |
1223
|
|
|
} |
1224
|
|
|
|
1225
|
|
|
/** |
1226
|
|
|
* Makes list of pairs from two lists. |
1227
|
|
|
* |
1228
|
|
|
* ```php |
1229
|
|
|
* F\pairsFrom([1, 2, 3], ['foo', 'bar', 'baz']); //=> [[1, 'foo'], [2, 'bar'], [3, 'baz']] |
1230
|
|
|
* F\pairsFrom([1, 2, 3], ['foo', 'bar']); //=> [[1, 'foo'], [2, 'bar']] |
1231
|
|
|
* F\pairsFrom([1, 3], ['foo', 'bar', 'baz']); //=> [[1, 'foo'], [3, 'bar']] |
1232
|
|
|
* F\pairsFrom([], ['foo', 'bar', 'baz']); //=> [] |
1233
|
|
|
* ``` |
1234
|
|
|
* |
1235
|
|
|
* @stream |
1236
|
|
|
* @signature [a] -> [b] -> [[a,b]] |
1237
|
|
|
* @param array $list1 |
|
|
|
|
1238
|
|
|
* @param array $list2 |
|
|
|
|
1239
|
|
|
* @return array |
1240
|
|
|
*/ |
1241
|
|
|
function pairsFrom() { |
1242
|
|
|
static $pairsFrom = false; |
1243
|
|
|
$pairsFrom = $pairsFrom ?: curry(function($list1, $list2) { |
1244
|
|
|
$length1 = count($list1); |
1245
|
|
|
$length2 = count($list2); |
1246
|
|
|
if (0 == $length1 || 0 == $length2) |
1247
|
|
|
return []; |
1248
|
|
|
$result = []; |
1249
|
|
|
$index = 0; |
1250
|
|
|
$length = min($length1, $length2); |
1251
|
|
|
while ($index < $length) { |
1252
|
|
|
$result[] = [$list1[$index], $list2[$index]]; |
1253
|
|
|
$index ++; |
1254
|
|
|
} |
1255
|
|
|
return $result; |
1256
|
|
|
}); |
1257
|
|
|
return _apply($pairsFrom, func_get_args()); |
1258
|
|
|
} |
1259
|
|
|
|
1260
|
|
|
/** |
1261
|
|
|
* Returns a copy of the given list, ordered using the given comparaison function. |
1262
|
|
|
* |
1263
|
|
|
* The `$compare` function should take two elements from the list and return `true` |
1264
|
|
|
* if the first element should be placed before the second element in the sorted |
1265
|
|
|
* list; and return `false` otherwise. |
1266
|
|
|
* |
1267
|
|
|
* **Note** This function is calling `usort` to sort elements, so: |
1268
|
|
|
* |
1269
|
|
|
* - if two elements `$a` and `$b` of the list are considered equal |
1270
|
|
|
* (ie `$compare($a, $b) == false` and `$compare($b, $a) == false`) then their |
1271
|
|
|
* order in the resulting array is undefined. |
1272
|
|
|
* |
1273
|
|
|
* - This function assigns new keys to the elements in array. It will remove any |
1274
|
|
|
* existing keys that may have been assigned, rather than just reordering the keys. |
1275
|
|
|
* ```php |
1276
|
|
|
* $numbers = [4, 5, 1, 3, 1, 2, 5]; |
1277
|
|
|
* F\sort(F\lt(), $numbers); //=> [1, 1, 2, 3, 4, 5, 5] |
1278
|
|
|
* F\sort(F\gt(), $numbers); //=> [5, 5, 4, 3, 2, 1, 1] |
1279
|
|
|
* ``` |
1280
|
|
|
* |
1281
|
|
|
* @stream |
1282
|
|
|
* @signature (a -> a -> Boolean) -> [a] -> [a] |
1283
|
|
|
* @param callable $compare |
1284
|
|
|
* @param array $list |
|
|
|
|
1285
|
|
|
* @return array |
1286
|
|
|
*/ |
1287
|
|
View Code Duplication |
function sort() { |
|
|
|
|
1288
|
|
|
static $sort = false; |
1289
|
|
|
$sort = $sort ?: curry(function($compare, $list) { |
1290
|
|
|
$result = clone_($list); |
1291
|
|
|
usort($result, comparator($compare)); |
1292
|
|
|
return $result; |
1293
|
|
|
}); |
1294
|
|
|
return _apply($sort, func_get_args()); |
1295
|
|
|
} |
1296
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.