[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 ...
随机推荐
- [ 总结 ] nginx 负载均衡 及 缓存
操作系统:centos6.4 x64 前端使用nginx做反向代理,后端服务器为:apache + php + mysql 1. nginx负载均衡. nginx编译安装(编译安装前面的文章已经写过) ...
- mysql:functional dependency
0down vote First, a functional dependency in the form A->B means that, given one value for A, we ...
- rest_framework 权限流程
权限流程 权限流程与认证流程非常相似,只是后续操作稍有不同 当用户访问是 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进行限制 s ...
- 前端读者 | 别人写的css,你敢用吗?
本文来自@yeaseonzhang:链接:http://yeaseonzhang.github.io/2018/04/10/%E5%88%AB%E4%BA%BA%E5%86%99%E7%9A%84cs ...
- SPOJ PT07J - Query on a tree III(划分树)
PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...
- 33、Django实战第33天:我的消息
1.编辑usercenter-message.html继承usercenter-base.html 2.编辑users.views.py ... from operation.models impor ...
- 多表联合查询,利用 concat 模糊搜索
select * from t1 as a join t2 as b on a.id = b.id where CONCAT(a.name,b.name) like '%测试%'
- shell head tail
head: 打印文件的前10行(默认) tail: 打印文件的后10行(默认) 如果你是个典型的开发或者部署人员,是不是觉得开始亲切起来.我们平时用的最多的命令 $tail -f cata ...
- ubuntu 下终端关于调试C++的命令
先确定安装了vim 和gcc (c语言)或者g++(c++) 如果没有安装可以在终端输入以下命令: sudo apt-get install build-essential sudo apt-get ...
- 【计算几何】【状压dp】Codeforces Round #226 (Div. 2) D. Bear and Floodlight
读懂题意发现是傻逼状压. 只要会向量旋转,以及直线求交点坐标就行了.(验证了我这俩板子都没毛病) 细节蛮多. #include<cstdio> #include<algorithm& ...