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的更多相关文章

  1. [Compose] 10. Capture Side Effects in a Task

    We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...

  2. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  3. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  4. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  5. 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 ...

  6. 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 ...

  7. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

  8. Functional programming idiom

    A functional programming function is like a mathematical function, which produces an output that typ ...

  9. 关于函数式编程(Functional Programming)

    初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...

随机推荐

  1. 去除win7桌面图标小箭头.bat

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" ...

  2. 【Java虚拟机】JVM学习笔记之GC

    JVM学习笔记二之GC GC即垃圾回收,在C++中垃圾回收由程序员自己来做,例如可以用free和delete来回收对象.而在Java中,JVM替程序员来执行垃圾回收的工作,下面看看GC的详细原理和执行 ...

  3. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  4. 【四边形不等式】HDU3506-Monkey Party

    [题目大意] 香蕉森林里一群猴子(n<=1000)围成一圈开会,会长给他们互相介绍,每个猴子需要时间a[i].每次只能介绍相邻的两只猴子x和y认识,同时x所有认识的猴子和y所有认识的猴子也就相互 ...

  5. python使用UnboundMethodType修改类方法

    from types import UnboundMethodType class class1(object): def fun1(self): print 'fun1' oldfun1 = cla ...

  6. 【转】代码混淆和apk反编译

    代码混淆 http://blog.csdn.net/vipzjyno1/article/details/21042823 apk反编译 http://blog.csdn.net/vipzjyno1/a ...

  7. 不按装mysql情况下,php安装pdo_mysql

    安装pdo时遇到 --with-pdo-mysql  这个要指向mysql安装目录:可是我这台机器不安装mysql; 解决方法: [root@localhost app]#  yum install ...

  8. linux常见命令集合(下)

    1. tar zcvf backup-$(date "+%Y-%m-%d").tar.gz demo01dir 常用命令集合 echo helloworld date “+%y-% ...

  9. CROC 2016 - Elimination Round (Rated Unofficial Edition) B. Mischievous Mess Makers 贪心

    B. Mischievous Mess Makers 题目连接: http://www.codeforces.com/contest/655/problem/B Description It is a ...

  10. StoryBoard学习(5):使用segue页面间传递数据

    StoryBoard学习(5):使用segue页面间传递数据 函数: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sen ...