[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 ...
随机推荐
- LeetCode(39):组合总和
Medium! 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...
- PHP抽象类
<?php /* * abstract * 抽象类: * 1.至少有一个抽象方法(没有具体实现的方法) * 2.不能被实例化,可以被继承 * 3.抽象类可以有子抽象类 * 相对于接口: * 1. ...
- BBC 记录片planet earth
He'll have to remain on guard for another two weeks, but in the jungle, just surviving the day can c ...
- 超实用!!!使用IDEA插件Alibaba Cloud Toolkit工具一键部署本地应用到ECS服务器
最近看到阿里云发布了一款名为 Alibaba Cloud Toolkit 的插件,可以帮助开发者高效开发并部署适合在云端运行的应用,瞬间击中了我的小心脏,这个对于个人开发者来说超级棒啊,终于不需要再手 ...
- hdu 4463 有一条边必须加上 (2012杭州区域赛K题)
耐克店 和 苹果店必须相连 Sample Input42 30 01 00 -1 1 -10 Sample Output3.41 # include <iostream> # includ ...
- Floyd-傻子也能看懂的弗洛伊德算法(转)
暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程,小哼希望在出发之前知道任意两个城市之前的最短路程. ...
- django csrf_protect及浏览器同源策略
1.django在检测post行为时会有诸多的限制. 为了防止跨域请求伪造安全 参考:http://www.qttc.net/201209211.html https://www.cnblogs. ...
- js获取按键
event.altKey.event.ctrlKey.event.shiftKey 属性 属性为true表示事件发生时Alt.Ctrl.Shift键被按下并保持,为false则Alt.Ctrl.Shi ...
- poj 1579 Function Run Fun 【记忆化递归】
<题目链接> 题目大意: 给出一些递归式,直接套用这些递归式计算. 解题分析: 递归式已经由题目明确说明了,但是无脑递归铁定超时,所以此时,我们需要加上记忆化,对于那些已经算过的,就没有必 ...
- Hash值破解工具(findmyhash与hash-identifier破解Hash值)
Hash值破解工具(findmyhash与hash-identifier破解Hash值) 前言: Kali Linux提供各种哈希密文破解工具,如hashcat.john.rainbows.不论哪一种 ...