| @@ 468-477 (lines=10) @@ | ||
| 465 | * @param array $list |
|
| 466 | * @return array |
|
| 467 | */ |
|
| 468 | function takeWhile() { |
|
| 469 | $takeWhile = function($predicate, $list) { |
|
| 470 | $first = head($list); |
|
| 471 | return (null == $first || !$predicate($first)) |
|
| 472 | ? [] |
|
| 473 | : prepend($first, takeWhile($predicate, $list)); |
|
| 474 | ||
| 475 | }; |
|
| 476 | return apply(curry($takeWhile), func_get_args()); |
|
| 477 | } |
|
| 478 | ||
| 479 | /** |
|
| 480 | * Same as `takeWhile` but taking elements from the end of the array. |
|
| @@ 492-501 (lines=10) @@ | ||
| 489 | * @param array $list |
|
| 490 | * @return array |
|
| 491 | */ |
|
| 492 | function takeLastWhile() { |
|
| 493 | $takeWhile = function($predicate, $list) { |
|
| 494 | $last = last($list); |
|
| 495 | return (null == $last || !$predicate($last)) |
|
| 496 | ? [] |
|
| 497 | : append($last, takeLastWhile($predicate, $list)); |
|
| 498 | ||
| 499 | }; |
|
| 500 | return apply(curry($takeLastWhile), func_get_args()); |
|
| 501 | } |
|
| 502 | ||
| 503 | /** |
|
| 504 | * Takes elements from an array **until** the predicate |
|
| @@ 841-852 (lines=12) @@ | ||
| 838 | * @param array $list |
|
| 839 | * @return int |
|
| 840 | */ |
|
| 841 | function indexOf() { |
|
| 842 | $indexOf = function($item, $list) { |
|
| 843 | if (is_string($list)) { |
|
| 844 | $index = strpos($list, $item); |
|
| 845 | return (-1 == $index) |
|
| 846 | ? null |
|
| 847 | : $index; |
|
| 848 | } |
|
| 849 | return findIndex(equals($item), $list); |
|
| 850 | }; |
|
| 851 | return apply(curry($indexOf), func_get_args()); |
|
| 852 | } |
|
| 853 | ||
| 854 | /** |
|
| 855 | * Same as `indexOf` but returns the key/position/name of the last item/substring/attribute. |
|
| @@ 870-881 (lines=12) @@ | ||
| 867 | * @param array $list |
|
| 868 | * @return int |
|
| 869 | */ |
|
| 870 | function lastIndexOf() { |
|
| 871 | $lastIndexOf = function($item, $list) { |
|
| 872 | if (is_string($list)) { |
|
| 873 | $index = strrpos($list, $item); |
|
| 874 | return (-1 == $index) |
|
| 875 | ? null |
|
| 876 | : $index; |
|
| 877 | } |
|
| 878 | return findLastIndex(equals($item), $list); |
|
| 879 | }; |
|
| 880 | return apply(curry($lastIndexOf), func_get_args()); |
|
| 881 | } |
|
| 882 | ||
| 883 | /** |
|
| 884 | * Removes duplicates from a list by keeping the first occurence of each group |
|