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. 限制tomcat仅响应本机请求(转)

    http://blog.bbzhh.com/index.php/archives/135.html 在VPS上搭建了nginx和tomcat应用,想通过nginx来反向代理127.0.0.1:8080 ...

  2. 1.6 Python基础知识 - for循环

    在循环语句中,除了while循环外,还有一种循环叫for循环的循环语句,for循环语句用于遍历可迭代(什么是迭代?以及迭代的相关知识,我们到后面再进行阐述,这里只要记住就可以了.)对象集合中的元素,并 ...

  3. 杭州"人才新政22条" 硕士来杭工作一次性补贴2万元

    转载自原文杭州"人才新政22条" 硕士来杭工作一次性补贴2万元 2016-11-8 继去年1月推出“人才新政27条”后,杭州在引才上又将有新动作.在昨天举行的2016浙江·杭州国际 ...

  4. lettuce--Advanced Redis client

    redis官方提供的java client: git地址:https://github.com/mp911de/lettuceAdvanced Redis client for thread-safe ...

  5. 关于spring获取webApplication.getBean多种途径和简单解释

    ApplicationContext ac1 = new FileSystemXmlApplicationContext("com/spark/system/applicationConte ...

  6. 漂亮的Android加载中动画:AVLoadingIndicatorView

    AVLoadingIndicatorView 包含一组漂亮的Android加载中动画. IOS版本:here. 示例 Download Apk 用法 步骤1 Add dependencies in b ...

  7. thinkphp3.2 图片平均颜色值

    public function imgColor($imgUrl) { $imageInfo = getimagesize($imgUrl); //图片类型 $imgType = strtolower ...

  8. MinGW、MinGW-w64 与TDM-GCC 应该如何选择?

    MinGW.MinGW-w64 与TDM-GCC 应该如何选择? https://www.zhihu.com/question/39952667

  9. Synopsys工艺库札记

    Synopsys工艺库札记 库的基本信息 库类 库类语句指定库名. library ( smic13HT_ss ) { ... <lirary description> ... } /*e ...

  10. HDU - 3078 Network(暴力+LCA)

    题目大意:给出n个点的权值.m条边,2种操作 0 u num,将第u个点的权值改成num k u v,询问u到v这条路上第k大的权值点 解题思路:该点的话直接该,找第k大的话直接暴力 #include ...