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