Working With MathFlux – 使用 MathFlux

最后修改: 2023年 11月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Reactive programming has introduced a new age of applications that are both responsive and scalable. Project Reactor is a superior toolkit for managing asynchronous and event-driven programming within this ecosystem.

Spring 反应式编程引入了一个既能响应又能扩展的应用程序新时代。项目Reactor是在此生态系统中管理异步和事件驱动编程的卓越工具包。

MathFlux is a component of Project Reactor, which provides us with a variety of mathematical functions designed for reactive programming.

MathFlux 是 Project Reactor 的一个组件,它为我们提供了为反应式编程设计的各种数学函数。

In this tutorial, we’ll explore the MathFlux module from Project Reactor and understand how to utilize it to execute various mathematical operations on reactive streams.

在本教程中,我们将探索 Project Reactor 的 MathFlux 模块,并了解如何利用它在反应流上执行各种数学运算。

2. Maven Dependency

2.Maven 依赖性

Let’s create a Spring Boot project in our IDE and add the reactor-core and reactor-extra dependencies to the pom.xml file:

让我们在 IDE 中创建一个 Spring Boot 项目,并在 pom.xml 文件中添加 reactor-corereactor-extra 依赖项:

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.6.0</version>
</dependency>

<dependency>
    <groupId>io.projectreactor.addons</groupId>
    <artifactId>reactor-extra</artifactId>
    <version>3.6.0</version>
</dependency>

Additionally, we require the inclusion of reactor-test to effectively test our code:

此外,我们要求包含 reactor-test 以有效测试我们的代码:

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-test</artifactId>
    <version>3.6.0</version>
    <scope>test</scope>
</dependency>

3. Using MathFlux‘s Basic Math Functions

3.使用 MathFlux基本数学函数

Most of the functions in MathFlux require the input of cardinality greater than one and produce an output of cardinality one.

MathFlux 中的大多数函数都要求输入的单词数大于 1,并产生单词数为 1 的输出。

These functions typically accept a Flux as input and return a Mono as output.

这些函数通常接受 Flux 作为输入,并返回 Mono 作为输出。

The reactor.math package includes a static class named MathFlux, a specialized version of Flux that contains mathematical operators such as max(), min(), sumInt(), and averageDouble().

reactor.math 包包含一个名为 MathFlux 的静态类,它是 Flux 的专门版本,包含数学运算符,如 max()min()sumInt() averageDouble()

It’s possible to perform mathematical operations using the MathFlux class by calling its associated methods.

通过调用 MathFlux 类的相关方法,可以使用 MathFlux 类执行数学运算。

Let’s explore MathFlux basic math functions in detail.

让我们详细了解 MathFlux 的基本数学函数。

3.1. Sum

3.1.总和

The sumInt() method calculates the sum of elements in a Flux of integers. It simplifies adding up numerical values in reactive streams.

sumInt() 方法计算整数 Flux 中元素的总和。它简化了反应流中数值的累加。

We’ll now create a unit test for the method sumInt(). We’ll use StepVerifier for testing our code.

现在,我们将为方法 sumInt() 创建一个单元测试。我们将使用 StepVerifier 测试代码。

This unit test ensures that the sumInt() method accurately calculates the sum of elements in the given Flux, and validates the correctness of the implementation:

该单元测试确保 sumInt() 方法准确计算给定 Flux 中元素的总和,并验证实现的正确性:

@Test
void givenFluxOfNumbers_whenCalculatingSum_thenExpectCorrectResult() {
    Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);
    Mono<Integer> sumMono = MathFlux.sumInt(numbers);
    StepVerifier.create(sumMono)
      .expectNext(15)
      .verifyComplete();
}

We first create a Flux of integers that represents a dataset. This Flux is then passed as a parameter to the sumInt() method.

我们首先创建一个表示数据集的整数 Flux 。然后将此 Flux 作为参数传递给 sumInt() 方法。

3.2. Average

3.2.平均值

The averageDouble() method calculates the average of elements in a Flux of integers, making it helpful in computing the mean value of an input.

averageDouble() 方法计算整数 Flux 中元素的平均值,因此有助于计算输入的平均值。

This unit test calculates the average of integers 1 to 5 and compares it to the expected result of 3:

该单元测试计算 15 整数的平均值,并将其与预期结果 3 进行比较:

@Test
void givenFluxOfNumbers_whenCalculatingAverage_thenExpectCorrectResult() {
    Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);
    Mono<Double> averageMono = MathFlux.averageDouble(numbers);
    StepVerifier.create(averageMono)
      .expectNext(3.0)
      .verifyComplete();
}

3.3. Min

3.3 最小

The min() method determines the smallest value among a Flux of integers.

min() 方法确定整数 Flux 中的最小值。

This unit test is designed to verify the functionality of the min() method:

该单元测试旨在验证 min() 方法的功能:

@Test
void givenFluxOfNumbers_whenFindingMinElement_thenExpectCorrectResult() {
    Flux<Integer> numbers = Flux.just(3, 1, 5, 2, 4);
    Mono<Integer> minMono = MathFlux.min(numbers);
    StepVerifier.create(minMono)
      .expectNext(1)
      .verifyComplete();
}

3.4. Max

3.4 最大值

We can use the max() function from MathFlux to find the highest element. The output is encapsulated in a Mono<Integer> which represents a reactive stream that emits a single Integer result.

我们可以使用 MathFlux 中的 max() 函数来查找最高元素。输出被封装在一个 Mono<Integer> 中,它代表一个反应流,发出一个 Integer 结果。

This unit test verifies the correct identification of the maximum integer in a Flux:

该单元测试验证是否正确识别了 Flux 中的最大整数:

@Test
void givenFluxOfNumbers_whenFindingMaxElement_thenExpectCorrectResult() {
    Flux<Integer> numbers = Flux.just(3, 1, 5, 2, 4);
    Mono<Integer> maxMono = MathFlux.max(numbers);
    StepVerifier.create(maxMono)
      .expectNext(5)
      .verifyComplete();
}

The given Flux in this unit test contains 3, 1, 5, 2, and 4. The purpose of the max() method is to identify the maximum element, which in this case is 5.

本单元测试中给定的 Flux 包含 3152,4max() 方法的目的是识别最大元素,本例中的最大元素是 5

4. Conclusion

4.结论

In this article, we discussed the use of MathFlux in Spring Reactive programming. By utilizing its capabilities, we can simplify complex mathematical tasks within our reactive applications. We saw that MathFlux enables us to seamlessly manage complex data processing, making Spring Reactive applications more intuitive and robust.

在本文中,我们讨论了在 Spring Reactive 编程中使用 MathFlux 的问题。利用它的功能,我们可以简化反应式应用程序中复杂的数学任务。我们看到,MathFlux 能让我们无缝地管理复杂的数据处理,使 Spring Reactive 应用程序更直观、更健壮。

As always, the source code for this tutorial is available over on GitHub.

一如既往,本教程的源代码可在 GitHub 上获取。