[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 ...
随机推荐
- 在phpWeChat里生成一个临时二维码(非微信二维码)
phpWeChat作为支持Pc+H5开发的管理系统,内置一套二维码生成API,访问地址: 您的域名/api/qrcode/index.php?data=二维码数据 以下为使用示例 使用时有一点需要注意 ...
- hdu 1058 dp.Humble Numbers
Humble Numbers Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- dalvik 基于 jvm 的改进
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 几个class 变成一个dex.constant pool 省内存 zygote ,co ...
- RxJava 机制
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha rxjava 是 以 响应式 编程思想 编程的 java类库
- PHP 笔记——Array 数组
要点 说明 数组构成 数组是由一个或多个数组元素组成的 数组元素 每个数组元素由键(Key)和值(Value)构成 键 元素的识别名称,也被称为数组下标 值 元素的内容 映射 键 和 值 之间存在一种 ...
- CXF和Axis2开发webservice也是可以添加asmx等后缀
在当家互联网时代, 手机APP所需要的后台服务接口经常会变化, 如果前期没有设计好, 把它们的请求地址配置在比较稳定不会经常修改的地址(例如专门一个后台服务用于获取所有最新的数据服务地址)这样不会因为 ...
- 在windows上安装和启动Elasticseach、Kibana
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- [转]jquery加载页面的方法(页面加载完成就执行)
jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){ $("#a&q ...
- leetcode132. Palindrome Partitioning II
leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...
- MySQL之char、varchar和text的设计
最近有表结构设计中出现了varchar(10000)的设计引起了大家的讨论,我们下面就来分析分析. 首先我们先普及一下常识: 1.char(n)和varchar(n)中括号中n代表字符的个数,并不代表 ...