[Ramda] Getter and Setter in Ramda & lens

Getter on Object:

1. prop:

R.prop('x', {x: 100}); //=> 100
R.prop('x', {}); //=> undefined

2. props: 

R.props(['x', 'y'], {x: 1, y: 2}); //=> [1, 2]
R.props(['c', 'a', 'b'], {b: 2, a: 1}); //=> [undefined, 1, 2]

 

Setter ob Object:

R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3}

 

Another way to use Lens:

var 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}

 

posted @ 2016-10-10 21:12  Zhentiw  阅读(413)  评论(0)    收藏  举报