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. 常用的find命令

    find命令 find [路径名] –name/-size/-perm find [路径名] –name “*p” 在路径搜索p结尾的文件夹及文件 find [路径名] –name “[ab]*” 在 ...

  2. 《Java编程思想》笔记 第二十一章 并发

    1.定义任务 实现Runnable 接口的类就是任务类(任务类不一定是实现Runnable接口的类). 实现Runnable 接口,重写run()方法,run方法的返回值只能是 void 任务类就是表 ...

  3. python-函数(装饰器)

    装饰器 装饰器的主要功能: 在不改变函数调用方式的基础上在函数的前.后添加功能. 装饰器的固定格式: #装饰器的本质 :闭包函数 #功能:就是在不改变原函数调用方式的情况下,在这个函数前后加上扩展功能 ...

  4. WN7下安装office2013编辑文档反应这么慢?

    把office在高级选项里面“禁用硬件加速”给打勾就OK了. [office 2013密钥] 9MBNG-4VQ2Q-WQ2VF-X9MV2-MPXKV F2V7V-8WN76-77BPV-MKY36 ...

  5. PHP的文件操作类

    <?php class file { function file() { die("Class file can not instantiated!"); } //创建目录 ...

  6. mysql table status

    SHOW TABLE STATUS 能获得表的信息 可以SHOW TABLE STATUS where name='表名'

  7. POJ 2438 Children's Dining(哈密顿回路)

    题目链接:http://poj.org/problem?id=2438 本文链接:http://www.cnblogs.com/Ash-ly/p/5452615.html 题意: 有2*N个小朋友要坐 ...

  8. usaco1.4.3等差数列

    为这道苟题鼓掌 题目: 一个等差数列是一个能表示成a, a+b, a+2b,…, a+nb (n=0,1,2,3,…)的数列.在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合 ...

  9. hdu6231

    hdu6231 题意 给出一些数字,对于任意长度不小于 \(k\) 的区间,把第 \(k\) 大数加入到一个新的数组 \(B\) 中,求 \(B\) 数组第 \(m\) 大数. 分析 二分答案 \(x ...

  10. golang笔记:net/smtp

    跟go语言的net/smtp斗争了一天,记录下历程.   先用最标准的例子 host := net.JoinHostPort(hostname, port) auth := smtp.PlainAut ...