Recentlly, I am learning crocks.js ADT libaray. In the beginning, it is hard to understand when to use 'runWith', 'evalWith', 'execWith'. Until I went though the coursea thrid times... I finally have some feelings for it.

State has 'get, put, modify' methods for use, and you can build them by yourself as well:

// getState :: () -> State s
const getState = () => State(s => Pair(s, s)) //putState :: s -> State s ()
const putState = state => State(() => Pair(Unit(), state)); // modifyState :: (s -> s) -> State s ()
const modifyState = fn => State(s => Pair(Unit(), fn(s)));

As well can see, for those methods, they are all using 'Pair'.

Pair(a, s):

On the left part of Pair, is 'a': stand for variable; On the right part of Pair, 's' is the State.

Now rules for using 'runWith', 'evalWith', 'execWith':

1. If you want to get result as Pair(a, s), you should use 'runWith':

const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.runWith(bubble) // Pair( (), { bubbles: 41 } )
)

2. If you only instreaded in the variable 'a', you should use 'evalWith':

const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.evalWith(bubble) // () Unit
)

3. If you instested in State, which is on the right part of Pair, you should use 'execWith':

const bubble = {
bubbles:
};
const add = x => y => x+ y;
console.log(
modify(mapProps({bubbles: add()}))
.execWith(bubble) // { bubbles: 41 }
)

[Functional Programming 101] runWIth, evalWith, execWith的更多相关文章

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

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

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

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

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

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

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

  6. Functional programming

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

  7. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  8. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  9. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

随机推荐

  1. hihocoder 1509 异或排序

    题面在这里! 考虑前后两个数 x,y,可以发现S只有在(x xor y)的最高有1位上的取值是要被确定的 (如果x==y那么没有限制),可以推一下什么情况下是1/0. 于是我们模拟一下这个操作,判一判 ...

  2. LuoguP5017 摆渡车 $dp$

    题意 戳这里 吐槽 听同学说今年\(pjT3\)很难,于是就去看了下. 一眼斜率优化...为什么\(n,m\)这么小啊... 感觉这题出的还是不错的. Solution 首先我们先转化一波题意:给出数 ...

  3. 浙江省队选拔 ZJOI2015 (Round 1) 解题报告

    最近莫名其妙地喜欢上了用这种格式写各省省选的全套题解= = 今年浙江省选的出题人是算法竞赛界传说级人物陈立杰,看样子他的出题风格很有特点……ABC三题难度是严格递减的,感觉如果在做第一题的时候被卡住的 ...

  4. codecombat js

    #1 // Move to the gem. // Don't touch the walls! // Type your code below. this.moveRight(); this.mov ...

  5. ACdream 速攻组~

    1007 a + b /*这题就是一个快速幂,但是十分猥琐的是,模是1e10 + 7,不是1e9 + 7,这就产生了一个爆long long的问题.所以要对快速幂中的乘法操作进行一下改造.请教了BIT ...

  6. CGI 、PHP-CGI、FASTCGI、PHP-FPM

    CGI是干嘛的? CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.web server(比如说nginx)只是内容的分发者.比如,如果请求的是/index/ht ...

  7. 使用Chrome快速实现数据的抓取(一)——概述

    对于一些简单的网页,我们可以非常容易的通过Develop Tool来获取其请求报文规律,并仿照其构建报文来获取页面信息.但是,随着网页越来越复杂,许多页面是由js动态渲染生成的.要获取这类信息,则需要 ...

  8. Java的线程和多线程教程

    Java线程(Java Thread)是执行某些任务的一种轻量级进程.Java中的Thread类提供了多线程(multi-threading)功能,应用程序能够创建多个线程并同一时候执行. 在一个应用 ...

  9. adb root : adbd cannot run as root in production builds

    在有些android手机上使用adb root希望获取root权限时出现如下提示信息:adbd cannot run as root in production builds.此时提升root权限的方 ...

  10. 向USB设备发送SCSI命令

    http://bbs3.driverdevelop.com/simple/?t84347.html { BOOL status = ; DWORD accessMode = , shareMode = ...