
Albert Y. C. Lai wrote:
Mitar wrote:
I am really missing the (general) split function built in standard Haskell. I do not understand why there is something so specific as words and lines but not a simple split? The same goes for join.
Don't forget Text.Regex.splitRegex.
Which is just:
matchRegexAll p str = matchM p str
{- | Splits a string based on a regular expression. The regular expression should identify one delimiter.
This is unsafe if the regex matches an empty string. -}
splitRegex :: Regex -> String -> [String] splitRegex _ [] = [] splitRegex delim str = case matchRegexAll delim str of Nothing -> [str] Just (firstline, _, remainder, _) -> if remainder == "" then firstline : [] : [] else firstline : splitRegex delim remainder
Inlining the matchRegexAll/matchM means this is 8 lines of code. Any given split function is very short, but there are enough design choices that I think the best library is none at all; the user can write exactly what they want in <= 10 lines of code. Though now that I look at it again, I think I like
splitRegex :: Regex -> String -> [String] splitRegex _ [] = [] splitRegex delim strIn = loop strIn where loop str = case matchM delim str of Nothing -> [str] Just (firstline, _, remainder) -> if null remainder then [firstline,""] else firstline : loop remainder
slightly better. I'll eventually update the unstable regex-compat. -- Chris