Skip to main content
Module

x/ramda/source/lens.js

:ram: Practical functional Javascript
Latest
File
import _curry2 from './internal/_curry2.js';import map from './map.js';

/** * Returns a lens for the given getter and setter functions. The getter "gets" * the value of the focus; the setter "sets" the value of the focus. The setter * should not mutate the data structure. * * @func * @memberOf R * @since v0.8.0 * @category Object * @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s * @sig (s -> a) -> ((a, s) -> s) -> Lens s a * @param {Function} getter * @param {Function} setter * @return {Lens} * @see R.view, R.set, R.over, R.lensIndex, R.lensProp * @example * * const xLens = R.lens(R.prop('x'), R.assoc('x')); * * R.view(xLens, {x: 1, y: 2}); //=> 1 * R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2} * R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2} */var lens = _curry2(function lens(getter, setter) { return function(toFunctorFn) { return function(target) { return map( function(focus) { return setter(focus, target); }, toFunctorFn(getter(target)) ); }; };});export default lens;