[Functional Programming] Using Lens to update nested object
For example, in React application, we have initial state;
const data = {
nextId: 4,
todoFilter: 'SHOW_ALL',
todos: [
{ id: 1, title: 'Hug Unicorn', completed: false },
{ id: 2, title: 'Mess with Texas', completed: false },
{ id: 3, title: 'Do Laundry', completed: true }
],
ui: {
filterGroups: {
status: false
}
}
}
We have a toggle button, which everytime, it is toggle 'ui.filterGroups.status' to true/false.
Whenever we have to update nested object, we should always comes up a word 'Lens'!
Here is how to do it:
import { State, compose } from 'crocks'
import {lensPath, lensProp, over, not} from 'ramda'
const { modify } = State
// ui -> filterGroups -> status
const lnsFilterGroups = lensPath(['ui', 'filterGroups'])
export const toggleFilterGroup = ({ group }) => {
const lns = compose(
lnsFilterGroups,
lensProp(group)
)
return modify(over(lns, not))
}
[Functional Programming] Using Lens to update nested object的更多相关文章
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2
In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
随机推荐
- laravel 接口跨域
最方便的方法,新建一个middleWare,把这个middleware加入到全局中间件,所有的请求,都会经过这个中间件的过滤. php artisan make:middleware CrossHtt ...
- Splay-Tree总结一:模拟队列
伸展树是一种强大的数据结构,由于其特性,可以很好地模拟队列的插队等操作,而线段树解决这类问题通常需要转化一下,比较伤脑筋 而用伸展树的解决方法就是先建好一颗节点数等于队列长度的树,每个队列元素在队列中 ...
- CF 449D 题解(状压+容斥)
状压妙啊... 本题的主体思路:状压+容斥原理(或状压+数位dp) 记g[i]表示按位与后结果所有位上至少有i个1的方案数 那么根据容斥原理,ans=g[0]-g[1]+g[2]-g[3]+g[4]. ...
- python 全栈开发,Day21(抽象类,接口类,多态,鸭子类型)
一.昨日复习 派生方法和派生属性 super 只有在子父类拥有同名方法的时候, 想使用子类的对象调用父类的方法时,才使用super super在类内 : super().方法名(arg1,..) 指名 ...
- DBMS_OUTPUT包学习
DBMS_OUTPUT包中的其他方法和函数的用法,所以这次特地来研究一下. 先简单的讲解一下这个包的所有procedure的含义及作用: ----------------------- 1. ...
- PE文件版本那些事儿
发现文件的版本号很有意思,win7下右键属性显示两个版本号,分别是File Version 和 Product version.但使用vs编辑版本资源里面却有四处版本号,如下: 发现有以下区别,上面为 ...
- Spring事务传播行为
什么是事务传播行为 public void methodA(){ methodB(); //doSomething } @Transaction(Propagation=XXX) public voi ...
- AOJ 0189 Convenient Location (Floyd)
题意: 求某一个办公室 到其他所有办公室的 总距离最短 办公室数 不超过10 输入:多组输入,每组第一行为n (1 ≤ n ≤ 45),接下来n行是 (x, y, d),x到y的距离是d输出:办公室 ...
- SQL中IN和EXISTS用法的区别
结论 1. in()适合B表比A表数据小的情况 2. exists()适合B表比A表数据大的情况 当A表数据与B表数据一样大时,in与exists效率差不多,可任选一个使用. select * fro ...
- 【LeetCode】161. One Edit Distance
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...