[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
Fri Apr 19 15:14:34 UTC 2019


I'd rather go for LocalTime from the time package, if there must be a custom data type for time and date. 
But beware: If you parse the line into a Map, then you can not mix the types of the individual fields: all must be the same. Hence in my example I went for String. It is the duty of downstream functions to transform the Map String String into a more detailed data structure. 

Olaf

> Am 19.04.2019 um 16:18 schrieb Jinxuan Zhu <zhujinxuan at gmail.com>:
> 
> 
> 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