Skip to content

Instantly share code, notes, and snippets.

@int-index
Created April 30, 2015 00:42
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 int-index/c09c7a641d50922dbfeb to your computer and use it in GitHub Desktop.
Save int-index/c09c7a641d50922dbfeb to your computer and use it in GitHub Desktop.
instance Num Dynamic
module Dynamic where
import Control.Applicative
import Data.Dynamic
please = maybe (error "dynamic typing is bad :(") toDyn
pleaseApp2 :: (Rational -> Rational -> Rational)
-> (Dynamic -> Dynamic -> Dynamic)
pleaseApp2 f a b = please $ f <$> fromDynamic a <*> fromDynamic b
instance Num Dynamic where
fromInteger = toDyn . toRational
(+) = pleaseApp2 (+)
(*) = pleaseApp2 (*)
(-) = pleaseApp2 (-)
abs = please . fmap abs . fromDynamic
signum = please . fmap signum . fromDynamic
instance Fractional Dynamic where
fromRational = toDyn
(/) = pleaseApp2 (/)
dynamicDouble :: Dynamic -> Double
dynamicDouble = maybe (error "dynamic typing, uh ._.") fromRational . fromDynamic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment