@@ 291-300 (lines=10) @@ | ||
288 | * |
|
289 | * @return Collection |
|
290 | */ |
|
291 | public function map(callable $callback) |
|
292 | { |
|
293 | $collection = Factory::create(); |
|
294 | $index = 0; |
|
295 | foreach ($this->items as $key => $value) { |
|
296 | $collection->set($key, $callback($value, $key, $index++)); |
|
297 | } |
|
298 | ||
299 | return $collection; |
|
300 | } |
|
301 | ||
302 | /** |
|
303 | * Map (in-place). |
|
@@ 328-339 (lines=12) @@ | ||
325 | * |
|
326 | * @return Collection |
|
327 | */ |
|
328 | public function filter(callable $callback) |
|
329 | { |
|
330 | $collection = Factory::create(); |
|
331 | $index = 0; |
|
332 | foreach ($this->items as $key => $value) { |
|
333 | if ($callback($value, $key, $index++)) { |
|
334 | $collection->set($key, $value); |
|
335 | } |
|
336 | } |
|
337 | ||
338 | return $collection; |
|
339 | } |
|
340 | ||
341 | /** |
|
342 | * Filter (in-place). |
|
@@ 454-463 (lines=10) @@ | ||
451 | * |
|
452 | * @return mixed |
|
453 | */ |
|
454 | public function reduce(callable $callback, $initial = null) |
|
455 | { |
|
456 | $accumulator = $initial; |
|
457 | $index = 0; |
|
458 | foreach ($this->items as $key => $value) { |
|
459 | $accumulator = $callback($accumulator, $value, $key, $index++); |
|
460 | } |
|
461 | ||
462 | return $accumulator; |
|
463 | } |
|
464 | ||
465 | /** |
|
466 | * Run the callback on each element (passive). |
@@ 25-34 (lines=10) @@ | ||
22 | /** |
|
23 | * {@inheritdoc} |
|
24 | */ |
|
25 | public function map(callable $callback) |
|
26 | { |
|
27 | $collection = Factory::create(); |
|
28 | $index = 0; |
|
29 | foreach ($this->items as $key => $value) { |
|
30 | $collection->set($key, $callback($value, $key, $index++)); |
|
31 | } |
|
32 | ||
33 | return $collection; |
|
34 | } |
|
35 | ||
36 | /** |
|
37 | * {@inheritdoc} |
|
@@ 39-50 (lines=12) @@ | ||
36 | /** |
|
37 | * {@inheritdoc} |
|
38 | */ |
|
39 | public function filter(callable $callback) |
|
40 | { |
|
41 | $collection = Factory::create(); |
|
42 | $index = 0; |
|
43 | foreach ($this->items as $key => $value) { |
|
44 | if ($callback($value, $key, $index++)) { |
|
45 | $collection->set($key, $value); |
|
46 | } |
|
47 | } |
|
48 | ||
49 | return $collection; |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * {@inheritdoc} |
|
@@ 55-64 (lines=10) @@ | ||
52 | /** |
|
53 | * {@inheritdoc} |
|
54 | */ |
|
55 | public function reduce(callable $callback, $initial = null) |
|
56 | { |
|
57 | $accumulator = $initial; |
|
58 | $index = 0; |
|
59 | foreach ($this->items as $key => $value) { |
|
60 | $accumulator = $callback($accumulator, $value, $key, $index++); |
|
61 | } |
|
62 | ||
63 | return $accumulator; |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * {@inheritdoc} |