[Functional Programming] Capture Side Effects in a Task / Async
We examine the data structure Task, see some constructors, familiar methods, and finally how it captures side effects through laziness.
We are going to check two libarays, one is 'data.task'. another is 'crocks/Async':
Install:
npm i -S data.task
npm i -S crocks
You can use 'of' construct method:
Task.of()
.fork(e => console.error(e), a => console.log(a)) // Async.of('U Wut M8')
.fork(e => console.error(e),a => console.log(a)) // U Wut M8
You can do rejection:
// Foucs to reject:
Task.rejected('not work')
.fork(e => console.error(e), a => console.log(a)) // not work Async.Rejected('Async badguy')
.fork(e => console.error(e),a => console.log(a)) // Async badguy
You can .map / .chain:
Task.of()
.map(x => x + )
.fork(e => console.error(e), a => console.log(a)) // Task.rejected()
.map(x => x + )
.fork(e => console.error(e), a => console.log(a)) // 1 Async.of()
.map(x => x + )
.fork(e => console.error(e),a => console.log(a)) // Async.Rejected()
.map(x => x + )
.fork(e => console.error(e),a => console.log(a)) // Task.of()
.map(x => x + )
.chain(x => Task.of(x + ))
.fork(e => console.error(e), a => console.log(a)) // 4 Async.of()
.map(x => x + )
.chain(x => Async.of(x + ))
.fork(e => console.error(e),a => console.log(a)) // 4
You can use 'contructor function':
const lunchMissiles = () =>
new Task((rej, res) => {
console.log('lunchMissiles');
res('missile!')
}); const lunchRocky = () =>
Async((rej, res) => {
console.log('lunchRocky');
res('Rocky!')
}); lunchMissiles()
.map(x => x + "!")
.fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!! lunchRocky()
.map(x => x + "!")
.fork(e => console.error(e), a => console.log(a)) // lunchMissiles missile!!
Finally, we can split the side effect without calling 'fork', and you compose with the rest of app:
const taskApp = lunchMissiles()
.map(x => x + "!"); const asyncApp = lunchRocky()
.map(x => x + "!") taskApp.map(x => " From Task").fork(e => console.error(e), a => console.log(a))
asyncApp.map(x => " From Async").fork(e => console.error(e), a => console.log(a))
[Functional Programming] Capture Side Effects in a Task / Async的更多相关文章
- [Compose] 10. Capture Side Effects in a Task
We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- 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 ...
- 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 ...
随机推荐
- java.io.BufferedWriter API 以及源码解读
下面是java se 7 API 对于java.io.BufferedWriter 继承关系的描述. BufferedWriter可以将文本写入字符流.它会将字符缓存,目的是提高写入字符的效率. bu ...
- RabbitMQ (十六) 消息队列的应用场景 (转)
原贴 : http://blog.csdn.net/cws1214/article/details/52922267 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题 ...
- 【Java虚拟机】JVM学习笔记之GC
JVM学习笔记二之GC GC即垃圾回收,在C++中垃圾回收由程序员自己来做,例如可以用free和delete来回收对象.而在Java中,JVM替程序员来执行垃圾回收的工作,下面看看GC的详细原理和执行 ...
- zabbix安装配置(2.4.5)
这是第一次安装配置,直接遭遇配置文件不明晰的大坑,因在编译阶段未指明配置文件路径,导致zabbix_server启动时直接读取默认的 /usr/local/zabbix/etc/zabbix_serv ...
- [BZOJ5427]最长上升子序列
考虑O(n log n)的LIS求法,dp[i]表示到目前为止,长度为i的LIS的末尾最小是多少. 当当前数确定时直接用LIS的求法更新dp数组,当不确定时,由于这个数可以是任意数,所以可以接在任意上 ...
- IOS http 请求
asihttprequest 为第三方数据请求,一下为get 和post 两种请求. Get: NSString *loginName=@"Tony"; NSString *pw ...
- Java解读内存,优化编程
1.别用new Boolean 在很多场景中Boolean类型是必须的,比如JDBC中boolean类型的set与get都是通过Boolean封装传递的,大部分ORM也是用Boolean来封装bool ...
- Tasker 中文版(4.6u2)
http://www.anzhi.com/soft_1868913.html Tasker绝对称得上是Android系统的神器之一,与Auto Memory Manager不同,Tasker不是加速型 ...
- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- ssl 复制
http://www.ttlsa.com/mysql/mysql-replication-base-on-ssl/ http://www.tuicool.com/articles/mi2iaq htt ...