Last active
August 24, 2019 15:25
-
-
Save snipsnipsnip/128742 to your computer and use it in GitHub Desktop.
instance Num Vector2の実装を簡単に
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
import Control.Applicative | |
data Vector2 a = Vector2 a a deriving (Show, Eq) | |
instance Applicative Vector2 where | |
pure x = Vector2 x x | |
Vector2 fx fy <*> Vector2 x y = Vector2 (fx x) (fy y) | |
instance Functor Vector2 where | |
fmap = liftA | |
instance Num a => Num (Vector2 a) where | |
(+) = liftA2 (+) | |
(-) = liftA2 (-) | |
(*) = liftA2 (*) | |
negate = fmap negate | |
abs = fmap abs | |
signum = fmap signum | |
fromInteger = pure . fromInteger | |
instance (Fractional a) => Fractional (Vector2 a) where | |
(/) = liftA2 (/) | |
recip = fmap recip | |
fromRational = pure . fromRational |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment