>>> 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],[]]
>>> wordsBy (`elem` ",;.?! ") "Hello there, world! How?" ["Hello","there","world","How"]
>>> wordsBy (=='x') "dogxxxcatxbirdxx" ["dog","cat","bird"]
>>> linesBy (==';') "foo;bar;;baz;" ["foo","bar","","baz"]
>>> linesBy (=='x') "dogxxxcatxbirdxx" ["dog","","","cat","bird",""]
wordsBy (== ':') "::xyz:abc::123::" == ["xyz","abc","123"] \s -> wordsBy isSpace s == words s
linesBy (== ':') "::xyz:abc::123::" == ["","","xyz","abc","","123",""] \s -> linesBy (== '\n') s == lines s linesBy (== ';') "my;list;here;" == ["my","list","here"]
split (== 'a') "aabbaca" == ["","","bb","c",""] split (== 'a') "" == [""] split (== ':') "::xyz:abc::123::" == ["","","xyz","abc","","123","",""] split (== ',') "my,list,here" == ["my","list","here"]
>>> 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",""]
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 pThis test captures both infinitely many groups and infinitely big groups:
forAllPredicates $ \p x -> flip seq True . (!!100) . concat . segmentAfter p . cycle . (x:)
>>> 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:)
intercalate [x] (chopWhen (== x) xs) == xs
>>> wordsBy (== ':') "bd:3" ["bd", "3"]
groupOn abs [1,-1,2] == [[1,-1], [2]]
> 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)
>>> splitWhen (<0) (BV.fromList [1,3,-4,5,7,-9,0,2]) [[1,3],[5,7],[0,2]]