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