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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. change object keys & UpperCase & LowerCase

    change object keys & UpperCase & LowerCase .toLocaleUpperCase(); && .toLocaleLowerCa ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [Ramda] Sort, SortBy, SortWith in Ramda

    The difference between sort, sortBy, sortWith is that: 1. sort: take function as args. 2. sortBy: ta ...

随机推荐

  1. C++ 学习路线推荐

        相信有非常大一部分学计算机的童鞋都是靠自学,即使本身是计算机专业的同学,也会认为只通过课堂上的学习是远远不够的,并且在上课时所用到的教材也不够好.然而自学的时候有个非常大的问题就是找不到合适的 ...

  2. 内网使用 IPV6 之 TunnelBroker隧道(6in4)篇

    内网使用 IPV6 之 TunnelBroker隧道(6in4)篇 据非专业网民推测 tunnelbroker isatap 和 6to4  貌似都需要公网,但有网民测试这位大作的方法可行.特别之处是 ...

  3. Rdis-主从复制

    配置 配置一个从服务器非常简单, 只要在配置文件中增加以下的这一行就可以了: slaveof 192.168.1.1 6379 注:主:bind 192.168.10.1 (指定主机IP) 当然, 你 ...

  4. Event Serializers官网剖析(博主推荐)

    不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) Flume ...

  5. 学习笔记:Vue——自定义指令

    在Vue2.0中,代码复用和抽象的主要形式是组件.然鹅,有的情况下,你仍然需要对普通DOM元素进行底层操作,这时候就会用到自定义指令. 1.举个聚焦输入框的例子,全局注册focus指令 Vue.dir ...

  6. WinPcap 简介

    WinPcap(windows packet capture) 它包括一个核心态的包过滤器NPF,一个底层的动态链接库(packet.dll)和一个高层的不依赖于系统的库(wpcap.dll). [w ...

  7. 零基础学python-2.3 凝视

    在python里面,使用"#"号表示凝视的開始,一整行到结束就是凝视,他的主要作用提示这段代码到底有什么用处 print("---------欢迎来到猜数字的地方.请開始 ...

  8. 8.2 Android灯光系统_led_class驱动

    android-5.0.2\hardware\libhardware\include\hardware\lights.h  //系统一些宏定义 android源码只带的灯光驱动在linux内核的dri ...

  9. IOS使用AsyncSocket进行Socket通信

    首先导入CFNetwork.framework框架 1.下载ASyncSocket库源码 2.把ASyncSocket库源码加入项目 3.在项目增加CFNetwork框架 使用AsyncSocket开 ...

  10. Bootstrap相关优质项目必备网址

    1:文档全集:这里收集了Bootstrap从V1.0.0版本到现在,整个文档的历史.Bootstrap本身就是一个传奇,而这些文档就是传奇的见证! 官方网址:http://docs.bootcss.c ...