[RxJS] map vs flatMap
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的更多相关文章
- 第35讲:List的map、flatMap、foreach、filter操作代码实战
List类的高阶方法 val fmap = List( 1,2 ,3). map { _ + 1 } //List(2, 3, 4) val fruit_rev2 = frui ...
- scala学习笔记(8): 列表的map,flatMap,zip和reduce
map,flatMap,zip和reduce函数可以让我们更容易处理列表函数. 1 map函数map将一个函数应用于列表的每一个元素并且将其作为一个新的列表返回.我们可以这样对列表的元素进行平方: s ...
- java8中stream的map和flatmap的理解
转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...
- map和flatmap的区别+理解、学习与使用 Java 中的 Optional
转自:map和flatmap的区别 对于stream, 两者的输入都是stream的每一个元素,map的输出对应一个元素,必然是一个元素(null也是要返回),flatmap是0或者多个元素(为n ...
- spark中map与flatMap的区别
作为spark初学者对,一直对map与flatMap两个函数比较难以理解,这几天看了和写了不少例子,终于把它们搞清楚了 两者的区别主要在于action后得到的值 例子: import org.apac ...
- Spark中map与flatMap
map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码: def map(self, f, preservesPartitioning=False): &q ...
- 第88讲:Scala中使用For表达式实现map、flatMap、filter
今天我们来学习一下如何使用for表达式实现map.flatMap以及filter 首先,我们来看下map.map的功能是,传入一个list,通过一个函数f,将list中的元素A变成元素B的过程.最后得 ...
- Swift中的map 和 flatMap 原理及用法
之前对这两个概念有点糊,今天正好遇到一个相关需求,才深入了解了下. 需求如下: 大概就是对一个数组的model,重构成一个新model,返回得到一个新数组 用map很容易实现,不过后来我需要对其中进行 ...
- 【Android】RxJava的使用(三)转换——map、flatMap
前两篇Android RxJava的使用(一)基本用法.Android RxJava的使用(二)Action介绍了RxJava的基本用法,对Rxjava还不了解的请先看以上两篇.这篇为大家讲解RxJa ...
随机推荐
- 初学socket,c语言写的简单局域网聊天
在客户端所在的目录新建一个IP.bwj的文件,写上服务端的IP,不要带空格,保存.双方都打开一个客户端和一个服务端就可以聊天了,(可以写自己的IP,自己跟自己聊..)没有第三方服务器,服务端所在的电脑 ...
- HashMap遍历,推荐使用entrySet()
之前map遍历,偶尔会先去keyset然后再遍历keyset 比如 Map map = new HashMap(); Iterator it = map.keySet().iterator(); wh ...
- c# 把 颜色值Hex 转换为 Color
原文: http://abujj.me/archives/695 Assuming you mean the HTML type RGB codes (called Hex codes, such a ...
- navBar
改变NavgationBar的颜色: [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; 改变NavgationB ...
- Immutable Object模式
多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object模式使得我们可以在不使用锁的情况下,既保证共享变量访问的线 ...
- German Collegiate Programming Contest 2013:E
数值计算: 这种积分的计算方法很好,学习一下! 代码: #include <iostream> #include <cmath> using namespace std; ; ...
- salt-API基本验证命令
配置SALT-API,网上有很多,作下来也很顺利. 我的参考: 作一下验证的记录: curl -k https://x.x.x.x:8000/login -H "Accept: applic ...
- Delphi基本图像处理代码
//浮雕procedure Emboss(SrcBmp,DestBmp:TBitmap;AzimuthChange:integer);overload;var i, j, Gray, Azimuth ...
- Host group 信息
- jquery.lazyload的使用
1.引入 <script src="jquery.js" type="text/javascript"></script> <sc ...