前言

前几篇介绍过了

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. 全网最适合入门的面向对象编程教程:17 类和对象的Python实现-鸭子类型与“file-like object“

    全网最适合入门的面向对象编程教程:17 类和对象的 Python 实现-鸭子类型与"file-like object" 摘要: 本文主要介绍了 Python 中创建自定义类时鸭子类 ...

  2. webpack4.15.1 学习笔记(一) — 基本概念

    目录 入口(entry) 出口(output) 加载器 Loaders 插件 Plugins 模式 webpack.config.js 配置 终终终终于下定决心,对你下手了,系统的学习一下. webp ...

  3. Mysql函数1-IFNULL

    IFNULL函数用于判断参数值是null时则返回指定内容. 原本 select goods_base_name,goods_id from goods where goods_id in (6,7,8 ...

  4. SSH Exporter:基于Prometheus的远程系统性能监控神器

    SSH Exporter English | 中文 介绍 SSH Exporter 是一个基于 Prometheus 规范的监控工具,通过 SSH 协议远程收集目标服务器的系统性能数据,如 CPU 使 ...

  5. 【DataBase】MySQL 03 基本命令 & 语法规范

    参考至视频:P12 - P15 https://www.bilibili.com/video/BV1xW411u7ax?p=82 SHOW 和相关的基本命令 查看所有的数据库 SHOW DATABAS ...

  6. 【Scala】03 函数

    1.Scala的方法语法: object Hello { def main(args : Array[String]) : Unit = { // scala 允许在方法的声明中再声明方法,并且调用 ...

  7. Jax框架:通过显存分析判断操作是否进行jit编译

    相关: https://jax.readthedocs.io/en/latest/device_memory_profiling.html 代码: import jax import jax.nump ...

  8. 【转载】解决Nvidia-smi没有进程但是显存不释放的问题

    ----------------版权声明:本文为CSDN博主「打拳的土豆」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn ...

  9. 记录实验室深度学习服务器显卡硬件故障的排查——RmInitAdapter failed! rm_init_adapter failed

    实验室突然通知我说是深度学习的服务器无法查看GPU,并且在GPU上运行的程序也halt on,需要解决.于是查询服务器的运行日志得到下面的信息: Nov 10 01:33:23 dell kernel ...

  10. [USACO22FEB] Paint by Rectangles P 题解

    前言 没用线段树的小常数.小短码. 题目链接:洛谷. 题意简述 给出 \(n\) 个平行于坐标轴的矩形,各边所在直线互不重合,钦定最外面为白色,对这个平面图黑白染色,分别求黑色块数和白色块数. 题目分 ...