@@ 234-243 (lines=10) @@ | ||
231 | * @param callable $function ($value, $key) |
|
232 | * @return Collection |
|
233 | */ |
|
234 | function map($collection, callable $function) |
|
235 | { |
|
236 | $generatorFactory = function () use ($collection, $function) { |
|
237 | foreach ($collection as $key => $value) { |
|
238 | yield $key => $function($value, $key); |
|
239 | } |
|
240 | }; |
|
241 | ||
242 | return new Collection($generatorFactory); |
|
243 | } |
|
244 | ||
245 | /** |
|
246 | * Returns a lazy collection of items from $collection for which $function returns true. |
|
@@ 252-263 (lines=12) @@ | ||
249 | * @param callable $function ($value, $key) |
|
250 | * @return Collection |
|
251 | */ |
|
252 | function filter($collection, callable $function) |
|
253 | { |
|
254 | $generatorFactory = function () use ($collection, $function) { |
|
255 | foreach ($collection as $key => $value) { |
|
256 | if ($function($value, $key)) { |
|
257 | yield $key => $value; |
|
258 | } |
|
259 | } |
|
260 | }; |
|
261 | ||
262 | return new Collection($generatorFactory); |
|
263 | } |
|
264 | ||
265 | /** |
|
266 | * Returns a lazy collection with items from all $collections passed as argument appended together |
|
@@ 420-431 (lines=12) @@ | ||
417 | * @param callable $function ($value, $key) |
|
418 | * @return Collection |
|
419 | */ |
|
420 | function each($collection, callable $function) |
|
421 | { |
|
422 | $generatorFactory = function () use ($collection, $function) { |
|
423 | foreach ($collection as $key => $value) { |
|
424 | $function($value, $key); |
|
425 | ||
426 | yield $key => $value; |
|
427 | } |
|
428 | }; |
|
429 | ||
430 | return new Collection($generatorFactory); |
|
431 | } |
|
432 | ||
433 | /** |
|
434 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 496-505 (lines=10) @@ | ||
493 | * @param callable $function ($value, $key) |
|
494 | * @return Collection |
|
495 | */ |
|
496 | function indexBy($collection, callable $function) |
|
497 | { |
|
498 | $generatorFactory = function () use ($collection, $function) { |
|
499 | foreach ($collection as $key => $value) { |
|
500 | yield $function($value, $key) => $value; |
|
501 | } |
|
502 | }; |
|
503 | ||
504 | return new Collection($generatorFactory); |
|
505 | } |
|
506 | ||
507 | /** |
|
508 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |