Skip to content

Instantly share code, notes, and snippets.

@CrossEye
Forked from andyhd/lens.js
Created June 5, 2014 15:36
Show Gist options
  • 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]
@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