Skip to content

Instantly share code, notes, and snippets.

@jamiecook
Created June 23, 2017 09:03
Show Gist options
  • Save jamiecook/4d5f33fe1e26e37de97d80a06c071308 to your computer and use it in GitHub Desktop.
Save jamiecook/4d5f33fe1e26e37de97d80a06c071308 to your computer and use it in GitHub Desktop.
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