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