Skip to content

Instantly share code, notes, and snippets.

@CrossEye
Forked from andyhd/lens.js
Created June 5, 2014 15:36
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 CrossEye/fde9b9e7f13498d392e9 to your computer and use it in GitHub Desktop.
Save CrossEye/fde9b9e7f13498d392e9 to your computer and use it in GitHub Desktop.
function lens(get, set) {
var f = function (a) { return get(a); };
f.set = set;
f.mod = function (f, a) { return set(a, f(get(a))); };
return f;
}
var first = lens(
function (a) { return a[0]; },
function (a, b) { return [b].concat(a.slice(1)); }
);
console.log(first([1, 2, 3])); // outputs 1
console.log(first.set([1, 2, 3], 5)); // outputs [5, 2, 3]
function tenTimes(x) { return x * 10 }
console.log(first.mod(tenTimes, [1, 2, 3])); // outputs [10, 2, 3]
@CrossEye
Copy link
Author

CrossEye commented Jun 5, 2014

This is beautiful as is. Forked to eventually add more examples.

@buzzdecafe
Copy link

the only question is which should come first for currying, get or set? or does it matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment