Commit 748d5d50 authored by Iustin Pop's avatar Iustin Pop
Browse files

Generalise the sepSplit function



Currently it works on splitting strings by individual chars, but we
can generalise it to split lists by list elements, which means we can
reuse it later in the Text module for splitting both lists of chars by
'|' or lists of lines by empty newlines. The change also makes the
code cleaner (uses “null xs” instead of string-specific “xs == ""”).

Note: I tried to rewrite this in a nicer, functional style using
unfolds, but I failed to account for the final terminator case
(e.g. ab|cd|) resulting in a valid but empty element.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarBalazs Lecz <leczb@google.com>
parent aec636b9
......@@ -73,13 +73,13 @@ debugXy a b = debug a `seq` b
commaJoin :: [String] -> String
commaJoin = intercalate ","
-- | Split a string on a separator and return an array.
sepSplit :: Char -> String -> [String]
-- | Split a list on a separator and return an array.
sepSplit :: Eq a => a -> [a] -> [[a]]
sepSplit sep s
| x == "" && xs == [] = []
| xs == [] = [x]
| ys == [] = [x,""]
| otherwise = x:sepSplit sep ys
| null s = []
| null xs = [x]
| null ys = [x,[]]
| otherwise = x:sepSplit sep ys
where (x, xs) = break (== sep) s
ys = drop 1 xs
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment