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. jquery发送json请求,给springmvc接收

    js var obj = { 'name':name, 'desc':desc, 'scheduleStartTime':scheduleStartTime, 'scheduleEndTime':sc ...

  2. Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  3. k8s的网络学习

    1.Kubernetes 网络模型 Kubernetes 采用的是基于扁平地址空间的网络模型,集群中的每个 Pod 都有自己的 IP 地址,Pod 之间不需要配置 NAT 就能直接通信.另外,同一个 ...

  4. 使用kubeadm安装kubernetes1.12.1

    kubeadm是kubernetes官方用来自动化高效安装kubernetes的工具,手动安装,特别麻烦. 使用kubeadm安装无疑是一种不错的选择. 1.环境准备 1.1系统配置 系统是CentO ...

  5. HDU 2164(模拟)

    Rock, Paper, or Scissors? Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  6. 求一个整数个位数之和 Exercise06_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求一个整数个位数之和 * */ public class Exercise ...

  7. Scala实战高手****第10课:Scala继承彻底实战和Spark源码鉴赏

    isInstanceOf  和 asInstanceOf is用于判断 as用于转换,把父类类型转换成子类类型   getClass 具体找出类型    

  8. jdk8新特性

    JDK8新特性(JDK8的新特性) * 接口中可以定义有方法体的方法,如果是非静态,必须用default修饰 * 如果是静态的就不用了 class Test { public void run() { ...

  9. Eclipse快速补全快捷键Ctrl+1修改为Android Studio的Alt+Enter

    步骤: Window ->Preferences->key-> type filter text 下输入quick fix(这个是快速补全的快捷键)改为Alt+Enter 下面的wh ...

  10. 魅族 -- 禁止D及以下级别LOG的输出

    真机调试时:Log.d()打印不出信息,模拟器可以. 使用的手机:魅族. 状况:禁止D及以下级别LOG的输出. PS.Log.d("h_bl", "进入执行") ...