[Functional Programming 101] runWIth, evalWith, execWith
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的更多相关文章
- [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 ...
- 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 ...
- Java 中的函数式编程(Functional Programming):Lambda 初识
Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
随机推荐
- android 安全退出 activity
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 定义一个 活动 的基础类, 每次打开一个 活动,就记录下来. 退出时,关闭每一个 活动. ...
- BZOJ 3238 [Ahoi2013]差异(后缀自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个串,设T[i]表示从第i位开始的后缀, 求sum(len( ...
- 2018-2019-20172329 《Java软件结构与数据结构》第三周学习总结
2018-2019-20172329 <Java软件结构与数据结构>第三周学习总结 教材学习内容总结 <Java软件结构与数据结构>第五章-队列 一.概述 1.队列是什么? 队 ...
- PYQT窗口托盘目录
#UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*- from PyQt4 import QtCore, QtGui try: _fromU ...
- (转)js中的hasOwnProperty和isPrototypeOf方法
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.isPrototypeOf ...
- ubuntu 自动获取ip
$sudo dhclient -r $sudo dhclient $sudo dhclient eth0
- 探秘GO语言《比较C#与GO的性能》
这段时间也来学学GO语言,听说它的性能相当的棒棒,我就拿C#来和它做比对一下. 这里只是单纯了做了for循环的比对,看看谁的循环快 C# 代码: static void Main(string[] a ...
- cocos2d-x3.0 macOS下配置Android开发环境以及使用cocos2d-console来新建执行project
下面是子龙山人录制的关于cocos2d-x3.0的视频教程,macOS下配置Android开发环境.使用cocos2d-console来新建执行project.怎样执行cocos2d-x 3.0win ...
- Backup your Android without root or custom recovery -- adb backup
ecently discovered a neat new way to back up apps on my Android without having to use Titanium Backu ...
- sql server 游标continue,总是死循环
也遇上过: 死循环是因为continue后又执行与上次相同的fetch了.在continue前加一个fetch next from就可以了.