Created
April 30, 2015 00:42
-
-
Save int-index/c09c7a641d50922dbfeb to your computer and use it in GitHub Desktop.
instance Num Dynamic
This file contains 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
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