Re: How to use Read for Data.Version

Viktor Dukhovni <[email protected]> Sat, 14 Aug 2021 13:13:53 -0400
Newsgroups gmane.comp.lang.haskell.libraries
Message-ID <[email protected]>
On Sat, Aug 14, 2021 at 06:53:08PM +0200, Andreas Abel wrote:

> readVersion :: String -> Version
> readVersion = read
> 
> main :: IO ()
> main = print $ readVersion "8.10.5"

Perhaps this is close to what you want:

    λ> import Data.Version
    λ> import Text.ParserCombinators.ReadP

    λ> foldr (const . Just) Nothing $ readP_to_S (parseVersion <* eof) "8.10.5"
    Just (Version {versionBranch = [8,10,5], versionTags = []},"")

    λ> foldr (const . Just) Nothing $ readP_to_S (parseVersion <* eof) "8.10.X"
    Nothing

    λ> foldr (const . Just) Nothing $ readP_to_S (parseVersion <* eof) "8.10.5-foo-bar"
    Just (Version {versionBranch = [8,10,5], versionTags = ["foo","bar"]},"")

[ foldr (const . Just) Nothing == listToMaybe) ]

-- 
    Viktor.
_______________________________________________
Libraries mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries