[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 ...
随机推荐
- Android学习笔记进阶十之Matrix错切变换
刚开始我也不懂啥叫错切变换,一看效果图你就恍然大悟. 对图像的错切变换做个总结: x = x0 + b*y0; y = d*x0 + y0; 与之对应的方法是: Matrix matrix = new ...
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...
- [Angular] Use Angular style sanitization to mark dynamic styles as trusted values
Angular has a very robust security model. Dynamically inserted html, style or url values into the DO ...
- python 的spyder用法
ctrl+tab可以进行跳转 https://blog.csdn.net/luckygirl0809/article/details/79929491
- Flask项目之手机端租房网站的实战开发(一)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶项目介绍 产品:关于手机移动端的租房网站 角色:在这个产品中用户包括房东与房客 功能:房东可以在这个平台发布自己的房屋,房客可 ...
- 小米R2D samba共享配置
编辑samba配置文件 vi /etc/config/samba 需要注意的是,samba有自己的配置文件 /etc/samba/smb.conf,但是修改这个文件是不生效的,这个配置文件会在重启路由 ...
- spark源码解析之scala基本语法
1. scala初识 spark由scala编写,要解析scala,首先要对scala有基本的了解. 1.1 class vs object A class is a blueprint for ob ...
- php 面试题一(看视频的学习量比网上瞎转悠要清晰和明了很多)(看视频做好笔记)(注重复习)
php 面试题一(看视频的学习量比网上瞎转悠要清晰和明了很多)(看视频做好笔记)(注重复习) 一.总结 1.无线分类的本质是树(数据结构)(数的话有多种储存结构可以实现,所以对应的算法也有很多),想到 ...
- 三、链路追踪系统 zipkin
一.构建项目 用到的依赖直接看pom.xml的注释吧 <?xml version="1.0" encoding="UTF-8"?> <proj ...
- VS2010制作dll
一.为什么需要dll 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,如ATL.M ...