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