[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 ...
随机推荐
- 6大原则java
1.开闭原则(Open Close Principle)定义:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 开放-封闭原则的意思就是说,你设计的时候,时刻要考虑,尽量让这个类是足够好 ...
- 「CTSC2016」单调上升路径
「CTSC2016」单调上升路径 解题思路:根据提示可以得到答案的下界是 \(n - 1\) ,然后打表发现这个下界好像一定可以取到. 事实上考虑 \(n\) 个点完全图的边数是 \(\frac{n( ...
- Problem F: 深入浅出学算法007-统计求和
Description 求含有数字a且不能被a整除的4位整数的个数,并求这些整数的和 Input 多组测试数据,先输入整数T表示组数然后每组输入1个整数a(1<=a<=9) Output ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- CDOJ 842 天下归晋 树状数组
天下归晋 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/842 Descrip ...
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HTML5 元素拖动 - 实现元素左右拖动, 或更改自身排序
1.拖放(Drag 和 drop)是 HTML5 标准的组成部分. 拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 浏览器支持:Int ...
- 【原】Eclipse部署Maven web项目到tomcat服务器时,没有将lib下的jar复制过去的解决办法
我们在做web开发是,经常都要在eclipse中搭建web服务器,并将开发中的web项目部署到web服务器进行调试,在此,我选择的是tomcat服务器.之前部署web项目到tomcat进行启动调试都很 ...
- 发现一个CentOS第三方源epel的仓库地址(repos.fedorapeople.org)
这个站点(http://repos.fedorapeople.org)很有意思,比如要安装maven,官方源默认是没有的,当然可以安装epel源,而如果只要单独安装,可以直接安装一个仓库地址. 下面将 ...
- 数论E - Biorhythms(中国剩余定理,一水)
E - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...