[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 ...
随机推荐
- Ubuntu安装redis和redis-php扩展
通过apt-get安装的redis使用方法 sudo apt-get install redis-server sudo apt-get install php-redis vim /etc/redi ...
- [BZOJ5317][JSOI2018]部落战争(闵可夫斯基和)
对于点集$A$,$B$,闵可夫斯基和$C=\{(x1+x2,y1+y2)|(x1,x2)\in A,(y1,y2)\in B\}$.由此可知,对于两个凸包$A$,$B$的闵可夫斯基和$C$满足,$C$ ...
- 【BZOJ】4565: [Haoi2016]字符合并
4565: [Haoi2016]字符合并 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 690 Solved: 316[Submit][Status ...
- 在Hexo中渲染MathJax数学公式
最近学机器学习涉及很多的数学公式,公式如果用截图显示,会比较low而且不方便.因此需要对Hexo做些配置,支持公式渲染.同时文末整理了各种公式的书写心得,比如矩阵.大小括号.手动编号.上下角标和多行对 ...
- cdoj 841 休生伤杜景死惊开 逆序数/树状数组
休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 陆伯言军陷八卦 ...
- Sql 先进先出计算积分
先建表,插入测试数据 --正积分表 CREATE table tb1 ( ) NOT NULL, ) NOT NULL, ) NULL, [point] [int] NULL ) ) ) ) ) ) ...
- (转,记录用)jQuery页面加载初始化的3种方法
jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看习惯吧,本人觉得第二种方法最好,比较简洁. 第一种: $(document).ready(functio ...
- ROS知识(19)----写一个简单的pluginlib例子
参考资料: 官方教程:Writing and Using a Simple Plugin
- 活动(Activity)
一.用Log打印日志 Log.d("HelloWorldActivity", "onCreate execute"); 二.Toast用法 Toast.make ...
- Jenkins搭建.NET自动编译测试与发布环境
本文地址: http://blog.csdn.net/wangjia184/article/details/18365553 操作系统Windows, 确保需要的.NET Framework已经安装 ...