Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created November 11, 2015 07:12
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 dtchepak/d695a7ae8a7a1ff11162 to your computer and use it in GitHub Desktop.
Save dtchepak/d695a7ae8a7a1ff11162 to your computer and use it in GitHub Desktop.
-- Based on https://gist.github.com/evancz/78293dc6a4ac2547676c
type alias Lens s a =
{ get : s -> a
, set : a -> s -> s
}
modify : Lens s a -> (a -> a) -> s -> s
modify l f s =
l.set (f (l.get s)) s
assignTo : Lens s a -> s -> a -> s
assignTo l = flip l.set
(=>) : Lens s a -> Lens a b -> Lens s b
(=>) sa ab =
{ get s = ab.get (sa.get s)
, set b s = sa.set (ab.set b (sa.get s)) s
}
fst' : Lens (a,b) a
fst' =
{ get = fst
, set a (_,b) = (a,b)
}
snd' : Lens (a,b) b
snd' =
{ get = snd
, set b (a,_) = (a,b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment