@@ 238-247 (lines=10) @@ | ||
235 | * @param callable $function ($value, $key) |
|
236 | * @return Collection |
|
237 | */ |
|
238 | function map($collection, callable $function) |
|
239 | { |
|
240 | $generatorFactory = function () use ($collection, $function) { |
|
241 | foreach ($collection as $key => $value) { |
|
242 | yield $key => $function($value, $key); |
|
243 | } |
|
244 | }; |
|
245 | ||
246 | return new Collection($generatorFactory); |
|
247 | } |
|
248 | ||
249 | /** |
|
250 | * Returns a lazy collection of items from $collection for which $function returns true. |
|
@@ 256-267 (lines=12) @@ | ||
253 | * @param callable $function ($value, $key) |
|
254 | * @return Collection |
|
255 | */ |
|
256 | function filter($collection, callable $function) |
|
257 | { |
|
258 | $generatorFactory = function () use ($collection, $function) { |
|
259 | foreach ($collection as $key => $value) { |
|
260 | if ($function($value, $key)) { |
|
261 | yield $key => $value; |
|
262 | } |
|
263 | } |
|
264 | }; |
|
265 | ||
266 | return new Collection($generatorFactory); |
|
267 | } |
|
268 | ||
269 | /** |
|
270 | * Returns a lazy collection with items from $collection2 appended at the end of $collection1 |
|
@@ 424-435 (lines=12) @@ | ||
421 | * @param callable $function ($value, $key) |
|
422 | * @return Collection |
|
423 | */ |
|
424 | function each($collection, callable $function) |
|
425 | { |
|
426 | $generatorFactory = function () use ($collection, $function) { |
|
427 | foreach ($collection as $key => $value) { |
|
428 | $function($value, $key); |
|
429 | ||
430 | yield $key => $value; |
|
431 | } |
|
432 | }; |
|
433 | ||
434 | return new Collection($generatorFactory); |
|
435 | } |
|
436 | ||
437 | /** |
|
438 | * Returns an item with $key key from $collection. If that key is not present, throws ItemNotFound. |
|
@@ 500-509 (lines=10) @@ | ||
497 | * @param callable $function ($value, $key) |
|
498 | * @return Collection |
|
499 | */ |
|
500 | function indexBy($collection, callable $function) |
|
501 | { |
|
502 | $generatorFactory = function () use ($collection, $function) { |
|
503 | foreach ($collection as $key => $value) { |
|
504 | yield $function($value, $key) => $value; |
|
505 | } |
|
506 | }; |
|
507 | ||
508 | return new Collection($generatorFactory); |
|
509 | } |
|
510 | ||
511 | /** |
|
512 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |