Skip to content

Instantly share code, notes, and snippets.

@dobkeratops
Last active June 23, 2017 01:48
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 dobkeratops/6aa63bf402968d31521fbbd69dbd505c to your computer and use it in GitHub Desktop.
Save dobkeratops/6aa63bf402968d31521fbbd69dbd505c to your computer and use it in GitHub Desktop.
-- Vec2, Vec3, Vec4 for x/y, x/y/z, x/y/z/w
instance Functor Vec3 where
fmap f (Vec3 x y z) = Vec3 (f x) (f y) (f z)
-- Docs tell me...
--class (Functor f)=> Applicative f where
-- pure :: a -> f a
-- (<*>) :: f (a -> b) -> f a -> f b
instance Applicative Vec3 where
pure a = Vec3 a a a -- ??? 'lifts the function' into fields of Vec3 ??
(Vec3 f0 f1 f2) <*> (Vec3 a0 a1 a2) = Vec3 (f0 a0)(f1 a1)(f2 a2)
class VectorOps x where
-- vbinop::(a->b->c) -> x a -> x b -> x c -- ??? liftA2 , .. but what to instacne to get it?
vreduce::(a->a->a) -> x a -> a -- in the process of replacig this with 'foldl1'
-- vscalarop::(a->b->c) -> x a -> b -> x c -- replaced with 'fmap'
instance VectorOps Vec3 where
vbinop f (Vec3 x0 y0 z0) (Vec3 x1 y1 z1)= Vec3 (f x0 x1)(f y0 y1)(f z0 z1)
vscalarop:: Functor v=>(a->b->c) -> v a -> b -> v c
vscalarop f v0 scalar= fmap (\x->f x scalar) v0
-
vbinop::Applicative v=>(a->b->c)->v a->v b->v c
vbinop = liftA2
-- ???
vbinop::Applicative v=>(a->b->c)->v a->v b->v c
vbinop = liftA2
--
vmul v0 v1 =vbinop (*) v0 v1
vadd v0 v1 =vbinop (+) v0 v1
vsub v0 v1 =vbinop (-) v0 v1
vdiv v0 v1 =vbinop (/) v0 v1
vdot va vb = vreduce (+) (vmul va vb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment