[Haskell-cafe] Is there already a lib to parse a line of string with a pattern and generate key-value map or a record?

Olaf Klinke olf at aatal-apotheke.de
Wed Apr 17 20:01:28 UTC 2019


You could emulate this with some custom parser combinators on top of any 
monadic parser combinator library such as attoparsec or megaparsec.

keyed :: (Ord key, Functor parser) =>
   key -> parser a -> parser (Map key a)
keyed key = fmap (singleton key)

Then, assuming 'char' and 'digitChar' exists in the parser library,
you may write

integer = some digitChar
logline = fmap mconcat $ sequence [
   keyed "year" (integer <* char '/'),
   keyed "month" (integer <* char '/'),
   keyed "day" (integer <* char ' '),
   -- etc.
   ]

Olaf


More information about the Haskell-Cafe mailing list