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

Jinxuan Zhu zhujinxuan at gmail.com
Fri Apr 19 14:18:00 UTC 2019


I feel like the type with Map is not very nice (as map can be 
anything).
Given the simplicity of year parsing , how about traditional

```
data DateTime = DT Int Int Int
timeP :: Parser DateTime
timeP = DT <*> p <$> p <$> p where
  p = integer <* char '/'
```



Olaf Klinke <olf at aatal-apotheke.de> writes:

> 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
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to 
> post.



More information about the Haskell-Cafe mailing list