[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 ...
随机推荐
- __unset()魔术方法 删除类内私有属性
__unset()魔术方法 删除私有属性 unset()对共有属性进行删除 可通过__unset()魔术方法对私有属性进行操作 当在类外部执行unset()函数时,自动执行类内__unset()魔术方 ...
- php版的求表达式的真值表-TrueValueTable
贴上代码: <?php error_reporting(E_ALL & ~E_NOTICE); $expression=$_GET['TrueTable']; //读取输入框数据 if( ...
- jQuery实现Twitter的自动文字补齐特效
上图效果可以使用jQuery插件Typeahead.js来实现,这款jQuery插件来自于Twitter的一个新的项目,支持远程和本地的数据集.比较有特色的地方在于你可以将数据集使用本地存储(loca ...
- bzoj 1053: [HAOI2007]反素数ant 搜索
1053: [HAOI2007]反素数ant Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1497 Solved: 821[Submit][Sta ...
- Java cookie的使用
1.cookie是什么? cookie是web应用当中非常常用的一种技术,用于储存某些特定的用户信息. 2.cookie的作用? 在用户登陆时将用户的信息存放在cookie中,用户在一定的时间中再次登 ...
- Python安装及开发环境配置
Python的语法简洁,功能强大,有大量的第三方开发包(模块).同时Python不像java一样对内存要求非常高,适合做一些经常性的任务方面的编程.根据codeeval网站数据统计显示,连续三年,Py ...
- 几个字符串的误区,以及setlocale函数的使用
转自 http://www.blogjava.net/baicker/archive/2007/08/09/135642.html 转自 http://witmax.cn/character-enco ...
- 14.6.3.5 Configuring InnoDB Buffer Pool Flushing
14.6.3.5 Configuring InnoDB Buffer Pool Flushing InnoDB 执行某些任务在后台, 包括脏叶的刷新(那些已经发生改变的pages 但是没有写入到数据文 ...
- GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名(转载)
转载自:http://www.cnblogs.com/cy163/archive/2009/03/12/1409434.html 1.gcc包含的c/c++编译器gcc,cc,c++,g++,gcc和 ...
- 【异步编程】when.js
异步编程:When.js快速上手 var api = 'http://qgy18.imququ.com/file/when/d.php?cb=?'; var getData = function() ...