[Ramda] Change Object Properties with Ramda Lenses
In this lesson we'll learn the basics of using lenses in Ramda and see how they enable you to focus changes on specific properties of an object while keeping your data immutable.
what 'R.lens' do is able to get or set prop value but keep the object immutable.
Normally you have 2 ways to use lens:
1. set up lens: 'lens' + 'prop' + 'assoc';
After set up you can either get value by using 'R.view';
Or change a prop by using 'R.set' or 'R.over'.
2. Using 'lens' + 'prop' + 'assoc' is a little bit work to do, there is short syntax to do the same thing: 'R.lensProp'.
const R = require('ramda'); const {view, lens, assoc, prop, set, over, lensProp, toUpper} = R; const person = {
firstName: 'Fred',
lastName: 'Filntstore'
}; /*
* What lens does is modify prop value for a given object
* and keep data immutable.
* */ const firstNameLens = lens(
prop('firstName'),
assoc('firstName')
);
const viewFirstName = view(firstNameLens);
// const result = viewFirstName(person); // Fred /** Using R.set **/
const setFirstName = set(firstNameLens);
const updateFirstName = setFirstName('Zhentian');
// const result = updateFirstName(person); // { firstName: 'Zhentian', lastName: 'Filntstore' } /** Using R.over instead of set **/
const result = over(firstNameLens, toUpper, person); /** lensProp instead of prop + assoc **/
const lastNameLens = lensProp('lastName');
const result2 = over(lastNameLens, toUpper, person); // { firstName: 'Fred', lastName: 'FILNTSTORE' }
console.log(result);
console.log(result2);
[Ramda] Change Object Properties with Ramda Lenses的更多相关文章
- [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where
Sometimes you need to filter an array of objects or perform other conditional logic based on a combi ...
- [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve
We don't always control the data we need in our applications, and that means we often find ourselves ...
- An unexpected exception occurred while creating a change object. see the error log for more details
今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷): An unexpected exception occurred while creating a c ...
- Android An unexpected exception occurred while creating a change object. see the error log for more details
今天再给Android项目工程中的包重命名时出现了这个错误(之前重命名的时候就没有出现,郁闷):An unexpected exception occurred while creating a ch ...
- change object keys & UpperCase & LowerCase
change object keys & UpperCase & LowerCase .toLocaleUpperCase(); && .toLocaleLowerCa ...
- [Ramda] Convert Object Methods into Composable Functions with Ramda
In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods ...
- [Javascript Crocks] Safely Access Object Properties with `prop`
In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefin ...
- [Ramda] Handle Branching Logic with Ramda's Conditional Functions
When you want to build your logic with small, composable functions you need a functional way to hand ...
- [Ramda] Sort, SortBy, SortWith in Ramda
The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: ta ...
随机推荐
- LayUI-Table表格渲染
记项目中又一表格使用方法,项目首选是使用BootstrapTable的,但是经过多番查证与调试,始终没有把固定列的功能调试成功,找到的成功的例子原样照搬都不行,文件引入也都没有问题,实在搞不懂了,如果 ...
- 当鼠标聚焦时输入框变色(focus事件实例)
当鼠标聚焦时输入框变色css相关,鼠标点击<input>输入域后出现有颜色的边框原理:css伪类之input输入框鼠标点击边框变色效果伪类元素的使用::focus 一:当输入框获得焦点时, ...
- java hadoop file system API
org.apache.hadoop.fs Class FileSystem java.lang.Object org.apache.hadoop.fs.FileSystem All Implement ...
- Android之——图片的内存优化
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46972817 1. 对图片本身进行操作 尽量不要使用 setImageBitmap ...
- 【Educational Codeforces Round 31 A】Book Reading
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...
- java中Arrays类的应用
java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的.具有以下功能: ² 给数组赋值:通过fill方法. ² 对数组排序:通过sort方法,按升序. ² 比较数组:通过equ ...
- js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言
js进阶 12-16 jquery如何实现通过点击按钮和按下组合键两种方式提交留言 一.总结 一句话总结:实现按下组合键提交留言是通过给input加keydown事件,判断按键的键码来实现的. 1.如 ...
- Intel X86 CPU 系列的寻址方式
Intel X86 CPU 系列的寻址方式 数据总线和地址总线要尽量相同,这个是一个地址就是一个指针.
- SaltStack快速部署及测试
测试环境:CentOS6.6 X86_64 # cat /etc/hosts 192.168.199.61 Ansible 192.168.199.60 Nginx1 192.168.199.62 N ...
- 被误解的MVC和被神化的MVVM
MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...