@@ 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 |
|
@@ 442-453 (lines=12) @@ | ||
439 | * @param callable $function ($value, $key) |
|
440 | * @return Collection |
|
441 | */ |
|
442 | function each($collection, callable $function) |
|
443 | { |
|
444 | $generatorFactory = function () use ($collection, $function) { |
|
445 | foreach ($collection as $key => $value) { |
|
446 | $function($value, $key); |
|
447 | ||
448 | yield $key => $value; |
|
449 | } |
|
450 | }; |
|
451 | ||
452 | return new Collection($generatorFactory); |
|
453 | } |
|
454 | ||
455 | /** |
|
456 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 518-527 (lines=10) @@ | ||
515 | * @param callable $function ($value, $key) |
|
516 | * @return Collection |
|
517 | */ |
|
518 | function indexBy($collection, callable $function) |
|
519 | { |
|
520 | $generatorFactory = function () use ($collection, $function) { |
|
521 | foreach ($collection as $key => $value) { |
|
522 | yield $function($value, $key) => $value; |
|
523 | } |
|
524 | }; |
|
525 | ||
526 | return new Collection($generatorFactory); |
|
527 | } |
|
528 | ||
529 | /** |
|
530 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |