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