Skip to content

Instantly share code, notes, and snippets.

@v0d1ch
Forked from dminuoso/lenses.hs
Created February 22, 2019 11:36
Show Gist options
  • Save v0d1ch/33e9410de560b01158becfaf62ed74e4 to your computer and use it in GitHub Desktop.
Save v0d1ch/33e9410de560b01158becfaf62ed74e4 to your computer and use it in GitHub Desktop.
traverseX :: forall f. Functor f = (a -> f a) -> Vec3 a -> f (Vec3 a)
traverseX f (Vec3 x y z) = (\h -> Vec3 h) <$> f x
traverseY :: forall f. Functor f = (a -> f a) -> Vec3 a -> f (Vec3 a)
traverseY f (Vec3 x y z) = (\h -> Vec3 h) <$> f x
traverseZ :: forall f. Functor f = (a -> f a) -> Vec3 a -> f (Vec3 a)
traverseZ f (Vec3 x y z) = (\h -> Vec3 h) <$> f x
type Lens s a = forall f. Funtor f => (a -> f a) -> s -> f s
_X :: Lens a (Vec3 a)
_X = traverseX
_Y :: Lens a (Vec3 a)
_Y = traverseY
_Z :: Lens a (Vec3 a)
_Z = traverseZ
-- > over _X (+1) (Vec3 1 0 7)
-- this is just for visual prettification, its just "flipped application"
(&) = flip ($)
(%~) = over
-- We can use it like this
-- > (Vec3 1 0 7) & _X %~ (+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment