[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 ...
随机推荐
- JS计算两个时间差的问题
计算两个时间差的问题 function getDateIsMatching(){ var pactbegindate=$("#loanbegindate").datetimebox ...
- 5.flume实战(二)
需求:监控一个文件实时采集新增的数据并输出到控制台 简单理解就是:监控一个文件,只要这个文件有新的内容追加,就将它输出到控制台. agent技术选型:exec source + memory chan ...
- Mark : Bootstrap fileInput控制预览页面上传、删除、详情按钮
Bootstrap fileInput默认预览上传效果: 而我们可能想要的结果是: 这时候可以通过初始参数layoutTemplates来控制:
- 如何让footer一直在网页底部(不使用绝对定位并且网页不论长度多长)
html: <html> <head> <link rel="stylesheet" href="layout.css" ... ...
- cpu中的缓存和操作系统中的缓存分别是什么?
cpu中的缓存和操作系统中的缓存分别是什么? 在操作系统中,为了提高系统的存取速度,在地址映射机制中增加了一个小容量的联想寄存器,即块表.用来存放当前访问最频繁的少数活动页面的页数.当某用户需要存取数 ...
- LAMP安装细则
利用xshell从Windows向Linux传输文件[root@nanainux ~]#yum install lrzsz[root@nanalinux ~]#rz MySq二进制包安装 mysql ...
- [图解算法] 归并排序MergeSort——<递归与分治策略>
#include"iostream.h" void Merge(int c[],int d[],int l,int m,int r){ ,k=l; while((i<=m)& ...
- Win10系统解决C盘分区限制一半的问题
1,按照网上的方法还不行,如链接 2,安装如下软件,里面有激活码,链接 链接:https://pan.baidu.com/s/14ifYpnCMGwJIbgykTYQR6Q 密码:whh3 3,安装并 ...
- HDU 6362.oval-and-rectangle-数学期望、微积分 (2018 Multi-University Training Contest 6 1001)
2018 Multi-University Training Contest 6 6362.oval-and-rectangle 题意就是椭圆里画内接矩形,问你矩形周长的期望. 比赛的时候推了公式,但 ...
- Codeforces Round #424 A(模拟)
#include<cstdio> ]; int main(){ scanf("%d",&n); ;i<=n;++i)scanf("%d" ...