[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 ...
随机推荐
- StringUtils.isEmpty()和StringUtils.isBlank() 区别
isBlank()判断空的情况包括了isEmpty()的情况,isBlank()不仅判断了 无对象.空对象的情况,而且也判断了无意义的空白字符,比如空格等.
- 使用Windows下的git工具往github上传代码 踩坑记录
使用Windows下的git工具往github上传代码 踩坑记录 背景 由于以前接触的项目都是通过svn进行版本控制,现在公司项目使用git,加上自己平时有一个练手小项目,趁着周末试着把项目上传到自己 ...
- Oracle存储过程学习笔记
SQL是一种语言! SQL是一种语言! SQL是一种语言! 个人理解:存储过程就相当于Java中的方法;声明变量区域就相当于java中的声明局部变量一样,只是放到一个指定区域定义了 一.先看一部分基础 ...
- js判断处理undefined类型的数据
code: resFlag = response.result.data.result; /查询客户为白名单用户时,将"*该企业已被列入黑名单"标记清除 if(typeof res ...
- 存储过程 ----- navicat 创建存储过程
以下为navicat 创建存储过程步骤图解: 1. 2. 3. 4. 在存储过程正文中是输入一行语句测试用,点击保存 5.输入存储过程名称,点击确定 6.到这来那么问题来了,会提示错误 7.切记存储过 ...
- 微信支付http://www.cnblogs.com/True_to_me/p/3565039.html
公众号支付有2种支付方式: JS API 支付:是指用户打开图文消息或者扫描二维码,在微信内置浏览器打开网页进行的支付.商户网页前端通过使用微信提供的 JS API,调用微信支付模块.这种方式,适合需 ...
- Selenium2+python自动化20-Excel数据参数化【转载】
前言 问: Python 获取到Excel一列值后怎么用selenium录制的脚本中参数化,比如对登录用户名和密码如何做参数化? 答:可以使用xlrd读取Excel的内容进行参数化.当然为了便于各位小 ...
- 使用python获取整月每一天的系统监控数据生成报表
1.安装阿里开源监控工具tsar tsar官方网站 wget -O tsar.zip https://github.com/alibaba/tsar/archive/master.zip --no-c ...
- 拒绝平庸——浅谈WEB登录页面设计
用户活跃度是检验产品成功与否的重要指标之一,传统行业的商家极为重视门面的装潢,因为一个好的门面可以聚集人气,招揽更多的顾客.古时候的大户人家院子门口的石狮子或其他的摆件的摆放极为讲究,有一定的风水学说 ...
- hdu 5179(数位DP||打表)
beautiful number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...