[RxJS] Handling a Complete Stream with Reduce
When a stream has completed, you often need to evaluate everything that has happened while the stream was running. This lesson covers how to use reduce to collect values and total up a “score” of this simple game.
const Observable = Rx.Observable;
const startButton = document.querySelector('#start');
const halfButton = document.querySelector('#half');
const quarterButton = document.querySelector('#quarter');
const stopButton = document.querySelector('#stop');
const resetButton = document.querySelector('#reset');
const input = document.querySelector('#input');
const start$ = Observable.fromEvent(startButton, 'click');
const half$ = Observable.fromEvent(halfButton, 'click');
const quarter$ = Observable.fromEvent(quarterButton, 'click');
const stop$ = Observable.fromEvent(stopButton, 'click');
const reset$ = Observable.fromEvent(resetButton, 'click');
const input$ = Observable.fromEvent(input, 'input')
.map(event => event.target.value);
const data = {count:};
const inc = (acc)=> ({count: acc.count + });
const reset = (acc)=> data;
const starters$ = Observable.merge(
start$.mapTo(),
half$.mapTo(),
quarter$.mapTo()
);
const intervalActions = (time)=> Observable.merge(
Observable.interval(time)
.takeUntil(stop$).mapTo(inc),
reset$.mapTo(reset)
);
const timer$ = starters$
.switchMap(intervalActions)
.startWith(data)
.scan((acc, curr)=> curr(acc))
Observable.combineLatest(
timer$,
input$,
(timer, input)=> ({count: timer.count, text: input})
)
.takeWhile((data)=> data.count <= )
.filter((data)=> data.count === parseInt(data.text))
.reduce((acc, curr)=> acc + , ) //count how many times it go thought filter
.subscribe(
(x)=> console.log(x),
err=> console.log(err),
()=> console.log('complete')
);
[RxJS] Handling a Complete Stream with Reduce的更多相关文章
- 【Java 8】Stream通过reduce()方法合并流为一条数据示例
在本页中,我们将提供 Java 8 Stream reduce()示例. Stream reduce()对流的元素执行缩减.它使用恒等式和累加器函数进行归约. 在并行处理中,我们可以将合并器函数作为附 ...
- java 数据类型:集合接口Collection之 Stream 的reduce方法
Stream 的reduce递归计算 import java.util.ArrayList; import java.util.Arrays; import java.util.List; impor ...
- [RxJS] Resubscribing to a Stream with Repeat
When you complete a stream, there’s no way to restart it, you must resubscribe. This lesson shows ho ...
- Stream中reduce()使用记录
一.reduce()使用1.第一个参数是我们给出的初值,2.第二个参数是累加器,可以自己用实现接口完成想要的操作,这里使用Bigdecimal的add方法 3.最后reduce会返回计算后的结果 Bi ...
- [RxJS] Observables can complete
The Observer object has the functions next() and error(). In this lesson we will see the other (and ...
- [RxJS] Handling Multiple Streams with Merge
You often need to handle multiple user interactions set to different streams. This lesson shows hows ...
- [五]java函数式编程归约reduce概念原理 stream reduce方法详解 reduce三个参数的reduce方法如何使用
reduce-归约 看下词典翻译: 好的命名是自解释的 reduce的方法取得就是其中归纳的含义 java8 流相关的操作中,我们把它理解 "累加器",之所以加引号是因为他并不仅仅 ...
- RxJS入门
一.RxJS是什么? 官方文档使用了一句话总结RxJS: Think of RxJS as Lodash for events.那么Lodash主要解决了什么问题?Lodash主要集成了一系列关于数组 ...
- Upgrading to Java 8——第四章 The Stream API
在这章中我们将学习Stream API,在JDK 8 中的一项新的特性.为了理解这一章的主题,你需要知道如何使用Lambda表达式和java.util.function里的预定义的函数式接口. 一个S ...
随机推荐
- 模块计算机类型“X64”与目标计算机类型“x86”冲突
问题描述:在X64 平台上开发dll 文件,在生成dll时Vs 2010 出现如下错误 :"fatal error LNK1112: 模块计算机类型"X64"与目标计算机 ...
- Android Log日志文件的分析、查看
Log 在android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 什么时候会产生log文件呢 ?一般 ...
- Fiddler使用笔记
http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#basic 1.filter的使用,跟踪某个网站的访问,例如:hr. ...
- iOS_SN_UITableView的优化
1.提前计算并缓存好高度(布局),因为heightForRowAtIndexPath:是调用最频繁的方法. 2.异步绘制,遇到复杂界面,遇到性能瓶颈时,可能就是突破口. 3.滑动时按需加载,这个在大量 ...
- linux int to string 方法
最近从windows 移植程序的时候发现to_string()函数在linux 中不能用,网上找了几种方法,觉得使用stringstream对象来实现类型转化比较好一点. 你只需在你工程中加入下面的t ...
- 机器学习理论与实战(十)K均值聚类和二分K均值聚类
接下来就要说下无监督机器学习方法,所谓无监督机器学习前面也说过,就是没有标签的情况,对样本数据进行聚类分析.关联性分析等.主要包括K均值聚类(K-means clustering)和关联分析,这两大类 ...
- mybatis入门,基本案例和xml讲解
mybatis入门 先举一个入门案例 1)创建一个mybatis-day01这么一个javaweb工程或java工程 2)导入mybatis和mysql/oracle的jar包到/WEB-INF/li ...
- kafka安装及Kafka-PHP扩展的使用
话说用了就要有点产出,要不然过段时间又忘了,所以在这里就记录一下试用Kafka的安装过程和php扩展的试用. 实话说,如果用于队列的话,跟PHP比较配的,还是Redis.用的顺手,呵呵,只是Redis ...
- 2016ICPC China-finals 题解
A:ans=n/3,因为8=1(mod7) B: C: D:二分+贪心,二分答案,即个数,check(mid)时贪心看能不能放成mid个; E:贪心,列出不等关系,然后写个高精度分数类; F:二分+h ...
- BZOJ 2732 射箭
http://www.lydsy.com/JudgeOnline/problem.php?id=2732 题意:给你n个靶子,让你求是否有一个经过原点的抛物线经过最多的前k个靶子,求出最大的k 思路: ...