| @@ 834-850 (lines=17) @@ | ||
| 831 | * @param callable $function ($value, $key) |
|
| 832 | * @return Collection |
|
| 833 | */ |
|
| 834 | function dropWhile($collection, callable $function) |
|
| 835 | { |
|
| 836 | $generatorFactory = function () use ($collection, $function) { |
|
| 837 | $shouldDrop = true; |
|
| 838 | foreach ($collection as $key => $value) { |
|
| 839 | if ($shouldDrop) { |
|
| 840 | $shouldDrop = $function($value, $key); |
|
| 841 | } |
|
| 842 | ||
| 843 | if (!$shouldDrop) { |
|
| 844 | yield $key => $value; |
|
| 845 | } |
|
| 846 | } |
|
| 847 | }; |
|
| 848 | ||
| 849 | return new Collection($generatorFactory); |
|
| 850 | } |
|
| 851 | ||
| 852 | /** |
|
| 853 | * Returns a lazy collection of items from $collection until first item for which $function returns false. |
|
| @@ 859-875 (lines=17) @@ | ||
| 856 | * @param callable $function ($value, $key) |
|
| 857 | * @return Collection |
|
| 858 | */ |
|
| 859 | function takeWhile($collection, callable $function) |
|
| 860 | { |
|
| 861 | $generatorFactory = function () use ($collection, $function) { |
|
| 862 | $shouldTake = true; |
|
| 863 | foreach ($collection as $key => $value) { |
|
| 864 | if ($shouldTake) { |
|
| 865 | $shouldTake = $function($value, $key); |
|
| 866 | } |
|
| 867 | ||
| 868 | if ($shouldTake) { |
|
| 869 | yield $key => $value; |
|
| 870 | } |
|
| 871 | } |
|
| 872 | }; |
|
| 873 | ||
| 874 | return new Collection($generatorFactory); |
|
| 875 | } |
|
| 876 | ||
| 877 | /** |
|
| 878 | * Returns a lazy collection. A result of calling map and flatten(1) |
|
| @@ 951-964 (lines=14) @@ | ||
| 948 | * @param mixed $startValue |
|
| 949 | * @return Collection |
|
| 950 | */ |
|
| 951 | function reductions($collection, callable $function, $startValue) |
|
| 952 | { |
|
| 953 | $generatorFactory = function () use ($collection, $function, $startValue) { |
|
| 954 | $tmp = duplicate($startValue); |
|
| 955 | ||
| 956 | yield $tmp; |
|
| 957 | foreach ($collection as $key => $value) { |
|
| 958 | $tmp = $function($tmp, $value, $key); |
|
| 959 | yield $tmp; |
|
| 960 | } |
|
| 961 | }; |
|
| 962 | ||
| 963 | return new Collection($generatorFactory); |
|
| 964 | } |
|
| 965 | ||
| 966 | /** |
|
| 967 | * Returns a lazy collection of every nth ($step) item in $collection. |
|