[Functional Programming 101] Crocks.js -- when to use map and when to use chain?
As a beginner of Crocks.js, it was a problem for we to figure out when to use .map() and when to use .chain().
Of course, using the docs help:
map: State s a ~> (a -> b) -> State s b
chain: State s a ~> (a -> State s b) -> State s b
The main difference is that, when using .map(fn), the param is a function.
For exmaple: addOne is just a plain function.
const addOne = x => x + ;
When using .chian(sfn), the param is a State(fn)
For example: modify return a state() and inside state(), we apply function addOne.
const modifyOne = () => modify(mapProps({'moves', addOne}));
Now, we are going to write two example, one is using .map() another one is using .chain() to achieve the same result.
// We want to get final result as {moves: 4}
const state = {
moves:
}
.chain():
const { curry, compose, State, mapProps, prop, option } = require("crocks"); const { modify, get } = State; const getState = key => get(prop(key)); const addOne = x => x + ; const modifyOne = () => over('moves', addOne); const over = (key, fn) => modify(mapProps({[key]: fn})) const state = {
moves:
} const getMoves = () => getState('moves').map(option()) console.log(
getMoves()
.chain(modifyOne)
.chain(modifyOne)
.chain(modifyOne)
.execWith(state) // {moves: 4}
)
Notice that 'getMoves' and 'modifyOne' both return State(), so they have to use .chian().
.map():
const { curry, compose, State, mapProps, prop, option } = require("crocks"); const { modify, get } = State; const getState = key => get(prop(key)); const addOne = x => x + ; const state = {
moves:
} const getMoves = () => getState('moves').map(option()) console.log(
getMoves()
.map(addOne)
.map(addOne)
.map(addOne)
.evalWith(state) // 4
)
Since 'addOne' is just a function, we can use .map() instead of .chian(). And more important, we have to use 'evalWith' to get result value, since we are not using 'modify' to change the state.
[Functional Programming 101] Crocks.js -- when to use map and when to use chain?的更多相关文章
- [Functional Programming 101] runWIth, evalWith, execWith
Recentlly, I am learning crocks.js ADT libaray. In the beginning, it is hard to understand when to u ...
- [Functional Programming] Function signature
It is really important to understand function signature in functional programming. The the code exam ...
- [Functional Programming] Functional JS - Pointfree Logic Functions
Learning notes. Video. Less than: If you use 'ramda', you maybe know 'lt, gt'.. R.lt(2, 1); //=> ...
- 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 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
随机推荐
- 【bzoj3439】KPM的MC密码
这题乍一看后缀相等很烦的样子…… 其实如果把字符串倒过来,那么相等的后缀就可以转化成前缀,前缀相等扔进trie就可以了. 剩下无非是Trie的树链kth,主席树随便维护就好. 注意一个串彻底结束才能打 ...
- [ Openstack ] Openstack-Mitaka 高可用之 环境初始化
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- DRF的过滤与排序
过滤 对于列表数据可能需要根据字段进行过滤,我们可以通过添加 django-filter 扩展来增强支持. pip install django-filter 在配置文件中增加过滤后端的设置: INS ...
- java Class.forName()
Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类. 虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class ...
- python Tk()生成的桌面的具体设置方法
rom tkinter import * root = Tk() root['height'] = 300 #设置高 root['width'] = 500 #设置宽 root.title('魔方小站 ...
- ubuntu 16.04安装redis(源码安装)zz
本文转载自: http://www.linuxdiyf.com/linux/22527.html Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Val ...
- Deep Learning关于Vision的Reading List
最近开始学习深度学习了,加油! 下文转载自:http://blog.sina.com.cn/s/blog_bda0d2f10101fpp4.html 主要是顺着Bengio的PAMI review的文 ...
- 学习OpenResty编程
1.Windows版本的下载位置 https://github.com/LomoX-Offical/nginx-openresty-windows Linux下OpenResty的下载和安装 http ...
- [BZOJ2553][BeiJing2011]禁忌 dp+AC自动机+矩阵快速幂
2553: [BeiJing2011]禁忌 Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1206 Solved ...
- 20、Flask实战第20天:Flask上下文
Local线程隔离对象 我们知道通过request可以获取表单中的数据.如果是多个用户同时在用网站,而全局request就只有一个,那么Flask是如何分辨哪用户对应哪个请求呢? 这种情况下,就会用到 ...