@@ 235-244 (lines=10) @@ | ||
232 | * @param callable $function ($value, $key) |
|
233 | * @return Collection |
|
234 | */ |
|
235 | function map($collection, callable $function) |
|
236 | { |
|
237 | $generatorFactory = function () use ($collection, $function) { |
|
238 | foreach ($collection as $key => $value) { |
|
239 | yield $key => $function($value, $key); |
|
240 | } |
|
241 | }; |
|
242 | ||
243 | return new Collection($generatorFactory); |
|
244 | } |
|
245 | ||
246 | /** |
|
247 | * Returns a lazy collection of items from $collection for which $function returns true. |
|
@@ 253-264 (lines=12) @@ | ||
250 | * @param callable $function ($value, $key) |
|
251 | * @return Collection |
|
252 | */ |
|
253 | function filter($collection, callable $function) |
|
254 | { |
|
255 | $generatorFactory = function () use ($collection, $function) { |
|
256 | foreach ($collection as $key => $value) { |
|
257 | if ($function($value, $key)) { |
|
258 | yield $key => $value; |
|
259 | } |
|
260 | } |
|
261 | }; |
|
262 | ||
263 | return new Collection($generatorFactory); |
|
264 | } |
|
265 | ||
266 | /** |
|
267 | * Returns a lazy collection with items from all $collections passed as argument appended together |
|
@@ 449-460 (lines=12) @@ | ||
446 | * @param callable $function ($value, $key) |
|
447 | * @return Collection |
|
448 | */ |
|
449 | function each($collection, callable $function) |
|
450 | { |
|
451 | $generatorFactory = function () use ($collection, $function) { |
|
452 | foreach ($collection as $key => $value) { |
|
453 | $function($value, $key); |
|
454 | ||
455 | yield $key => $value; |
|
456 | } |
|
457 | }; |
|
458 | ||
459 | return new Collection($generatorFactory); |
|
460 | } |
|
461 | ||
462 | /** |
|
463 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 525-534 (lines=10) @@ | ||
522 | * @param callable $function ($value, $key) |
|
523 | * @return Collection |
|
524 | */ |
|
525 | function indexBy($collection, callable $function) |
|
526 | { |
|
527 | $generatorFactory = function () use ($collection, $function) { |
|
528 | foreach ($collection as $key => $value) { |
|
529 | yield $function($value, $key) => $value; |
|
530 | } |
|
531 | }; |
|
532 | ||
533 | return new Collection($generatorFactory); |
|
534 | } |
|
535 | ||
536 | /** |
|
537 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |