RosettaCodeData/Task/Tokenize-a-string-with-escaping/Haskell/tokenize-a-string-with-escaping-2.hs

12 lines
374 B
Haskell
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
{-#Language LambdaCase #-}
import Conduit
splitEscC :: (Monad m, Eq t) => t -> t -> Conduit t m [t]
splitEscC sep esc = mapOutput reverse $ go True []
where
go notEsc b = await >>= \case
Nothing -> yield b
Just ch | notEsc && ch == esc -> go False b
| notEsc && ch == sep -> yield b >> go True []
| otherwise -> go True (ch:b)