Skip to content

Instantly share code, notes, and snippets.

@halan
Created March 23, 2020 11:53
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 halan/a8cfe6bab856fda0a2d6a599301e138a to your computer and use it in GitHub Desktop.
Save halan/a8cfe6bab856fda0a2d6a599301e138a to your computer and use it in GitHub Desktop.
A pure lens implementation strongly based on ramda implementation, made to teaching my internships at Codeminer42.com
o = { user: { name: 'foo' } }
const get = key => obj => obj[key]
const set = key => (x, obj) =>
({ ...obj, [key]: x })
Identity = v => ({
value: v,
map: mapping => Identity(mapping(v))
})
const lens = (getter, setter) => toFunctor => data =>
toFunctor(getter(data))
.map(focus => setter(focus, data))
const overLens = setter =>
lens(x => x, focus => setter(focus))(Identity)
const nameLens = lens(get('name'), set('name'));
const userNameLens = f => lens(get('user'), set('user'))(nameLens(f))
const over = (l, mapping, obj) =>
l(overLens(mapping))(obj).value
console.log(
over(userNameLens, x => "fooofoo", o)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment