Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created March 21, 2017 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/2251472e0ee490a38605c118373d156a to your computer and use it in GitHub Desktop.
Save rightfold/2251472e0ee490a38605c118373d156a to your computer and use it in GitHub Desktop.
readRational :: String -> Maybe Rational
readRational = (<|>) <$> withPoint <*> withSlash
where
withPoint =
String.split (String.Pattern ".")
>>> traverse Int.fromString
>=> case _ of
[a] -> Just $ a % 1
[a, b] -> Just $ a % 1 + b % 10
_ -> Nothing
withSlash =
String.split (String.Pattern "/")
>>> traverse Int.fromString
>=> case _ of
[a] -> Just $ a % 1
[a, b] -> Just $ a % b
_ -> Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment