前言

前几篇介绍过了

Creation Operators

Filtering Operators

Join Creation Operators

Error Handling Operators

Transformation Operators

Join Operators

Utility Operators

Conditional and Boolean Operators

这篇继续介绍 Mathematical and Aggregate Operators

参考

Docs – Mathematical and Aggregate Operators

count

complete 以后, 统计之前总共发布了几次

const subject = new Subject<number>();
subject.pipe(count()).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.complete();

效果

如果一次都没有, 也会 next value 0 哦

max & min

complete 后, 找出之前发布过最大/最小的值

const subject = new Subject<{ age: number }>();
subject.pipe(max(person => person.age)).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.next({ age: 50 });
subject.next({ age: 72 });
subject.next({ age: 40 });
subject.complete();

效果

这里我只给 max 的例子, min 就是找最小.

reduce

reduce = scan + last

在 complete 之后, 它才去跑 JS 的 Array.reduce

const subject = new Subject<number>();
subject.pipe(reduce((acc, value) => acc + value, 0)).subscribe(v => console.log('next', v));
subject.next(1);
subject.next(2);
subject.next(3);
subject.complete();

效果

用 scan + last 来实现

.pipe(
scan((acc, value) => acc + value, 0),
last()
)

一句话总结

count : complete 后统计之前总共发布几次

max & min : complete 后, 找出之前发布过最大/最小值

reduce : 等价于 scan + last. scan 每一次都会发布, reduce 只在 complete 后才发布.

RxJS 系列 – Mathematical and Aggregate Operators的更多相关文章

  1. RxSwift 系列(六) -- Mathematical and Aggregate Operators

    前言 本篇文章将要学习RxSwift中数学和集合操作符,在RxSwift中包括了: toArray reduce concat toArray 将一个Observable序列转化为一个数组,并转换为一 ...

  2. RxSwift 系列(八) -- Error Handing Operators

    前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...

  3. rxjs笔记(未完成)

    首先是 Observable 和promise的区别, 1返回值个数,Observable 可以返回0到无数个值. 2.Promise主动推送,控制着"值"何时被 "推送 ...

  4. ReactiveX Operators

    This documentation groups information about the various operators and examples of their usage into t ...

  5. Rxjava, RxAndroid, Retrofit 等库的使用

    RxJava的基本用法: 关于 unSubscribe() 的调用问题: There is no need to unsubscribe in onCompleted. Take a look at  ...

  6. RxSwift 之官方文档

    RxSwift 官方文档结构 Introduction: Subjects Transforming Observables Filtering Observables Combining Obser ...

  7. ReactiveX 学习笔记(7)聚合操作符

    Mathematical and Aggregate Operators 本文的主题为处理 Observable 的聚合操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操 ...

  8. Rx = Observables(响应) + LINQ(声明式语言) + Schedulers(异步)

    Reactive = Observables(响应)+ Schedulers(异步). Extensions = LINQ(语言集成查询) LINQ: The Operators of Reactiv ...

  9. RxJava中的doOnSubscribe默认运行线程分析

    假设你对RxJava1.x还不是了解,能够參考以下文章. 1. RxJava使用介绍 [视频教程] 2. RxJava操作符   • Creating Observables(Observable的创 ...

  10. RxJava 2.x 理解-2

    操作符总结: http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki Ope ...

随机推荐

  1. [oeasy]python0094_视频游戏_双人网球_pong_atari_mos_6502_雅达利_米洛华

    编码进化 回忆上次内容 上次 我们回顾了 微软之前的 比尔盖茨和保罗艾伦 mits 迎来的 是帮手 还是隐患? intel-8080 遇到了 mos-6502 底层硬件 驱动 游戏行业进化 不光是扑克 ...

  2. oeasy 教您玩转 linux 010207 黑客帝国 matrix

    我们来回顾一下 上一部分我们都讲了什么? 蒸汽机车sl 变身小机车-l 变身飞天机车-F 让我们再开一次车 sl 上次还想看看黑客帝国来着?! 黑客帝国Matrix apt search matrix ...

  3. oeasy教您玩转linux 010216 随机诗词 fortunezh

    我们来回顾一下 上一部分我们都讲了什么? 下载fortune 输出重定向到cowsay 多重输出重定向 fortune的细节 有没有中️文的fortune呢 # 搜索一下fortune apt sea ...

  4. Prometheus 基于Python Django实现Prometheus Exporter

    基于Python Django实现Prometheus Exporter 需求描述 运行监控需求,需要采集Nginx 每个URL请求的相关信息,涉及两个指标:一分钟内平均响应时间,调用次数,并且为每个 ...

  5. Django template层之json报文遍历总结

    Django template层之json报文遍历总结 by:授客 QQ:1033553122 测试环境 Win7 Django 1.11 实例 Views.py def home(request): ...

  6. pyspark初步了解

    spark的运行角色: 分布式代码的流程分析 pythononspark原理

  7. 新项目加入mybatisplus,我给自己挖了个坑 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 上述问题的解决办法:1首先看看@mapp ...

  8. 【SpringCloud】Nacos集群部署(Centos平台)

    一.前提环境准备 Nacos 下载 https://github.com/alibaba/nacos/releases 或者使用其它博主备份的 https://blog.csdn.net/weixin ...

  9. 【Java】【常用类】LocalDateTime 当前日期时间类 相关

    LocalDate主要的三个API类: java.time.LocalDate; java.time.LocalDateTime; java.time.LocalTime; LocatDate对象获取 ...

  10. 【Java】【常用类】Calendar 日历类

    Calendar 日历类,我居然念错发音,来,好好看下音标  ['kælɪndə]  卡琳达 public class DateTest { public static void main(Strin ...