|
1
|
|
|
<?php namespace Tarsana\Functional; |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains some useful functions to handle arrays. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Gets the value of a key. |
|
8
|
|
|
* ```php |
|
9
|
|
|
* $data = [ |
|
10
|
|
|
* ['name' => 'foo', 'type' => 'test'], |
|
11
|
|
|
* ['name' => 'bar', 'type' => 'test'] |
|
12
|
|
|
* ]; |
|
13
|
|
|
* $nameOf = value('name'); |
|
14
|
|
|
* value(0, $data) // ['name' => 'foo', 'type' => 'test'] |
|
15
|
|
|
* $nameOf($data[1]) // 'bar' |
|
16
|
|
|
* ``` |
|
17
|
|
|
* |
|
18
|
|
|
* @signature String -> [key => *] -> * |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* @param array $array |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
// TODO: handle stdClass |
|
24
|
|
|
function value() { |
|
25
|
|
|
$value = function($name, $array){ |
|
26
|
|
|
return $array[$name]; |
|
27
|
|
|
}; |
|
28
|
|
|
return apply(curry($value), func_get_args()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Curried version of `array_map()`. |
|
33
|
|
|
* ```php |
|
34
|
|
|
* $doubles = map(function($x) { return 2 * $x; }); |
|
35
|
|
|
* $doubles([1, 2, 3, 4]) // [2, 4, 6, 8] |
|
36
|
|
|
* ``` |
|
37
|
|
|
* |
|
38
|
|
|
* @signature (a -> b) -> [a] -> [b] |
|
39
|
|
|
* @param callable $fn |
|
|
|
|
|
|
40
|
|
|
* @param array $array |
|
|
|
|
|
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
function map() { |
|
44
|
|
|
return apply(curry(function($fn, $array){ |
|
45
|
|
|
return array_map($fn, $array); |
|
46
|
|
|
}), func_get_args()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Curried version of `array_filter` with modified order of |
|
51
|
|
|
* arguments. The callback is the first argument then the array. |
|
52
|
|
|
* ```php |
|
53
|
|
|
* $array = [1, 'aa', 3, [4, 5]]; |
|
54
|
|
|
* $numeric = F\filter('is_numeric'); |
|
55
|
|
|
* $numeric($array) // [1, 3] |
|
56
|
|
|
* ``` |
|
57
|
|
|
* @signature (a -> Boolean) -> [a] -> [a] |
|
58
|
|
|
* @param callable $fn |
|
|
|
|
|
|
59
|
|
|
* @param array $array |
|
|
|
|
|
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
function filter() { |
|
63
|
|
|
$filter = function($fn, $array){ |
|
64
|
|
|
return array_values(array_filter($array, $fn)); |
|
65
|
|
|
}; |
|
66
|
|
|
return apply(curry($filter), func_get_args()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// TODO |
|
70
|
|
|
function where() {} |
|
71
|
|
|
// where([name => startsWith('A')], [{name: Amine, ...},...]) == [{name: Amine,...},...] |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Curried version of `array_reduce` with modified order of |
|
75
|
|
|
* arguments ($callback, $initial, $array). |
|
76
|
|
|
* ```php |
|
77
|
|
|
* $array = [1, 2, 3, 4]; |
|
78
|
|
|
* $sum = reduce('Tarsana\Functional\plus', 0); |
|
79
|
|
|
* $sum($array) // 10 |
|
80
|
|
|
* ``` |
|
81
|
|
|
* |
|
82
|
|
|
* @signature (* -> a -> *) -> * -> [a] -> * |
|
83
|
|
|
* @param callable $fn |
|
|
|
|
|
|
84
|
|
|
* @param mixed $initial |
|
|
|
|
|
|
85
|
|
|
* @param array $array |
|
|
|
|
|
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
function reduce() { |
|
89
|
|
|
$reduce = function($fn, $initial, $array){ |
|
90
|
|
|
return array_reduce($array, $fn, $initial); |
|
91
|
|
|
}; |
|
92
|
|
|
return apply(curry($reduce), func_get_args()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Applies the callback to each item and returns the original array. |
|
97
|
|
|
* ```php |
|
98
|
|
|
* $array = [1, 2, 3, 4]; |
|
99
|
|
|
* each(function($item){ |
|
100
|
|
|
* echo $item, PHP_EOL; |
|
101
|
|
|
* }, $array); |
|
102
|
|
|
* // Outputs: |
|
103
|
|
|
* // 1 |
|
104
|
|
|
* // 2 |
|
105
|
|
|
* // 3 |
|
106
|
|
|
* // 4 |
|
107
|
|
|
* ``` |
|
108
|
|
|
* |
|
109
|
|
|
* @signature (a -> *) -> [a] -> [a] |
|
110
|
|
|
* @param callable $fn |
|
|
|
|
|
|
111
|
|
|
* @param array $array |
|
|
|
|
|
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
function each() { |
|
115
|
|
|
$each = function($fn, $array){ |
|
116
|
|
|
foreach ($array as $item) { |
|
117
|
|
|
apply($fn, [$item]); |
|
118
|
|
|
} |
|
119
|
|
|
return $array; |
|
120
|
|
|
}; |
|
121
|
|
|
return apply(curry($each), func_get_args()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Returns the first item of the given array or string. |
|
126
|
|
|
* ```php |
|
127
|
|
|
* head([1, 2, 3, 4]) // 1 |
|
128
|
|
|
* head('Hello') // 'H' |
|
129
|
|
|
* head([]) // null |
|
130
|
|
|
* head('') // '' |
|
131
|
|
|
* ``` |
|
132
|
|
|
* |
|
133
|
|
|
* @signature [a] -> a |
|
134
|
|
|
* @signature String -> String |
|
135
|
|
|
* @param array|string $array |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
function head($array) { |
|
|
|
|
|
|
139
|
|
|
if(is_string($array)) |
|
140
|
|
|
return substr($array, 0, 1); |
|
141
|
|
|
return (count($array) > 0) |
|
142
|
|
|
? $array[0] |
|
143
|
|
|
: null; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Returns the last item of the given array or string. |
|
148
|
|
|
* ```php |
|
149
|
|
|
* last([1, 2, 3, 4]) // 4 |
|
150
|
|
|
* last('Hello') // 'o' |
|
151
|
|
|
* last([]) // null |
|
152
|
|
|
* last('') // '' |
|
153
|
|
|
* ``` |
|
154
|
|
|
* |
|
155
|
|
|
* @signature [a] -> a |
|
156
|
|
|
* @signature String -> String |
|
157
|
|
|
* @param array|string $array |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
View Code Duplication |
function last($array) { |
|
|
|
|
|
|
161
|
|
|
if(is_string($array)) |
|
162
|
|
|
return substr($array, -1); |
|
163
|
|
|
return (count($array) > 0) |
|
164
|
|
|
? $array[count($array) - 1] |
|
165
|
|
|
: null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Returns all but the last element of the given array or string. |
|
170
|
|
|
* ```php |
|
171
|
|
|
* init([1, 2, 3, 4]) // [1, 2, 3] |
|
172
|
|
|
* init('Hello') // 'Hell' |
|
173
|
|
|
* init([7]) // [] |
|
174
|
|
|
* init([]) // [] |
|
175
|
|
|
* init('') // '' |
|
176
|
|
|
* ``` |
|
177
|
|
|
* |
|
178
|
|
|
* @signature [a] -> a |
|
179
|
|
|
* @signature String -> String |
|
180
|
|
|
* @param array|string $array |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
function init($array) { |
|
184
|
|
View Code Duplication |
if(is_string($array)) |
|
|
|
|
|
|
185
|
|
|
return (strlen($array) > 1) |
|
186
|
|
|
? substr($array, 0, strlen($array) - 1) |
|
187
|
|
|
: ''; |
|
188
|
|
|
return (count($array) > 1) |
|
189
|
|
|
? array_slice($array, 0, count($array) - 1) |
|
190
|
|
|
: []; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns all but the first element of the given array or string. |
|
195
|
|
|
* ```php |
|
196
|
|
|
* tail([1, 2, 3, 4]) // [2, 3, 4] |
|
197
|
|
|
* tail('Hello') // 'ello' |
|
198
|
|
|
* tail([7]) // [] |
|
199
|
|
|
* tail([]) // [] |
|
200
|
|
|
* tail('') // '' |
|
201
|
|
|
* ``` |
|
202
|
|
|
* |
|
203
|
|
|
* @signature [a] -> a |
|
204
|
|
|
* @signature String -> String |
|
205
|
|
|
* @param array|string $array |
|
206
|
|
|
* @return array |
|
207
|
|
|
*/ |
|
208
|
|
|
function tail($array) { |
|
209
|
|
View Code Duplication |
if(is_string($array)) |
|
|
|
|
|
|
210
|
|
|
return (strlen($array) > 1) |
|
211
|
|
|
? substr($array, 1) |
|
212
|
|
|
: ''; |
|
213
|
|
|
return (count($array) > 1) |
|
214
|
|
|
? array_slice($array, 1) |
|
215
|
|
|
: []; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Alias of `array_reverse()` and `strrev()`. |
|
220
|
|
|
* ```php |
|
221
|
|
|
* reverse([1, 2, 3, 4]) // [4, 3, 2, 1] |
|
222
|
|
|
* reverse('Hello') // 'olleH' |
|
223
|
|
|
* ``` |
|
224
|
|
|
* |
|
225
|
|
|
* @signature [a] -> [a] |
|
226
|
|
|
* @signature String -> String |
|
227
|
|
|
* @param array|string $array |
|
228
|
|
|
* @return array |
|
229
|
|
|
*/ |
|
230
|
|
|
function reverse($array) { |
|
231
|
|
|
return is_string($array) |
|
232
|
|
|
? strrev($array) |
|
233
|
|
|
: array_reverse($array); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Alias for `count()` and `strlen()`. |
|
238
|
|
|
* ```php |
|
239
|
|
|
* length([1, 2, 3, 4]) // 4 |
|
240
|
|
|
* length('Hello') // 5 |
|
241
|
|
|
* ``` |
|
242
|
|
|
* |
|
243
|
|
|
* @signature [a] -> Number |
|
244
|
|
|
* @signature String -> Number |
|
245
|
|
|
* @param array|string $array |
|
246
|
|
|
* @return int |
|
247
|
|
|
*/ |
|
248
|
|
|
function length($array) { |
|
249
|
|
|
return is_string($array) |
|
250
|
|
|
? strlen($array) |
|
251
|
|
|
: count($array); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Checks if the `$predicate` is verified by **all** items of the array. |
|
256
|
|
|
* ```php |
|
257
|
|
|
* $allNotNull = all(notEq(0)); |
|
258
|
|
|
* $allNotNull([9, 3, 2, 4]); // true |
|
259
|
|
|
* $allNotNull([9, 3, 0, 4]); // false |
|
260
|
|
|
* ``` |
|
261
|
|
|
* |
|
262
|
|
|
* @signature (a -> Boolean) -> [a] -> Boolean |
|
263
|
|
|
* @param callable $predicate |
|
|
|
|
|
|
264
|
|
|
* @param array $array |
|
|
|
|
|
|
265
|
|
|
* @return bool |
|
266
|
|
|
*/ |
|
267
|
|
|
function all() { |
|
268
|
|
|
$all = function($predicate, $array) { |
|
269
|
|
|
return length(filter($predicate, $array)) == length($array); |
|
270
|
|
|
}; |
|
271
|
|
|
return apply(curry($all), func_get_args()); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Checks if the `$predicate` is verified by **any** items of the array. |
|
277
|
|
|
* ```php |
|
278
|
|
|
* $anyNumeric = any('is_numeric'); |
|
279
|
|
|
* $anyNumeric(['Hello', '12', []]); // true |
|
280
|
|
|
* $anyNumeric(['Hello', 'Foo']); // false |
|
281
|
|
|
* ``` |
|
282
|
|
|
* |
|
283
|
|
|
* @signature (a -> Boolean) -> [a] -> Boolean |
|
284
|
|
|
* @param callable $predicate |
|
|
|
|
|
|
285
|
|
|
* @param array $array |
|
|
|
|
|
|
286
|
|
|
* @return bool |
|
287
|
|
|
*/ |
|
288
|
|
|
function any() { |
|
289
|
|
|
// TODO: use findBy when available instead of filter ! |
|
290
|
|
|
$any = function($predicate, $array) { |
|
291
|
|
|
return length(filter($predicate, $array)) > 0; |
|
292
|
|
|
}; |
|
293
|
|
|
return apply(curry($any), func_get_args()); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Concatenates two arrays or strings. |
|
298
|
|
|
* ```php |
|
299
|
|
|
* concat([1, 2], [3, 4]) // [1, 2, 3, 4] |
|
300
|
|
|
* concat('Hello ', 'World') // 'Hello World' |
|
301
|
|
|
* ``` |
|
302
|
|
|
* |
|
303
|
|
|
* @signature [*] -> [*] -> [*] |
|
304
|
|
|
* @param array $array1 |
|
|
|
|
|
|
305
|
|
|
* @param array $array2 |
|
|
|
|
|
|
306
|
|
|
* @return array |
|
307
|
|
|
*/ |
|
308
|
|
View Code Duplication |
function concat() { |
|
|
|
|
|
|
309
|
|
|
$concat = function($array1, $array2) { |
|
310
|
|
|
if (is_string($array1)) |
|
311
|
|
|
return $array1 . $array2; |
|
312
|
|
|
return array_merge($array1, $array2); |
|
313
|
|
|
}; |
|
314
|
|
|
return apply(curry($concat), func_get_args()); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Appends an item to an array. |
|
319
|
|
|
* ```php |
|
320
|
|
|
* append(5, [1, 2, 3]) // [1, 2, 3, 5] |
|
321
|
|
|
* append(' World', 'Hello') // 'Hello World' |
|
322
|
|
|
* ``` |
|
323
|
|
|
* |
|
324
|
|
|
* @signature * -> [*] -> [*] |
|
325
|
|
|
* @signature String -> String -> String |
|
326
|
|
|
* @param mixed $item |
|
|
|
|
|
|
327
|
|
|
* @param array $array |
|
|
|
|
|
|
328
|
|
|
* @return array |
|
329
|
|
|
*/ |
|
330
|
|
View Code Duplication |
function append() { |
|
|
|
|
|
|
331
|
|
|
$append = function ($item, $array) { |
|
332
|
|
|
if (is_string($array)) |
|
333
|
|
|
return $array . $item; |
|
334
|
|
|
return array_merge($array, [$item]); |
|
335
|
|
|
}; |
|
336
|
|
|
return apply(curry($append), func_get_args()); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* Adds an item to teh first of an array. |
|
341
|
|
|
* ```php |
|
342
|
|
|
* prepend(5, [1, 2, 3]) // [5, 1, 2, 3] |
|
343
|
|
|
* prepend('Hello ', 'World') // 'Hello World' |
|
344
|
|
|
* ``` |
|
345
|
|
|
* |
|
346
|
|
|
* @signature * -> [*] -> [*] |
|
347
|
|
|
* @signature String -> String -> String |
|
348
|
|
|
* @param mixed $item |
|
|
|
|
|
|
349
|
|
|
* @param array $array |
|
|
|
|
|
|
350
|
|
|
* @return array |
|
351
|
|
|
*/ |
|
352
|
|
View Code Duplication |
function prepend() { |
|
|
|
|
|
|
353
|
|
|
$prepend = function ($item, $array) { |
|
354
|
|
|
if (is_string($array)) |
|
355
|
|
|
return $item . $array; |
|
356
|
|
|
return array_merge([$item], $array); |
|
357
|
|
|
}; |
|
358
|
|
|
return apply(curry($prepend), func_get_args()); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Takes a number of elements from an array. If `$count` is negative, |
|
363
|
|
|
* the elements are taken from the end of the array. |
|
364
|
|
|
* ```php |
|
365
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
|
366
|
|
|
* take(2, $items) // ['Foo', 'Bar'] |
|
367
|
|
|
* take(0, $items) // [] |
|
368
|
|
|
* take(-2, $items) // ['Bar', 'Baz'] |
|
369
|
|
|
* take(5, 'Hello World') // 'Hello' |
|
370
|
|
|
* take(-5, 'Hello World') // 'World' |
|
371
|
|
|
* ``` |
|
372
|
|
|
* |
|
373
|
|
|
* @signature Number -> [a] -> [a] |
|
374
|
|
|
* @signature Number -> String -> String |
|
375
|
|
|
* @param int $count |
|
|
|
|
|
|
376
|
|
|
* @param array $array |
|
|
|
|
|
|
377
|
|
|
* @return array |
|
378
|
|
|
*/ |
|
379
|
|
View Code Duplication |
function take() { |
|
|
|
|
|
|
380
|
|
|
$take = function($count, $array) { |
|
381
|
|
|
if(is_string($array)) { |
|
382
|
|
|
return ($count >= 0) |
|
383
|
|
|
? substr($array, 0, $count) |
|
384
|
|
|
: substr($array, $count); |
|
385
|
|
|
} |
|
386
|
|
|
return ($count >= 0) |
|
387
|
|
|
? array_slice($array, 0, $count) |
|
388
|
|
|
: array_slice($array, $count); |
|
389
|
|
|
}; |
|
390
|
|
|
return apply(curry($take), func_get_args()); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
// TODO |
|
394
|
|
|
function takeLast() {} |
|
395
|
|
|
function takeWhile() {} |
|
396
|
|
|
function takeLastWhile() {} |
|
397
|
|
|
function takeWhere() {} // alias of filter |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* Removes a number of elements from an array. |
|
401
|
|
|
* If `$count` is negative, the elements are |
|
402
|
|
|
* removed from the end of the array. |
|
403
|
|
|
* ```php |
|
404
|
|
|
* $items = ['Foo', 'Bar', 'Baz']; |
|
405
|
|
|
* remove(2, $items) // ['Baz'] |
|
406
|
|
|
* remove(-1, $items) // ['Foo', 'Bar'] |
|
407
|
|
|
* remove(5, $items) // [] |
|
408
|
|
|
* remove(6, 'Hello World') // 'World' |
|
409
|
|
|
* remove(-6, 'Hello World') // 'Hello' |
|
410
|
|
|
* ``` |
|
411
|
|
|
* |
|
412
|
|
|
* @signature Number -> [a] -> [a] |
|
413
|
|
|
* @signature Number -> String -> String |
|
414
|
|
|
* @param int $count |
|
|
|
|
|
|
415
|
|
|
* @param array $array |
|
|
|
|
|
|
416
|
|
|
* @return array |
|
417
|
|
|
*/ |
|
418
|
|
View Code Duplication |
function remove() { |
|
|
|
|
|
|
419
|
|
|
$remove = function($count, $array) { |
|
420
|
|
|
if(is_string($array)) { |
|
421
|
|
|
return ($count >= 0) |
|
422
|
|
|
? substr($array, $count) |
|
423
|
|
|
: substr($array, 0, $count); |
|
424
|
|
|
} |
|
425
|
|
|
return ($count >= 0) |
|
426
|
|
|
? array_slice($array, $count) |
|
427
|
|
|
: array_slice($array, 0, $count); |
|
428
|
|
|
}; |
|
429
|
|
|
return apply(curry($remove), func_get_args()); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
// TODO |
|
433
|
|
|
function removeLast() {} |
|
434
|
|
|
function removeWhile() {} |
|
435
|
|
|
function removeLastWhile() {} |
|
436
|
|
|
function removeWhere() {} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Converts an associative array to an array of [key,value] pairs. |
|
440
|
|
|
* ```php |
|
441
|
|
|
* $array = ['key' => 'value', 'number' => 53, 'foo', 'bar']; |
|
442
|
|
|
* toPairs($array); // [['key', 'value'], ['number', 53], [0, 'foo'], [1, 'bar']] |
|
443
|
|
|
* ``` |
|
444
|
|
|
* |
|
445
|
|
|
* @signature [a => b] -> [(a,b)] |
|
446
|
|
|
* @param array $array |
|
447
|
|
|
* @return array |
|
448
|
|
|
*/ |
|
449
|
|
|
function toPairs($array) { |
|
450
|
|
|
return map(function($key) use($array) { |
|
451
|
|
|
return [$key, $array[$key]]; |
|
452
|
|
|
}, array_keys($array)); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
// TODO |
|
456
|
|
|
function fromPairs() {}// fromPairs([['a', 1], ['b', 2], ['c', 3]]) => {a: 1, b: 2, c: 3} |
|
|
|
|
|
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Applies a function to items of the array and concatenates the results. |
|
460
|
|
|
* This is also known as `flatMap` in some libraries. |
|
461
|
|
|
* ```php |
|
462
|
|
|
* $words = chain(split(' ')); |
|
463
|
|
|
* $words(['Hello World', 'How are you']) // ['Hello', 'World', 'How', 'are', 'you'] |
|
464
|
|
|
* ``` |
|
465
|
|
|
* |
|
466
|
|
|
* @signature (a -> [b]) -> [a] -> [b] |
|
467
|
|
|
* @param callable $fn |
|
|
|
|
|
|
468
|
|
|
* @param array $array |
|
|
|
|
|
|
469
|
|
|
* @return array |
|
470
|
|
|
*/ |
|
471
|
|
|
function chain() { |
|
472
|
|
|
$chain = function($fn, $array) { |
|
473
|
|
|
return reduce('Tarsana\\Functional\\concat', [], map($fn, $array)); |
|
474
|
|
|
}; |
|
475
|
|
|
return apply(curry($chain), func_get_args()); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Gets an array of slices of size `$size` from an array. |
|
480
|
|
|
* ```php |
|
481
|
|
|
* $pairs = slices(2); |
|
482
|
|
|
* $pairs([1, 2, 3, 4, 5]); // [[1, 2], [3, 4], [5]] |
|
483
|
|
|
* $pairs("Hello World"); // ['He', 'll', 'o ', 'Wo', 'rl', 'd'] |
|
484
|
|
|
* slices(5, [1, 2]); // [[1, 2]] |
|
485
|
|
|
* slices(3, []) // [] |
|
486
|
|
|
* slices(3, '') // '' |
|
487
|
|
|
* ``` |
|
488
|
|
|
* |
|
489
|
|
|
* @signature Number -> [a] -> [[a]] |
|
490
|
|
|
* @signature Number -> String -> [String] |
|
491
|
|
|
* @param int $size |
|
|
|
|
|
|
492
|
|
|
* @param array $array |
|
|
|
|
|
|
493
|
|
|
* @return array |
|
494
|
|
|
*/ |
|
495
|
|
|
function slices() { |
|
496
|
|
|
$slices = function($size, $array) { |
|
497
|
|
|
if(empty($array)) |
|
498
|
|
|
return is_string($array) ? '' : []; |
|
499
|
|
|
if(length($array) <= $size) |
|
500
|
|
|
return [$array]; |
|
501
|
|
|
return prepend(take($size, $array), slices($size, remove($size, $array))); |
|
502
|
|
|
}; |
|
503
|
|
|
return apply(curry($slices), func_get_args()); |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* Checks if an array contains an item. |
|
508
|
|
|
* ```php |
|
509
|
|
|
* contains('foo', ['foo', 'bar', 'baz']) // true |
|
510
|
|
|
* contains('hi', ['foo', 'bar', 'baz']) // false |
|
511
|
|
|
* contains('hi', 'Hello World') // false |
|
512
|
|
|
* contains('He', 'Hello World') // true |
|
513
|
|
|
* ``` |
|
514
|
|
|
* |
|
515
|
|
|
* @signature a -> [a] -> Boolean |
|
516
|
|
|
* @signature String -> String -> Boolean |
|
517
|
|
|
* @param mixed $item |
|
|
|
|
|
|
518
|
|
|
* @param array|string $array |
|
|
|
|
|
|
519
|
|
|
* @return bool |
|
520
|
|
|
*/ |
|
521
|
|
View Code Duplication |
function contains() { |
|
|
|
|
|
|
522
|
|
|
$contains = function($item, $array) { |
|
523
|
|
|
if(is_string($array)) |
|
524
|
|
|
return false !== strpos($array, $item); |
|
525
|
|
|
return in_array($item, $array); |
|
526
|
|
|
}; |
|
527
|
|
|
return apply(curry($contains), func_get_args()); |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
// TODO |
|
531
|
|
|
function find() {} |
|
532
|
|
|
function findIndex() {} |
|
533
|
|
|
function findLast() {} |
|
534
|
|
|
function findLastIndex() {} |
|
535
|
|
|
|
|
536
|
|
|
function unique() {} |
|
537
|
|
|
function uniqueBy() {} |
|
538
|
|
|
|
|
539
|
|
|
function flatten() {} |
|
540
|
|
|
|
|
541
|
|
|
function groupBy() {} |
|
542
|
|
|
// groupBy(value('tag'), [{name:'..', tag: 'A'}, ...]) // {'A': [{..}, {...}], 'B':[...]} |
|
|
|
|
|
|
543
|
|
|
|
|
544
|
|
|
function indexBy() {} |
|
545
|
|
|
// var list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}]; |
|
|
|
|
|
|
546
|
|
|
// R.indexBy(R.prop('id'), list); |
|
547
|
|
|
//=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}} |
|
548
|
|
|
|
|
549
|
|
|
function indexOf() {} |
|
550
|
|
|
function lastIndexOf() {} |
|
551
|
|
|
function insert() {} |
|
552
|
|
|
function insertAll() {} |
|
553
|
|
|
// insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4] |
|
|
|
|
|
|
554
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.