Skip to content

Commit

Permalink
add lensIso
Browse files Browse the repository at this point in the history
  • Loading branch information
Harris-Miller committed May 2, 2024
1 parent ad50734 commit 7ef7be5
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions source/lensIso.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import _curry2 from './internal/_curry2.js';
import map from './map.js';


/**
* Returns a lens for the given isomorphic function pairs to/from.
* Where we go source `to` focus, then backwards source `from` focus.
* The function pairs should never loose data between the structure mappings
*
* @func
* @memberOf R
* @since v0.31.0
* @category Object
* @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
* @sig (s -> a) -> (a -> s) -> Lens s a
* @param {Function} to
* @param {Function} from
* @return {Lens}
* @see R.view, R.set, R.over, R.lensIndex, R.lensProp
* @example
*
* const xJson = lensIso(JSON.parse, JSON.stringify);
*
* R.view(xJson, '{"x": 4, "y": 2}'); //=> {x: 4, y: 2}
* R.set(xJson, {x: 4, y: 10 }, '{"x": 4, "y": 2}'); //=> '{"x": 4, "y": 10}'
* R.over(xJson, R.assoc('x', 6), '{"x": 4,"y": 2}'); //=> '{"x": 6 ,"y": 2}'
*/
var lensIso = _curry2(function lensIso(to, from) {
return function(toFunctorFn) {
return function(target) {
return map(
function(focus) {
return from(focus);
},
toFunctorFn(to(target))
);
};
};
});
export default lensIso;

0 comments on commit 7ef7be5

Please sign in to comment.