php小編新一在解釋"串聯通量,其中第二個通量是與第一個通量的最後值即時創建的"時,可以簡潔明了地解釋這個概念。在串聯通量中,第一個通量的值會傳遞給第二個通量,而第二個通量的值則是根據第一個通量的最後值即時產生的。這種機制可以用來實現動態的資料傳遞和處理,使得程式的流程更加靈活和有效率。透過合理運用串聯通量,可以提升程式的效能和可維護性,提供更好的使用者體驗。
問題內容
我懷疑這肯定是重複的,但我只是在谷歌上搜尋了錯誤的術語。
我有兩個通量 a 和 b,但 b 只能使用 a 的最後一個值來建立。
我想創建一個本質上是 a 和 b 的串聯的通量,但 b 的創建會被推遲,直到我們獲得 a 的最後一個值。
也許,它看起來像這樣:
fluxC = fluxA.concatWith(lastA -> createFluxB(lastA))
解決方法
我不知道庫中是否有任何函數完全執行此操作。
但是,您可以透過以下方式製作這樣的運算子:
- 快取輸入流量的最新值
- 使用標準 concat 操作從快取流中取得最後一個值來建立以下序列。
注意:它不應該有太多開銷,因為該操作一次只緩存一個值,演算法的第二部分應該直接取回快取的值,而不會觸發反向來源通量.
這是一個範例實作和測試:
import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import org.junit.jupiter.api.Test; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; public class TestConcatLast { /** * Create a stream that emit all elements from input flux, * followed by all items from a flux created by provided function. * Input function is triggered using last element of source flux as input. * * @param source The flux providing elements of the first part of the concatenation. * @param createFromLastElement A function that provides the tail of the concatenation from a given element. * It will be triggered <em>at most once</em> using the last element of input flux. * @param errorIfSourceEmpty If true and input stream is empty, the returned flow will trigger an error. * If false, an empty flux is produced if input is empty. */ public <T> Flux<T> concatLast(Flux<T> source, boolean errorIfSourceEmpty, Function<T, Flux<T>> createFromLastElement) { var sourceWithLatestCached = source.cache(1); final Mono<T> deferLast = Mono.defer(errorIfSourceEmpty ? sourceWithLatestCached::last : sourceWithLatestCached::next); return sourceWithLatestCached.concatWith( deferLast.flatMapMany(createFromLastElement) ); } @Test public void testConcat() { var nextExpectedElement = new AtomicInteger(1); var elts = Flux.just(1, 2, 3, 4) // Check cache works and no element has been fetched back from source .doOnNext(i -> { assert nextExpectedElement.compareAndSet(i, i+1); }); var concatenated = concatLast(elts, true, i -> Flux.just(i + 1, i + 2, i + 3)); StepVerifier.create(concatenated) .expectNext(1, 2, 3, 4, 5, 6, 7) .verifyComplete(); } }
以上是串聯通量,其中第二個通量是與第一個通量的最後值即時創建的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!
陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前ByDDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前ByDDD
Inzoi:如何申請學校和大學
3 週前ByDDD
如何修復KB5055518無法在Windows 10中安裝?
2 週前ByDDD
Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具