@@ 232-241 (lines=10) @@ | ||
229 | * @param callable $function ($value, $key) |
|
230 | * @return Collection |
|
231 | */ |
|
232 | function map($collection, callable $function) |
|
233 | { |
|
234 | $generatorFactory = function () use ($collection, $function) { |
|
235 | foreach ($collection as $key => $value) { |
|
236 | yield $key => $function($value, $key); |
|
237 | } |
|
238 | }; |
|
239 | ||
240 | return new Collection($generatorFactory); |
|
241 | } |
|
242 | ||
243 | /** |
|
244 | * Returns a lazy collection of items from $collection for which $function returns true. |
|
@@ 452-463 (lines=12) @@ | ||
449 | * @param callable $function ($value, $key) |
|
450 | * @return Collection |
|
451 | */ |
|
452 | function each($collection, callable $function) |
|
453 | { |
|
454 | $generatorFactory = function () use ($collection, $function) { |
|
455 | foreach ($collection as $key => $value) { |
|
456 | $function($value, $key); |
|
457 | ||
458 | yield $key => $value; |
|
459 | } |
|
460 | }; |
|
461 | ||
462 | return new Collection($generatorFactory); |
|
463 | } |
|
464 | ||
465 | /** |
|
466 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 528-537 (lines=10) @@ | ||
525 | * @param callable $function ($value, $key) |
|
526 | * @return Collection |
|
527 | */ |
|
528 | function indexBy($collection, callable $function) |
|
529 | { |
|
530 | $generatorFactory = function () use ($collection, $function) { |
|
531 | foreach ($collection as $key => $value) { |
|
532 | yield $function($value, $key) => $value; |
|
533 | } |
|
534 | }; |
|
535 | ||
536 | return new Collection($generatorFactory); |
|
537 | } |
|
538 | ||
539 | /** |
|
540 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |