:: (a -> Bool) -> [a] -> [[a]] -package:base-prelude

Split on elements satisfying the given predicate. Equivalent to split . dropDelims . whenElt.
>>> splitWhen (<0) [1,3,-4,5,7,-9,0,2]
[[1,3],[5,7],[0,2]]
>>> splitWhen (<0) [1,-2,3,4,-5,-6,7,8,-9]
[[1],[3,4],[],[7,8],[]]
Split into "words", with word boundaries indicated by the given predicate. Satisfies words === wordsBy isSpace; equivalent to split . dropBlanks . dropDelims . whenElt.
>>> wordsBy (`elem` ",;.?! ") "Hello there, world! How?"
["Hello","there","world","How"]
>>> wordsBy (=='x') "dogxxxcatxbirdxx"
["dog","cat","bird"]
Split into "lines", with line boundaries indicated by the given predicate. Satisfies lines === linesBy (=='n'); equivalent to split . dropFinalBlank . dropDelims . whenElt.
>>> linesBy (==';') "foo;bar;;baz;"
["foo","bar","","baz"]
>>> linesBy (=='x') "dogxxxcatxbirdxx"
["dog","","","cat","bird",""]
A variant of words with a custom test. In particular, adjacent separators are discarded, as are leading or trailing separators.
wordsBy (== ':') "::xyz:abc::123::" == ["xyz","abc","123"]
\s -> wordsBy isSpace s == words s
A variant of lines with a custom test. In particular, if there is a trailing separator it will be discarded.
linesBy (== ':') "::xyz:abc::123::" == ["","","xyz","abc","","123",""]
\s -> linesBy (== '\n') s == lines s
linesBy (== ';') "my;list;here;" == ["my","list","here"]
Splits a list into components delimited by separators, where the predicate returns True for a separator element. The resulting components do not contain the separators. Two adjacent separators result in an empty component in the output.
split (== 'a') "aabbaca" == ["","","bb","c",""]
split (== 'a') ""        == [""]
split (== ':') "::xyz:abc::123::" == ["","","xyz","abc","","123","",""]
split (== ',') "my,list,here" == ["my","list","here"]
Split the list at the occurrences of a separator into sub-lists. Remove the separators. This is somehow a generalization of lines and words. But note the differences:
>>> words "a  a"
["a","a"]

>>> chop (' '==) "a  a"
["a","","a"]
>>> lines "a\n\na"
["a","","a"]

>>> chop ('\n'==) "a\n\na"
["a","","a"]
>>> lines "a\n"
["a"]

>>> chop ('\n'==) "a\n"
["a",""]
Split the list after each occurence of a terminator. Keep the terminator. There is always a list for the part after the last terminator. It may be empty. See package non-empty for more precise result type.
forAllPredicates $ \p xs -> concat (segmentAfter p xs) == xs
forAllPredicates $ \p xs -> length (filter p xs) == length (tail (segmentAfter p xs))
forAllPredicates $ \p -> all (p . last) . init . segmentAfter p
forAllPredicates $ \p -> all (all (not . p) . init) . init . segmentAfter p
This test captures both infinitely many groups and infinitely big groups:
forAllPredicates $ \p x -> flip seq True . (!!100) . concat . segmentAfter p . cycle . (x:)
Split the list before each occurence of a leading character. Keep these characters. There is always a list for the part before the first leading character. It may be empty. See package non-empty for more precise result type.
>>> segmentBefore isUpper "AbcdXyz"
["","Abcd","Xyz"]

>>> segmentBefore isUpper "kAbcdXYZ"
["k","Abcd","X","Y","Z"]
forAllPredicates $ \p xs -> concat (segmentBefore p xs) == xs
forAllPredicates $ \p xs -> length (filter p xs) == length (tail (segmentBefore p xs))
forAllPredicates $ \p -> all (p . head) . tail . segmentBefore p
forAllPredicates $ \p -> all (all (not . p) . tail) . tail . segmentBefore p
forAllPredicates $ \p x -> flip seq True . (!!100) . concat . segmentBefore p . cycle . (x:)
This function takes a list, and returns all suffixes whose first item matches the predicate.
This function is similar to sections, but splits the list so no element appears in any two partitions.
Split list by groups of one or more sep.
Repeatedly splits a list and collects the results
Chop a list into segments, at separators identified by the predicate. The separator items are discarded.
Chop a list at the positions when the predicate holds. Contrary to wordsBy, consecutive separator elements will result in an empty segment in the result. O(n).
intercalate [x] (chopWhen (== x) xs) == xs
split given list of a by given single a, e.g.
>>> wordsBy (== ':') "bd:3"
["bd", "3"]
The groupWith function uses the user supplied function which projects an element out of every list element in order to first sort the input list and then to form groups by equality on these projected elements
A combination of group and sort, using a part of the value to compare on.
groupSortOn length ["test","of","sized","item"] == [["of"],["test","item"],["sized"]]
A version of group where the equality is done on some extracted value.
groupOn abs [1,-1,2] == [[1,-1], [2]]
Divides a list into sublists such that the members in a sublist share the same key. It uses semantics of groupBy, not that of groupBy.
Classify values based on the result of a given function.
> classifyOn head ["sheep", "chip", "ship", "cheap"]
[["sheep","ship"],["chip","cheap"]]
> classifyOn odd [1,2,3,4,5,6]
[[1,3,5],[2,4,6]]
(cf. classify, classifyBy)
Group values using a given field selector.