Created
March 23, 2020 11:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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