How to use Read for Data.Version

Viktor Dukhovni ietf-dane at dukhovni.org
Sat Aug 14 17:13:53 UTC 2021


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.


More information about the Libraries mailing list