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