Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active August 24, 2019 15:25
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 snipsnipsnip/128742 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/128742 to your computer and use it in GitHub Desktop.
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