Tokenize a string with escaping: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Added a functional draft (using reduce). Compatible with Python 3.) |
(→Haskell Alternative: foldl -> foldr) |
||
Line 1,095: | Line 1,095: | ||
===Alternative=== |
===Alternative=== |
||
This is essentially equivalent to the first (DFA) example, but, though possibly less elegant than the guard idiom, appears to be fractionally faster with larger (eg 180k) test strings. |
This is essentially equivalent to the first (DFA) example, but, though possibly less elegant than the guard idiom, appears to be fractionally faster with larger (eg 180k) test strings. |
||
<lang haskell> |
<lang haskell>import Data.Bool (bool) |
||
:: (Eq a, Foldable t) |
|||
tokenize :: Char -> Char -> String -> [String] |
|||
tokenize delim esc str = reverse $ reverse <$> (token : list) |
tokenize delim esc str = reverse $ reverse <$> (token : list) |
||
where |
where |
||
(token, list, _) = |
(token, list, _) = |
||
foldr |
|||
(\(aToken, aList, aEsc) |
(\x (aToken, aList, aEsc) -> |
||
let literal = not aEsc |
let literal = not aEsc |
||
isEsc = literal && (x == esc) |
isEsc = literal && (x == esc) |
||
in |
in bool |
||
(bool (x : aToken) aToken isEsc, aList, isEsc) |
|||
([], aToken : aList, isEsc) |
|||
(literal && x == delim)) |
|||
else x : aToken |
|||
, aList |
|||
, isEsc)) |
|||
([], [], False) |
([], [], False) |
||
str |
(reverse str) |
||
main :: IO () |
main :: IO () |