Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
instance Num Vector2の実装を簡単に
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