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?的更多相关文章

  1. [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 ...

  2. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  3. [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); //=> ...

  4. 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 ...

  5. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  6. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  7. 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 ...

  8. 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 ...

  9. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

随机推荐

  1. 5.shell运算符

    无论是哪门语言,运算都是跑不掉的,shell中如何进行运算呢?语法: $((运算式))或者 $[运算式]expr 运算式.

  2. 某dp题

    [NOI联考by ysy]庆典 2016年6月17日1,1040 [题目描述] 战狂在昌和帝国的首都法法城召开了庆典,向一万名最杰出的士兵分发了用魔法猪做的猪肉饺子,士兵们吃了猪肉饺子后,战斗力大幅提 ...

  3. 分析函数调用堆栈的原理和Delphi实现

    来自:http://blog.163.com/liuguang_123/blog/static/816701920105262543890/ ----------------------------- ...

  4. Selenium2+python自动化68-html报告乱码问题【转载】

    前言 python2用HTMLTestRunner生成测试报告时,有中文输出情况会出现乱码,这个主要是编码格式不统一,改下编码格式就行. 下载地址:http://tungwaiyip.info/sof ...

  5. F - 等式(1/x + 1/y = 1/n)

    链接:https://www.nowcoder.com/acm/contest/90/F来源:牛客网 题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均为正整 ...

  6. aoj 0033 Ball【dfs/枚举】

    有一个形似央视大楼(Orz)的筒,从A口可以放球,放进去的球可通过挡板DE使其掉进B裤管或C裤管里,现有带1-10标号的球按给定顺序从A口放入,问是否有一种控制挡板的策略可以使B裤管和C裤管中的球从下 ...

  7. ASP.NET Core 2.2 基础知识(二) 中间件

    中间件是一种装配到应用管道以处理请求和相应的软件.每个软件都可以: 1.选择是否将请求传递到管道中的下一个组件; 2.可在调用管道中的下一个组件前后执行工作. 管道由 IApplicationBuil ...

  8. Verify Preorder Serialization of a Binary Tree -- LeetCode

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  9. 微服务之SpringCloud实战(四):SpringCloud Eureka源码分析

    Eureka源码解析: 搭建Eureka服务的时候,我们会再SpringBoot启动类加上@EnableEurekaServer的注解,这个注解做了一些什么,我们一起来看. 点进@EnableEure ...

  10. microsoft sql server - online Resources 在线资源