[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 ...
随机推荐
- Python函数系列-迭代器,生成器
一 迭代器 一 迭代的概念 #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复,因而不 ...
- pool创建多进程
这中方式用的比较多,毕竟要控制并发数量,不可能不限制并发数 #_*_coding:utf-8_*_ from multiprocessing import Pool import time def f ...
- 【BZOJ 2007】 2007: [Noi2010]海拔 (平面图转对偶图+spfa)
2007: [Noi2010]海拔 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2504 Solved: 1195 Description YT市 ...
- JZYZOJ1311 邮局设置问题 dp
易得每两个点之间建立邮局的最好位置为两点最中间的点,两点之间如果没有奇数个数的点则中间两个点都可以...(自己画一下图可以看出随着右边点的增大最佳点的增大非常平滑...强迫症一本满足) w[i][ ...
- 使用SQL语句将数据库中的两个表合并成一张表
select * into 新表名 from (select * from T1 union all select * from T2) 这个语句可以实现将合并的数据追加到一个新表中. 不合并重复数 ...
- JSP -- include指令与include动作的区别
JSP -- include指令与include动作的区别 (1)格式的区别: include指令:<%@include file = "文件名"%> include动 ...
- Linux知识(6)----VIM
vi的第一版是由Bill Joy在1978年写成的,当时他是UC Berkeley的学生.后来他共同创建了神奇的Sun公司.vi来源于visual一词,目标是在终端上可视化地模拟文本的编辑,是的更人性 ...
- 用rem设置文字大小
一.px与em 用px设置文字大小是再正常不过的事情,比如 html {font-size: 12px;} 随处可见的在设置width.height使用px,这也是细致稳妥的设置方法,这样做的缺点在于 ...
- Git配置非22端口,解决:ssh: connect to host xxx port 22: Connection timed out fatal: The remote end hung up unexpectedly
背景:私自搭建了Git服务器,而Git本身就是SSH进行连接的,而Git命令上默认只能通过22端口实现. 解决方法: 第一种: 在系统的用户目录下的文件夹:.ssh 如果该路径下没有config文件, ...
- mysql中日期比较大小的方法
假如有个表product有个字段add_time,它的数据类型为datetime,有人可能会这样写sql: select * from product where add_time = '2013-0 ...