Created
June 23, 2017 09:03
-
-
Save jamiecook/4d5f33fe1e26e37de97d80a06c071308 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data PercentageBase100 = PercentageBase100 Double deriving (Eq,Show) | |
instance FromField PercentageBase100 where | |
parseField p = pure $ (f . read . BS8.unpack) p | |
where f = checkPercentageBounds (0.0, 100.0) PercentageBase100 | |
checkPercentageBounds :: (Double,Double) -> | |
(Double -> a) -> Scientific -> a | |
checkPercentageBounds (low, high) f s | |
| ((d < low) || (d > high)) = error $ "That's an invalid percentage, expected " ++ (show d) ++ " to be in the range " ++ (show (low,high)) | |
| otherwise = f $ d | |
where d = toRealFloat s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment