@@ 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. |
|
@@ 251-262 (lines=12) @@ | ||
248 | * @param callable $function ($value, $key) |
|
249 | * @return Collection |
|
250 | */ |
|
251 | function filter($collection, callable $function) |
|
252 | { |
|
253 | $generatorFactory = function () use ($collection, $function) { |
|
254 | foreach ($collection as $key => $value) { |
|
255 | if ($function($value, $key)) { |
|
256 | yield $key => $value; |
|
257 | } |
|
258 | } |
|
259 | }; |
|
260 | ||
261 | return new Collection($generatorFactory); |
|
262 | } |
|
263 | ||
264 | /** |
|
265 | * Returns a lazy collection with items from all $collections passed as argument appended together |
|
@@ 447-458 (lines=12) @@ | ||
444 | * @param callable $function ($value, $key) |
|
445 | * @return Collection |
|
446 | */ |
|
447 | function each($collection, callable $function) |
|
448 | { |
|
449 | $generatorFactory = function () use ($collection, $function) { |
|
450 | foreach ($collection as $key => $value) { |
|
451 | $function($value, $key); |
|
452 | ||
453 | yield $key => $value; |
|
454 | } |
|
455 | }; |
|
456 | ||
457 | return new Collection($generatorFactory); |
|
458 | } |
|
459 | ||
460 | /** |
|
461 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 523-532 (lines=10) @@ | ||
520 | * @param callable $function ($value, $key) |
|
521 | * @return Collection |
|
522 | */ |
|
523 | function indexBy($collection, callable $function) |
|
524 | { |
|
525 | $generatorFactory = function () use ($collection, $function) { |
|
526 | foreach ($collection as $key => $value) { |
|
527 | yield $function($value, $key) => $value; |
|
528 | } |
|
529 | }; |
|
530 | ||
531 | return new Collection($generatorFactory); |
|
532 | } |
|
533 | ||
534 | /** |
|
535 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |