Skip to content

Instantly share code, notes, and snippets.

@tim2CF
Last active May 6, 2020 13:01
Show Gist options
  • Save tim2CF/9aa20cbfc54a05e295b7d83f7e13e2b5 to your computer and use it in GitHub Desktop.
Save tim2CF/9aa20cbfc54a05e295b7d83f7e13e2b5 to your computer and use it in GitHub Desktop.
safeFromIntegral ::
forall a b.
( Integral a,
Integral b,
Bounded b
) =>
a ->
Maybe b
safeFromIntegral x =
if (intX >= intMin) && (intX <= intMax)
then Just $ fromIntegral x
else Nothing
where
intX = fromIntegral x :: Integer
intMin = fromIntegral (minBound :: b) :: Integer
intMax = fromIntegral (maxBound :: b) :: Integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment