[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 ...
随机推荐
- mysql备份sql,脚本
MySQL 安装位置:/usr/local/mysq 论坛数据库名称为:bbs MySQL root 密码:123456 数据库备份目的地:/var/db_backup/ #! /bin/bash / ...
- 那些年被我坑过的Python——邂逅与初识(第一章)
第一问:为什么学习Python? 虚妖说:为了还债,还技术债,很早接触编程,却一直徘徊,也码了很多代码,却从未真真学会编程! 第二问:什么是Python 是一种以简洁.优雅著称的解释型.动态.强类型的 ...
- bzoj 1053: [HAOI2007]反素数ant 搜索
1053: [HAOI2007]反素数ant Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1497 Solved: 821[Submit][Sta ...
- Solr总结
http://www.cnblogs.com/guozk/p/3498831.html Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注 ...
- mysql 字段存储类型
摘自:http://zuo.ai.xiao.blog.163.com/blog/static/6079155320121293750732/ 1.数字类型 ...
- Android学习之 UI效果
探究Android的多分辨率支持以及各种类型图标尺寸大小 - CSDN 各种数字提醒控件-Android 代码仓库-eoe Android ViewBadger - 开源中国社区 Android 微信 ...
- 【CF】556D A Lot of Games
构建trie树,可以得到4类结点:必胜点,必负点,完全主宰点(可胜可负),完全无法主宰点(无法控制最终胜负).递归到叶子结点,即为必胜点,回溯分情况讨论.注意叶子结点使用属性n来控制,n表示当前结点的 ...
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- 【转】模拟器上安装googleplay apk
原文网址:http://blog.sina.com.cn/s/blog_9fc2ff230101gv57.html 1.进入到sdk\android-sdk-windows\tools>目录下: ...
- Rails 看起来很不错哦。
最新在工作中遇上了ruby,确切的说是rails. 其实我的工作是一个渗透测试工程师(其实就是拿着一堆黑客工具扫描的活). 而我不怎么了解ruby on rails.但是客户即将上线的商城系统是用 ...