@@ 802-818 (lines=17) @@ | ||
799 | * @param callable $function ($value, $key) |
|
800 | * @return Collection |
|
801 | */ |
|
802 | function dropWhile($collection, callable $function) |
|
803 | { |
|
804 | $generatorFactory = function () use ($collection, $function) { |
|
805 | $shouldDrop = true; |
|
806 | foreach ($collection as $key => $value) { |
|
807 | if ($shouldDrop) { |
|
808 | $shouldDrop = $function($value, $key); |
|
809 | } |
|
810 | ||
811 | if (!$shouldDrop) { |
|
812 | yield $key => $value; |
|
813 | } |
|
814 | } |
|
815 | }; |
|
816 | ||
817 | return new Collection($generatorFactory); |
|
818 | } |
|
819 | ||
820 | /** |
|
821 | * Returns a lazy collection of items from $collection until first item for which $function returns false. |
|
@@ 827-843 (lines=17) @@ | ||
824 | * @param callable $function ($value, $key) |
|
825 | * @return Collection |
|
826 | */ |
|
827 | function takeWhile($collection, callable $function) |
|
828 | { |
|
829 | $generatorFactory = function () use ($collection, $function) { |
|
830 | $shouldTake = true; |
|
831 | foreach ($collection as $key => $value) { |
|
832 | if ($shouldTake) { |
|
833 | $shouldTake = $function($value, $key); |
|
834 | } |
|
835 | ||
836 | if ($shouldTake) { |
|
837 | yield $key => $value; |
|
838 | } |
|
839 | } |
|
840 | }; |
|
841 | ||
842 | return new Collection($generatorFactory); |
|
843 | } |
|
844 | ||
845 | /** |
|
846 | * Returns a lazy collection. A result of calling map and flatten(1) |
|
@@ 919-932 (lines=14) @@ | ||
916 | * @param mixed $startValue |
|
917 | * @return Collection |
|
918 | */ |
|
919 | function reductions($collection, callable $function, $startValue) |
|
920 | { |
|
921 | $generatorFactory = function () use ($collection, $function, $startValue) { |
|
922 | $tmp = duplicate($startValue); |
|
923 | ||
924 | yield $tmp; |
|
925 | foreach ($collection as $key => $value) { |
|
926 | $tmp = $function($tmp, $value, $key); |
|
927 | yield $tmp; |
|
928 | } |
|
929 | }; |
|
930 | ||
931 | return new Collection($generatorFactory); |
|
932 | } |
|
933 | ||
934 | /** |
|
935 | * Returns a lazy collection of every nth ($step) item in $collection. |