Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Last active May 3, 2021 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidchambers/45aa0187a32fbac6912d4b3b4e8530c5 to your computer and use it in GitHub Desktop.
Save davidchambers/45aa0187a32fbac6912d4b3b4e8530c5 to your computer and use it in GitHub Desktop.
import S from 'sanctuary';
import Identity from 'sanctuary-identity';
// lens :: (s -> a) -> (a -> s -> s) -> Lens s a
const lens = getter => setter => f => s => (
S.map (v => setter (v) (s))
(f (getter (s)))
);
// view :: Lens s a -> s -> a
const view = l => x => (l (S.Left) (x)).value;
// over :: Lens s a -> (a -> a) -> s -> s
const over = l => f => x => S.extract (l (y => Identity (f (y))) (x));
// email :: Lens User String
const email = lens (user => user.email) (email => user => ({...user, email}));
// user :: User
const user = {id: 1, email: 'dc@davidchambers.me'};
console.log (view (email) (user));
console.log (over (email) (S.toUpper) (user));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment