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
|
|
View Code Duplication |
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 = all(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 all() { |
260
|
|
|
$all = function($predicate, $list) { |
261
|
|
|
return length(filter($predicate, $list)) == length($list); |
262
|
|
|
}; |
263
|
|
|
return apply(curry($all), func_get_args()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Checks if the `$predicate` is verified by **any** item of the array. |
269
|
|
|
* ```php |
270
|
|
|
* $anyNumeric = any('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 any() { |
281
|
|
|
$any = function($predicate, $list) { |
282
|
|
|
return null != findIndex($predicate, $list); |
283
|
|
|
}; |
284
|
|
|
return apply(curry($any), 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
|
|
|
if (is_string($list1)) |
303
|
|
|
return $list1 . $list2; |
304
|
|
|
return array_merge($list1, $list2); |
305
|
|
|
}; |
306
|
|
|
return apply(curry($concat), func_get_args()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Concatenates a list of arrays or strings. |
311
|
|
|
* ```php |
312
|
|
|
* concatAll([[1, 2], [3, 4], [5, 6]]) // [1, 2, 3, 4, 5, 6] |
313
|
|
|
* concatAll('Hello ', 'World', ' !') // 'Hello World !' |
314
|
|
|
* ``` |
315
|
|
|
* |
316
|
|
|
* @signature [[a]] -> [a] |
317
|
|
|
* @param array $lists |
|
|
|
|
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
|
|
function concatAll() { |
321
|
|
|
$concat = function($lists) { |
322
|
|
|
return reduce(concat(), [], $lists); |
323
|
|
|
}; |
324
|
|
|
return apply(curry($concat), func_get_args()); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Inserts an item at some position into an array or a substring into a string. If `$position < 0` |
329
|
|
|
* the item or substring is inserted before the last `|$position|` elements/characters. |
330
|
|
|
* ```php |
331
|
|
|
* insert(2, 'x', [1, 2, 3, 4]); // [1, 2, 'x', 3, 4] |
332
|
|
|
* insert(-1, 'x', [1, 2, 3, 4]); // [1, 2, 3, 'x', 4] |
333
|
|
|
* insert(11, 'x', [1, 2, 3, 4]); // [1, 2, 3, 4, 'x'] |
334
|
|
|
* insert(0, 'x', [1, 2, 3, 4]); // ['x', 1, 2, 3, 4] |
335
|
|
|
* insert(-11, 'x', [1, 2, 3, 4]); // ['x', 1, 2, 3, 4] |
336
|
|
|
* insert(3, 'l', 'Helo World'); // 'Hello World' |
337
|
|
|
* ``` |
338
|
|
|
* |
339
|
|
|
* @signature Number -> a -> [a] -> [a] |
340
|
|
|
* @signature Number -> String -> String -> String |
341
|
|
|
* @param int $position |
|
|
|
|
342
|
|
|
* @param mixed $item |
|
|
|
|
343
|
|
|
* @param array $list |
|
|
|
|
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
function insert() { |
347
|
|
|
$insert = function($position, $item, $list) { |
348
|
|
|
return (is_string($list)) |
349
|
|
|
? insertAll($position, $item, $list) |
350
|
|
|
: insertAll($position, [$item], $list); |
351
|
|
|
}; |
352
|
|
|
return apply(curry($insert), func_get_args()); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Same as `insert` but inserts an array instead of a single item. |
357
|
|
|
* ```php |
358
|
|
|
* insertAll(2, ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 'x', 'y', 3, 4] |
359
|
|
|
* insertAll(-1, ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 3, 'x', 'y', 4] |
360
|
|
|
* insertAll(11, ['x', 'y'], [1, 2, 3, 4]); // [1, 2, 3, 4, 'x', 'y'] |
361
|
|
|
* insertAll(0, ['x', 'y'], [1, 2, 3, 4]); // ['x', 'y', 1, 2, 3, 4] |
362
|
|
|
* insertAll(-11, ['x', 'y'], [1, 2, 3, 4]); // ['x', 'y', 1, 2, 3, 4] |
363
|
|
|
* insert(2, 'llo', 'He World'); // 'Hello World' |
364
|
|
|
* ``` |
365
|
|
|
* |
366
|
|
|
* @signature Number -> [a] -> [a] -> [a] |
367
|
|
|
* @signature Number -> String -> String -> String |
368
|
|
|
* @param int $position |
|
|
|
|
369
|
|
|
* @param mixed $items |
|
|
|
|
370
|
|
|
* @param array $list |
|
|
|
|
371
|
|
|
* @return array |
372
|
|
|
*/ |
373
|
|
|
function insertAll() { |
374
|
|
|
$insertAll = function($position, $items, $list) { |
375
|
|
|
if ($position < 0) |
376
|
|
|
$position = length($list) - $position; |
377
|
|
|
return concatAll([take($position, $list), $items, remove($position, $list)]); |
378
|
|
|
}; |
379
|
|
|
return apply(curry($insertAll), func_get_args()); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Appends an item to an array or a substring to a string. |
384
|
|
|
* ```php |
385
|
|
|
* append(5, [1, 2, 3]) // [1, 2, 3, 5] |
386
|
|
|
* append(' World', 'Hello') // 'Hello World' |
387
|
|
|
* ``` |
388
|
|
|
* |
389
|
|
|
* @signature * -> [*] -> [*] |
390
|
|
|
* @signature String -> String -> String |
391
|
|
|
* @param mixed $item |
|
|
|
|
392
|
|
|
* @param array $list |
|
|
|
|
393
|
|
|
* @return array |
394
|
|
|
*/ |
395
|
|
|
function append() { |
396
|
|
|
$append = function ($item, $list) { |
397
|
|
|
return concat($list, [$item]); |
398
|
|
|
}; |
399
|
|
|
return apply(curry($append), func_get_args()); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Inserts an item at the begining of an array or a substring at the begining of a string. |
404
|
|
|
* Note that this function is equivalent to `insert(0)`. |
405
|
|
|
* ```php |
406
|
|
|
* prepend(5, [1, 2, 3]) // [5, 1, 2, 3] |
407
|
|
|
* prepend('Hello ', 'World') // 'Hello World' |
408
|
|
|
* ``` |
409
|
|
|
* |
410
|
|
|
* @signature a -> [a] -> [a] |
411
|
|
|
* @signature String -> String -> String |
412
|
|
|
* @param mixed $item |
|
|
|
|
413
|
|
|
* @param array $list |
|
|
|
|
414
|
|
|
* @return array |
415
|
|
|
*/ |
416
|
|
|
function prepend() { |
417
|
|
|
return apply(insert(0), func_get_args()); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Takes a number of elements from an array. If `$count` is |
422
|
|
|
* negative, the elements are taken from the end of the array. |
423
|
|
|
* ```php |
424
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
425
|
|
|
* take(2, $items) // ['Foo', 'Bar'] |
426
|
|
|
* take(0, $items) // [] |
427
|
|
|
* take(-2, $items) // ['Bar', 'Baz'] |
428
|
|
|
* take(5, 'Hello World') // 'Hello' |
429
|
|
|
* take(-5, 'Hello World') // 'World' |
430
|
|
|
* ``` |
431
|
|
|
* |
432
|
|
|
* @signature Number -> [a] -> [a] |
433
|
|
|
* @signature Number -> String -> String |
434
|
|
|
* @param int $count |
|
|
|
|
435
|
|
|
* @param array $list |
|
|
|
|
436
|
|
|
* @return array |
437
|
|
|
*/ |
438
|
|
|
function take() { |
439
|
|
|
$take = function($count, $list) { |
440
|
|
|
if ($count > $length || $count < -$length) |
|
|
|
|
441
|
|
|
return []; |
442
|
|
|
if(is_string($list)) { |
443
|
|
|
return ($count >= 0) |
444
|
|
|
? substr($list, 0, $count) |
445
|
|
|
: substr($list, $count); |
446
|
|
|
} |
447
|
|
|
return ($count >= 0) |
448
|
|
|
? array_slice($list, 0, $count) |
449
|
|
|
: array_slice($list, $count); |
450
|
|
|
}; |
451
|
|
|
return apply(curry($take), func_get_args()); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Takes elements from an array while they match the given predicate. It stops at the |
456
|
|
|
* first element not matching the predicate and does not include it in the result. |
457
|
|
|
* ```php |
458
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
459
|
|
|
* takeWhile(startsWith('F'), $items) // ['Foo', 'Fun'] |
460
|
|
|
* takeWhile(startsWith('D'), $items) // [] |
461
|
|
|
* ``` |
462
|
|
|
* |
463
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
464
|
|
|
* @param callable $predicate |
|
|
|
|
465
|
|
|
* @param array $list |
|
|
|
|
466
|
|
|
* @return array |
467
|
|
|
*/ |
468
|
|
View Code Duplication |
function takeWhile() { |
|
|
|
|
469
|
|
|
$takeWhile = function($predicate, $list) { |
470
|
|
|
$first = head($list); |
471
|
|
|
return (null == $first || !$predicate($first)) |
472
|
|
|
? [] |
473
|
|
|
: prepend($first, takeWhile($predicate, $list)); |
474
|
|
|
|
475
|
|
|
}; |
476
|
|
|
return apply(curry($takeWhile), func_get_args()); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Same as `takeWhile` but taking elements from the end of the array. |
481
|
|
|
* ```php |
482
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
483
|
|
|
* takeLastWhile(startsWith('B'), $items) // ['Bar', 'Baz'] |
484
|
|
|
* takeLastWhile(startsWith('D'), $items) // [] |
485
|
|
|
* ``` |
486
|
|
|
* |
487
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
488
|
|
|
* @param callable $predicate |
|
|
|
|
489
|
|
|
* @param array $list |
|
|
|
|
490
|
|
|
* @return array |
491
|
|
|
*/ |
492
|
|
View Code Duplication |
function takeLastWhile() { |
|
|
|
|
493
|
|
|
$takeWhile = function($predicate, $list) { |
|
|
|
|
494
|
|
|
$last = last($list); |
495
|
|
|
return (null == $last || !$predicate($last)) |
496
|
|
|
? [] |
497
|
|
|
: append($last, takeLastWhile($predicate, $list)); |
498
|
|
|
|
499
|
|
|
}; |
500
|
|
|
return apply(curry($takeLastWhile), func_get_args()); |
|
|
|
|
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Takes elements from an array **until** the predicate |
505
|
|
|
* is satisfied, not including the satisfying element. |
506
|
|
|
* ```php |
507
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
508
|
|
|
* takeUntil(startsWith('B'), $items) // ['Foo', 'Fun', 'Dev'] |
509
|
|
|
* takeUntil(startsWith('F'), $items) // [] |
510
|
|
|
* ``` |
511
|
|
|
* |
512
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
513
|
|
|
* @param callable $predicate |
|
|
|
|
514
|
|
|
* @param array $list |
|
|
|
|
515
|
|
|
* @return array |
516
|
|
|
*/ |
517
|
|
|
function takeUntil() { |
518
|
|
|
$takeUntil = function($predicate, $list) { |
519
|
|
|
return takeWhile(pipe($predicate, not_()), $list); |
520
|
|
|
}; |
521
|
|
|
return apply(curry($takeUntil), func_get_args()); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Same as `takeUntil` but takes elements from the end of the array. |
526
|
|
|
* ```php |
527
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
528
|
|
|
* takeLastUntil(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz'] |
529
|
|
|
* takeLastUntil(startsWith('B'), $items) // [] |
530
|
|
|
* ``` |
531
|
|
|
* |
532
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
533
|
|
|
* @param callable $predicate |
|
|
|
|
534
|
|
|
* @param array $list |
|
|
|
|
535
|
|
|
* @return array |
536
|
|
|
*/ |
537
|
|
|
function takeLastUntil() { |
538
|
|
|
$takeUntil = function($predicate, $list) { |
539
|
|
|
return takeLastWhile(pipe($predicate, not()), $list); |
540
|
|
|
}; |
541
|
|
|
return apply(curry($takeUntil), func_get_args()); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Removes a number of elements from an array. |
546
|
|
|
* If `$count` is negative, the elements are |
547
|
|
|
* removed from the end of the array. |
548
|
|
|
* ```php |
549
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
550
|
|
|
* remove(2, $items) // ['Baz'] |
551
|
|
|
* remove(-1, $items) // ['Foo', 'Bar'] |
552
|
|
|
* remove(5, $items) // [] |
553
|
|
|
* remove(6, 'Hello World') // 'World' |
554
|
|
|
* remove(-6, 'Hello World') // 'Hello' |
555
|
|
|
* ``` |
556
|
|
|
* |
557
|
|
|
* @signature Number -> [a] -> [a] |
558
|
|
|
* @signature Number -> String -> String |
559
|
|
|
* @param int $count |
|
|
|
|
560
|
|
|
* @param array $list |
|
|
|
|
561
|
|
|
* @return array |
562
|
|
|
*/ |
563
|
|
|
function remove() { |
564
|
|
|
$remove = function($count, $list) { |
565
|
|
|
$length = length($list); |
566
|
|
|
if ($count > $length || $count < -$length) |
567
|
|
|
return []; |
568
|
|
|
return ($count > 0) |
569
|
|
|
? take($count - $length) |
570
|
|
|
: take($count + $length); |
571
|
|
|
}; |
572
|
|
|
return apply(curry($remove), func_get_args()); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Removes elements from an array while they match the given predicate. It stops at the |
577
|
|
|
* first element not matching the predicate and does not remove it. |
578
|
|
|
* ```php |
579
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
580
|
|
|
* removeWhile(startsWith('F'), $items) // ['Dev', 'Bar', 'Baz'] |
581
|
|
|
* removeWhile(startsWith('D'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
582
|
|
|
* ``` |
583
|
|
|
* |
584
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
585
|
|
|
* @param callable $predicate |
|
|
|
|
586
|
|
|
* @param array $list |
|
|
|
|
587
|
|
|
* @return array |
588
|
|
|
*/ |
589
|
|
|
function removeWhile() { |
590
|
|
|
$removeWhile = function($predicate, $list) { |
591
|
|
|
return remove(length(takeWhile($predicate, $list)), $list); |
592
|
|
|
}; |
593
|
|
|
return apply(curry($removeWhile), func_get_args()); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Same as `removeWhile` but removes elements from the end of the array. |
598
|
|
|
* ```php |
599
|
|
|
* $items = ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz']; |
600
|
|
|
* removeLastWhile(startsWith('F'), $items) // ['Foo', 'Fun', 'Bye', 'Dev', 'Bar', 'Baz'] |
601
|
|
|
* removeLastWhile(startsWith('B'), $items) // ['Foo', 'Fun', 'Bye', 'Dev'] |
602
|
|
|
* ``` |
603
|
|
|
* |
604
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
605
|
|
|
* @param callable $predicate |
|
|
|
|
606
|
|
|
* @param array $list |
|
|
|
|
607
|
|
|
* @return array |
608
|
|
|
*/ |
609
|
|
|
function removeLastWhile() { |
610
|
|
|
$removeLastWhile = function($predicate, $list) { |
611
|
|
|
return remove(- length(takeLastWhile($predicate, $list)), $list); |
612
|
|
|
}; |
613
|
|
|
return apply(curry($removeLastWhile), func_get_args()); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Removes elements from an array **until** the predicate |
618
|
|
|
* is satisfied, not removing the satisfying element. |
619
|
|
|
* ```php |
620
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
621
|
|
|
* removeUntil(startsWith('B'), $items) // ['Bar', 'Baz'] |
622
|
|
|
* removeUntil(startsWith('F'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
623
|
|
|
* removeUntil(startsWith('A'), $items) // [] |
624
|
|
|
* ``` |
625
|
|
|
* |
626
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
627
|
|
|
* @param callable $predicate |
|
|
|
|
628
|
|
|
* @param array $list |
|
|
|
|
629
|
|
|
* @return array |
630
|
|
|
*/ |
631
|
|
|
function removeUntil() { |
632
|
|
|
$removeUntil = function($predicate, $list) { |
633
|
|
|
return remove(length(takeUntil($predicate, $list)), $list); |
634
|
|
|
}; |
635
|
|
|
return apply(curry($removeUntil), func_get_args()); |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
/** |
639
|
|
|
* Same as `removeUntil` but removes elements from the end of the array. |
640
|
|
|
* ```php |
641
|
|
|
* $items = ['Foo', 'Fun', 'Dev', 'Bar', 'Baz']; |
642
|
|
|
* removeLastUntil(startsWith('B'), $items) // ['Foo', 'Fun', 'Dev', 'Bar', 'Baz'] |
643
|
|
|
* removeLastUntil(startsWith('F'), $items) // ['Foo', 'Fun'] |
644
|
|
|
* removeLastUntil(startsWith('A'), $items) // [] |
645
|
|
|
* ``` |
646
|
|
|
* |
647
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
648
|
|
|
* @param callable $predicate |
|
|
|
|
649
|
|
|
* @param array $list |
|
|
|
|
650
|
|
|
* @return array |
651
|
|
|
*/ |
652
|
|
|
function removeLastUntil() { |
653
|
|
|
$removeLastUntil = function($predicate, $list) { |
654
|
|
|
return remove(- length(takeLastUntil($predicate, $list)), $list); |
655
|
|
|
}; |
656
|
|
|
return apply(curry($removeLastUntil), func_get_args()); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Converts an array of (key, value) pairs to an object (instance of `stdClass`). |
661
|
|
|
* ```php |
662
|
|
|
* fromPairs([['name', 'Foo'], ['age', 11]]); // (object) ['name' => 'Foo', 'age' => 11] |
663
|
|
|
* ``` |
664
|
|
|
* |
665
|
|
|
* @signature [(k, v)] -> {k: v} |
666
|
|
|
* @param array $pairs |
|
|
|
|
667
|
|
|
* @return stdClass |
668
|
|
|
*/ |
669
|
|
|
function fromPairs() { |
670
|
|
|
$fromPairs = function($pairs) { |
671
|
|
|
return reduce(function($result, $pair) { |
672
|
|
|
$result->{$pair[0]} = $pair[1]; |
673
|
|
|
return $result; |
674
|
|
|
}, new \stdClass, $pairs); |
675
|
|
|
}; |
676
|
|
|
return apply(curry($fromPairs), func_get_args()); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* Gets an array of slices of size `$size` from an array. |
681
|
|
|
* ```php |
682
|
|
|
* $pairs = slices(2); |
683
|
|
|
* $pairs([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]] |
684
|
|
|
* $pairs("Hello World"); // ['He', 'll', 'o ', 'Wo', 'rl', 'd'] |
685
|
|
|
* slices(5, [1, 2]); // [[1, 2]] |
686
|
|
|
* slices(3, []) // [] |
687
|
|
|
* slices(3, '') // '' |
688
|
|
|
* ``` |
689
|
|
|
* |
690
|
|
|
* @signature Number -> [a] -> [[a]] |
691
|
|
|
* @signature Number -> String -> [String] |
692
|
|
|
* @param int $size |
|
|
|
|
693
|
|
|
* @param array $list |
|
|
|
|
694
|
|
|
* @return array |
695
|
|
|
*/ |
696
|
|
|
function slices() { |
697
|
|
|
$slices = function($size, $list) { |
698
|
|
|
if(empty($list)) |
699
|
|
|
return is_string($list) ? '' : []; |
700
|
|
|
if(length($list) <= $size) |
701
|
|
|
return [$list]; |
702
|
|
|
return prepend(take($size, $list), slices($size, remove($size, $list))); |
703
|
|
|
}; |
704
|
|
|
return apply(curry($slices), func_get_args()); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Checks if an array contains an item. |
709
|
|
|
* ```php |
710
|
|
|
* contains('foo', ['foo', 'bar', 'baz']) // true |
711
|
|
|
* contains('hi', ['foo', 'bar', 'baz']) // false |
712
|
|
|
* contains('hi', 'Hello World') // false |
713
|
|
|
* contains('He', 'Hello World') // true |
714
|
|
|
* ``` |
715
|
|
|
* |
716
|
|
|
* @signature a -> [a] -> Boolean |
717
|
|
|
* @signature String -> String -> Boolean |
718
|
|
|
* @param mixed $item |
|
|
|
|
719
|
|
|
* @param array|string $list |
|
|
|
|
720
|
|
|
* @return bool |
721
|
|
|
*/ |
722
|
|
|
function contains() { |
723
|
|
|
$contains = function($item, $list) { |
724
|
|
|
return null != indexOf($item, $list); |
725
|
|
|
}; |
726
|
|
|
return apply(curry($contains), func_get_args()); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
/** |
730
|
|
|
* Returns the position/key of the first item satisfying the |
731
|
|
|
* predicate in the array or null if no such element is found. |
732
|
|
|
* ```php |
733
|
|
|
* findIndex(startsWith('b'), ['foo', 'bar', 'baz']); // 1 |
734
|
|
|
* findIndex(startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); // 'b' |
735
|
|
|
* findIndex(startsWith('c'), ['foo', 'bar', 'baz']); // null |
736
|
|
|
* ``` |
737
|
|
|
* |
738
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(Number) |
739
|
|
|
* @signature (v -> Boolean) -> {k: v} -> Maybe(k) |
740
|
|
|
* @param callable $predicate |
|
|
|
|
741
|
|
|
* @param array $list |
|
|
|
|
742
|
|
|
* @return mixed |
743
|
|
|
*/ |
744
|
|
|
function findIndex() { |
745
|
|
|
$findIndex = function($predicate, $list) { |
746
|
|
|
foreach ($list as $key => $value) { |
747
|
|
|
if ($predicate($value)) |
748
|
|
|
return $key; |
749
|
|
|
} |
750
|
|
|
return null; |
751
|
|
|
}; |
752
|
|
|
return apply(curry($findIndex), func_get_args()); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* Returns the position/key of the last item satisfying the |
757
|
|
|
* predicate in the array or null if no such element is found. |
758
|
|
|
* ```php |
759
|
|
|
* findLastIndex(startsWith('b'), ['foo', 'bar', 'baz']); // 2 |
760
|
|
|
* findLastIndex(startsWith('b'), ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']); // 'c' |
761
|
|
|
* findLastIndex(startsWith('c'), ['foo', 'bar', 'baz']); // null |
762
|
|
|
* ``` |
763
|
|
|
* |
764
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(Number) |
765
|
|
|
* @signature (v -> Boolean) -> {k: v} -> Maybe(k) |
766
|
|
|
* @param callable $predicate |
|
|
|
|
767
|
|
|
* @param array $list |
|
|
|
|
768
|
|
|
* @return mixed |
769
|
|
|
*/ |
770
|
|
|
function findLastIndex() { |
771
|
|
|
$findLastIndex = function($predicate, $list) { |
772
|
|
|
foreach (reverse(toPairs($list)) as $pair) { |
773
|
|
|
if($predicate($pair[1])) |
774
|
|
|
return $pair[0]; |
775
|
|
|
} |
776
|
|
|
return null; |
777
|
|
|
}; |
778
|
|
|
return apply(curry($findLastIndex), func_get_args()); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* Returns the first item satisfying the predicate in |
783
|
|
|
* the array or null if no such element is found. |
784
|
|
|
* ```php |
785
|
|
|
* find(startsWith('b'), ['foo', 'bar', 'baz']); // 'bar' |
786
|
|
|
* find(startsWith('c'), ['foo', 'bar', 'baz']); // null |
787
|
|
|
* ``` |
788
|
|
|
* |
789
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(a) |
790
|
|
|
* @param callable $predicate |
|
|
|
|
791
|
|
|
* @param array $list |
|
|
|
|
792
|
|
|
* @return mixed |
793
|
|
|
*/ |
794
|
|
|
function find() { |
795
|
|
|
$find = function($predicate, $list) { |
796
|
|
|
return get(findIndex($predicate, $list), $list); |
797
|
|
|
}; |
798
|
|
|
return apply(curry($find), func_get_args()); |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* Returns the last item satisfying the predicate in |
803
|
|
|
* the array or null if no such element is found. |
804
|
|
|
* ```php |
805
|
|
|
* findLast(startsWith('b'), ['foo', 'bar', 'baz']); // 'baz' |
806
|
|
|
* findLast(startsWith('c'), ['foo', 'bar', 'baz']); // null |
807
|
|
|
* ``` |
808
|
|
|
* |
809
|
|
|
* @signature (a -> Boolean) -> [a] -> Maybe(a) |
810
|
|
|
* @param callable $predicate |
|
|
|
|
811
|
|
|
* @param array $list |
|
|
|
|
812
|
|
|
* @return mixed |
813
|
|
|
*/ |
814
|
|
|
function findLast() { |
815
|
|
|
$findLast = function($predicate, $list) { |
816
|
|
|
return get(findLastIndex($predicate, $list), $list); |
817
|
|
|
}; |
818
|
|
|
return apply(curry($findLast), func_get_args()); |
819
|
|
|
} |
820
|
|
|
|
821
|
|
|
/** |
822
|
|
|
* If `$list` is an array, it returns the **key** of the first **item** which is equal to `$item` or `null` if not found. |
823
|
|
|
* If `$list` is a string, it returns the **position** of the first **substring** which is equal to `$item` or `null` if not found. |
824
|
|
|
* If `$list` is an object, it returns the **name** of the first **attribute** which is equal to `$item` or `null` if not found. |
825
|
|
|
* Note that elements are deeply compared using `Tarsana\Functional\equals`. |
826
|
|
|
* ```php |
827
|
|
|
* indexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); // 1 |
828
|
|
|
* indexOf(['Hello'], [1, ['Hello', 'World'], true]); // -1 |
829
|
|
|
* indexOf('World', 'Hello World'); // 6 |
830
|
|
|
* indexOf('World !', 'Hello World'); // -1 |
831
|
|
|
* indexOf('foo', (object) ['name' => 'foo', 'age' => 11]); // 'name' |
832
|
|
|
* ``` |
833
|
|
|
* |
834
|
|
|
* @signature a -> [a] -> Maybe(Number) |
835
|
|
|
* @signature v -> {k: v} -> Maybe(k) |
836
|
|
|
* @signature String -> String -> Maybe(Number) |
837
|
|
|
* @param mixed $item |
|
|
|
|
838
|
|
|
* @param array $list |
|
|
|
|
839
|
|
|
* @return int |
840
|
|
|
*/ |
841
|
|
View Code Duplication |
function indexOf() { |
|
|
|
|
842
|
|
|
$indexOf = function($item, $list) { |
843
|
|
|
if (is_string($list)) { |
844
|
|
|
$index = strpos($list, $item); |
845
|
|
|
return (-1 == $index) |
846
|
|
|
? null |
847
|
|
|
: $index; |
848
|
|
|
} |
849
|
|
|
return findIndex(equals($item), $list); |
850
|
|
|
}; |
851
|
|
|
return apply(curry($indexOf), func_get_args()); |
852
|
|
|
} |
853
|
|
|
|
854
|
|
|
/** |
855
|
|
|
* Same as `indexOf` but returns the key/position/name of the last item/substring/attribute. |
856
|
|
|
* ```php |
857
|
|
|
* lastIndexOf(['Hello', 'World'], [1, ['Hello', 'World'], true, ['Hello', 'World']]); // 3 |
858
|
|
|
* lastIndexOf(['Hello'], [1, ['Hello', 'World'], true]); // -1 |
859
|
|
|
* lastIndexOf('World', 'Hello World'); // 6 |
860
|
|
|
* lastIndexOf('World !', 'Hello World'); // -1 |
861
|
|
|
* lastIndexOf('foo', (object) ['name' => 'foo', 'age' => 11]); // 'name' |
862
|
|
|
* ``` |
863
|
|
|
* |
864
|
|
|
* @signature a -> [a] -> Number |
865
|
|
|
* @signature String -> String -> Number |
866
|
|
|
* @param mixed $item |
|
|
|
|
867
|
|
|
* @param array $list |
|
|
|
|
868
|
|
|
* @return int |
869
|
|
|
*/ |
870
|
|
View Code Duplication |
function lastIndexOf() { |
|
|
|
|
871
|
|
|
$lastIndexOf = function($item, $list) { |
872
|
|
|
if (is_string($list)) { |
873
|
|
|
$index = strrpos($list, $item); |
874
|
|
|
return (-1 == $index) |
875
|
|
|
? null |
876
|
|
|
: $index; |
877
|
|
|
} |
878
|
|
|
return findLastIndex(equals($item), $list); |
879
|
|
|
}; |
880
|
|
|
return apply(curry($lastIndexOf), func_get_args()); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* Removes duplicates from a list by keeping the first occurence of each group |
885
|
|
|
* of items which are equivalent according to the given `$areEqual` callable. |
886
|
|
|
* ```php |
887
|
|
|
* uniqueBy(eq(), [1, '2', '1', 3, '3', 2, 2]); // [1, '2', 3] |
888
|
|
|
* ``` |
889
|
|
|
* |
890
|
|
|
* @signature (a -> a -> Boolean) -> [a] -> [a] |
891
|
|
|
* @param callable $areEqual |
|
|
|
|
892
|
|
|
* @param array $list |
|
|
|
|
893
|
|
|
* @return array |
894
|
|
|
*/ |
895
|
|
|
function uniqueBy() { |
896
|
|
|
$uniqueBy = function($areEqual, $list) { |
897
|
|
|
// make sure the compare function is curried |
898
|
|
|
$compare = function($a) use($areEqual) { |
|
|
|
|
899
|
|
|
return function($b) use($areEqual) { |
900
|
|
|
return $areEqual($a, $b); |
|
|
|
|
901
|
|
|
}; |
902
|
|
|
}; |
903
|
|
|
|
904
|
|
|
return reduce(function($result, $item) { |
905
|
|
|
return (null == findIndex($compare($item), $result)) |
|
|
|
|
906
|
|
|
? append($item, $result) |
907
|
|
|
: $result; |
908
|
|
|
}, [], $list); |
909
|
|
|
}; |
910
|
|
|
return apply(curry($uniqueBy), func_get_args()); |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
/** |
914
|
|
|
* Alias of `uniqueBy(equals())`. |
915
|
|
|
* ```php |
916
|
|
|
* unique([1, '1', [1, 2], 1, ['1', 2], [1, 2]]); // [1, '1', [1, 2], ['1', 2]] |
917
|
|
|
* ``` |
918
|
|
|
* |
919
|
|
|
* @signature [a] -> [a] |
920
|
|
|
* @param array $list |
|
|
|
|
921
|
|
|
* @return array |
922
|
|
|
*/ |
923
|
|
|
function unique() { |
924
|
|
|
return apply(curry(uniqueBy(equals())), func_get_args()); |
925
|
|
|
} |
926
|
|
|
|
927
|
|
|
/** |
928
|
|
|
* Converts an array to an associative array, based on the result of calling `$fn` |
929
|
|
|
* on each element, and grouping the results according to values returned. |
930
|
|
|
* Note that `$fn` should take an item from the list and return a string. |
931
|
|
|
* ```php |
932
|
|
|
* $persons = [ |
933
|
|
|
* ['name' => 'foo', 'age' => 11], |
934
|
|
|
* ['name' => 'bar', 'age' => 9], |
935
|
|
|
* ['name' => 'baz', 'age' => 16], |
936
|
|
|
* ['name' => 'zeta', 'age' => 33], |
937
|
|
|
* ['name' => 'beta', 'age' => 25] |
938
|
|
|
* ]; |
939
|
|
|
* $phase = function($person) { |
940
|
|
|
* $age = $person['age']; |
941
|
|
|
* return $age < 13 ? 'child' : |
942
|
|
|
* $age < 19 ? 'teenager' : |
943
|
|
|
* 'adult'; |
944
|
|
|
* }; |
945
|
|
|
* groupBy($phase, $persons); |
946
|
|
|
* // [ |
947
|
|
|
* // 'child' => [['name' => 'foo', 'age' => 11], ['name' => 'bar', 'age' => 9]], |
948
|
|
|
* // 'teenager' => [['name' => 'baz', 'age' => 16]], |
949
|
|
|
* // 'adult' => [['name' => 'zeta', 'age' => 33], ['name' => 'beta', 'age' => 25]] |
950
|
|
|
* // ] |
951
|
|
|
* ``` |
952
|
|
|
* |
953
|
|
|
* @signature (a -> String) -> [a] -> {String: a} |
954
|
|
|
* @param callable $fn |
|
|
|
|
955
|
|
|
* @param array $list |
|
|
|
|
956
|
|
|
* @return array |
957
|
|
|
*/ |
958
|
|
|
function groupBy() { |
959
|
|
|
$groupBy = function($fn, $list) { |
|
|
|
|
960
|
|
|
return reduce(function($result, $item) use($fn) { |
961
|
|
|
$index = $fn($item); |
962
|
|
|
if (! isset($result[$index])) |
963
|
|
|
$result[$index] = []; |
964
|
|
|
$result[$index][] = $item; |
965
|
|
|
return $result; |
966
|
|
|
}, [], $list); |
967
|
|
|
}; |
968
|
|
|
return apply(curry(uniqueBy(equals())), func_get_args()); |
969
|
|
|
} |
970
|
|
|
|
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.