Skip to content

Instantly share code, notes, and snippets.

@fritz0705
Last active August 29, 2015 14:23
Show Gist options
  • Save fritz0705/9cb6d6ed21e06ee4890c to your computer and use it in GitHub Desktop.
Save fritz0705/9cb6d6ed21e06ee4890c to your computer and use it in GitHub Desktop.
newtype Vector2 a = Vector2 (a, a)
instance Eq a => Eq (Vector2 a) where
Vector2 x == Vector2 y = x == y
instance Functor Vector2 where
fmap f (Vector2 (a, b)) = Vector2 (f a, f b)
instance Applicative Vector2 where
pure x = Vector2 (x, x)
Vector2 (f, g) <*> Vector2 (x, y) = Vector2 (f x, g y)
instance Foldable Vector2 where
foldr f z (Vector2 (x, y)) = f y . f a $ z
instance (Eq a, Ord a) => Ord (Vector2 a) where
compare x y = fold $ compare <$> x <*> y
instance Num a => Num (Vector2 a) where
Vector2 (a, b) + Vector2 (c, d) = Vector2 (a + c, b + d)
abs = (abs <$>)
signum = (signum <$>)
fromInteger = undefined -- Is there a sensible instance?
(*) = undefined -- There is no sensible instance for a commutative-associative multiplication *: V x V -> V
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment