What's the difference between map and flatmap? First, let's show what map is. To show that, I need a source stream, so I'm going to make an interval. It takes a tenth of a second, and I'm only going to take 10 values, and subscribe to it.

var source = Rx.Observable.interval(100).take(10).map((x) => x*2);
console.clear();
source.subscribe(function(res){
console.log(res);
}); /*
0
2
4
6
8
10
12
14
16
18
*/

But what happens if I want to do something asynchronous in here? To show this, I'm going to return an observable timer, which is just going to wait for half of a second, and then map to exactly the same values.

var source =
Rx.Observable.interval(100).take(10)
.map(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
});
console.clear();
source.subscribe(function(res){
console.log(res.toString());
}); /*
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
*/

'Rx.Observable.timer(100)' causes it delay 100 ms.

You find You're going to get objects back. That's because these objects are observables. I'd really, really like to get my values back into my stream. There's actually two ways to do this.

First Way: mergerAll()

What a merge all does is, it takes in a stream of observables, or an observable of observables, and merges them together as they come in.

It subscribes to each one, and pumps them into one output stream.

var source =
Rx.Observable.interval(100).take(10)
.map(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
}).mergeAll();
console.clear();
source.subscribe(function(res){
console.log(res.toString());
}); /*
"0"
"1"
"2"
"3"
"4"
"5"
"6"
"7"
"8"
"9"
*/

Second way: flatMap()

What flatmap is going to do is it's going to perform this mapping function and then subscribe to each observable returned by the map.

var source =
Rx.Observable.interval(100).take(10)
.flatMap(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
});
console.clear();
source.subscribe(function(res){
console.log(res.toString());
});

[RxJS] map vs flatMap的更多相关文章

  1. 第35讲:List的map、flatMap、foreach、filter操作代码实战

    List类的高阶方法          val fmap = List( 1,2 ,3). map { _ + 1 } //List(2, 3, 4)    val fruit_rev2 = frui ...

  2. scala学习笔记(8): 列表的map,flatMap,zip和reduce

    map,flatMap,zip和reduce函数可以让我们更容易处理列表函数. 1 map函数map将一个函数应用于列表的每一个元素并且将其作为一个新的列表返回.我们可以这样对列表的元素进行平方: s ...

  3. java8中stream的map和flatmap的理解

    转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...

  4. map和flatmap的区别+理解、学习与使用 Java 中的 Optional

    转自:map和flatmap的区别 对于stream,   两者的输入都是stream的每一个元素,map的输出对应一个元素,必然是一个元素(null也是要返回),flatmap是0或者多个元素(为n ...

  5. spark中map与flatMap的区别

    作为spark初学者对,一直对map与flatMap两个函数比较难以理解,这几天看了和写了不少例子,终于把它们搞清楚了 两者的区别主要在于action后得到的值 例子: import org.apac ...

  6. Spark中map与flatMap

    map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码: def map(self, f, preservesPartitioning=False): &q ...

  7. 第88讲:Scala中使用For表达式实现map、flatMap、filter

    今天我们来学习一下如何使用for表达式实现map.flatMap以及filter 首先,我们来看下map.map的功能是,传入一个list,通过一个函数f,将list中的元素A变成元素B的过程.最后得 ...

  8. Swift中的map 和 flatMap 原理及用法

    之前对这两个概念有点糊,今天正好遇到一个相关需求,才深入了解了下. 需求如下: 大概就是对一个数组的model,重构成一个新model,返回得到一个新数组 用map很容易实现,不过后来我需要对其中进行 ...

  9. 【Android】RxJava的使用(三)转换——map、flatMap

    前两篇Android RxJava的使用(一)基本用法.Android RxJava的使用(二)Action介绍了RxJava的基本用法,对Rxjava还不了解的请先看以上两篇.这篇为大家讲解RxJa ...

随机推荐

  1. Object之克隆对象clone 和__clone()函数

    在前面的PHP面向对象之对象和引用,"$b=$a"的方式复制对象传递的是对象的地址,而不是传递对象的值(内容),我们可以通过克隆对象来实现对对象的内容和引用的复制 使用传址引用的方 ...

  2. 聊一聊c++中指针为空的三种写法 ----->NULL, 0, nullptr

    看到同事用了一下nullptr.不是很了解这方面东东,找个帖子学习学习 http://www.cppblog.com/airtrack/archive/2012/09/16/190828.aspx N ...

  3. EQueue 2.3.2

    EQueue 2.3.2版本发布(支持高可用) 前言 前段时间针对EQueue的完善终于告一段落了,实在值得庆祝,自己的付出和坚持总算有了成果.这次新版本主要为EQueue实现了集群功能,基本实现了B ...

  4. C#使用字符串分割字符串

    我们都会用字符分割字符串: string[] recvArr = recv.Split(';'); 如果用字符串分割呢?下面: string[] sArray = Regex.Split(recv, ...

  5. hexo 适合前端 geek 的博客

    原文出自:http://www.qiangji.tk/hexo%E9%80%82%E5%90%88%E5%89%8D%E7%AB%AFgeek%E7%9A%84%E5%8D%9A%E5%AE%A2/ ...

  6. Codeforces 713 C Sonya and Problem Wihtout a Legend

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  7. Java实现生产者消费者

    方法1:最简单--利用LinkedBlockingQueue 队列具有先进先出的特点,成为经常应用于生产-消费者模式的数据结构. 1.将一个对象放到队列尾部,如果队列已满,就等待直到有空闲节点. —— ...

  8. random随机函数

    SQL> select * from (select ename,job from emp order by dbms_random.value() ) where rownum <5 2 ...

  9. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  10. git 添加忽略文件

    使用github for windows客户端添加.gitignore文件:   如下图所示,在github客户端可以看到未提交的更改列表 随便选中一个文件,右链,选择ignore file. 然后会 ...