Reactive - 10 - Other Operations

翻譯自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/OthersOperations

Other Operations


Description


In this section, we'll have a look at a few more useful operators that don't
fall into the broad categories we explored earlier. Reactor 3 contains a lot
of operators, so don't hesitate to have a look at the
Flux
and Mono
javadocs as well as the reference guide
to learn about more of them.

在本節(jié)中投慈,我們將了解一些更有用的運(yùn)算符尉辑,這些運(yùn)算符不屬于我們前面探討的大類(lèi)別铡俐。
Reactor 3包含很多運(yùn)算符赠幕,所以請(qǐng)毫不猶豫地查看Flux和Mono javadocs以及參考指南蚀之,以了解更多袱院。

Practice


In the first exercise we'll receive 3 Flux<String>. Their elements could
arrive with latency, yet each time the three sequences have all emitted
an element, we want to combine these 3 elements and create a new User.
This concatenate-and-transform operation is called zip:

在第一個(gè)練習(xí)中嘁字,我們將收到3個(gè)Flux<String>蒿往。它們的元素可能會(huì)延遲到達(dá),但每次這三個(gè)序列
都發(fā)出一個(gè)元素棕诵,我們希望將這三個(gè)元素結(jié)合起來(lái)裁良,創(chuàng)建一個(gè)新用戶。這種連接和轉(zhuǎn)換操作稱為zip:
    // Create a Flux of user from Flux of username, firstname and lastname.
    Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
        return Flux.zip(usernameFlux, firstnameFlux, lastnameFlux).map(tuples->new User(tuples.getT1(), tuples.getT2(), tuples.getT3()));
    }

If you have 3 possible Mono sources and you only want to keep the one that
emits its value the fastest, you can use the firstWithValue static method:

如果您有3個(gè)可能的Mono源校套,并且只想保持發(fā)射其值最快的一個(gè)价脾,那么可以使用firstWithValue靜態(tài)方法:
    // Return the mono which returns its value faster
    Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
        return Mono.firstWithValue(mono1, mono2);
    }

Flux also has the firstWithValue static method. Only the first element
emitted by each Flux is considered to select the fastest Flux (which is
then mirrored in the output):

Flux也有firstWithValue靜態(tài)方法。只考慮每個(gè)通量發(fā)射的第一個(gè)元素來(lái)選擇最快的通量(然后在輸出中鏡像):
    // Return the flux which returns the first value faster
    Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
        return Flux.firstWithValue(flux1, flux2);
    }

Sometimes you're not interested in elements of a Flux<T>. If you want to
still keep a Flux<T> type, you can use ignoreElements(). But if you really
just want the completion, represented as a Mono<Void>, you can use then() instead:

有時(shí)你對(duì)Flux<T>的元素不感興趣笛匙。如果仍要保持Flux<T>類(lèi)型侨把,可以使用ignoreElements()。
但是妹孙,如果您真的只想完成秋柄,用Mono<Void>表示,您可以使用then():
    // Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
    Mono<Void> fluxCompletion(Flux<User> flux) {
        return flux.then();
    }

Reactive Streams does not allow null values in onNext. There's an operator
that allow to just emit one value, unless it is null in which case it will
revert to an empty Mono. Can you find it?

反應(yīng)流不允許onNext中出現(xiàn)空值蠢正。有一個(gè)運(yùn)算符只允許發(fā)出一個(gè)值骇笔,除非它為null,在這種情況下嚣崭,
它將恢復(fù)為空的Mono笨触。你能找到它嗎?
    // Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
    Mono<User> nullAwareUserToMono(User user) {
        return Mono.justOrEmpty(user);
    }

Similarly, if you want to prevent the empty Mono case by falling back to a
different one, you can find an operator that does this switch:

類(lèi)似地雹舀,如果您想通過(guò)返回另一個(gè)Mono來(lái)防止出現(xiàn)空Mono情況芦劣,您可以找到一個(gè)執(zhí)行此switch的運(yùn)算符:
    // Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty
    Mono<User> emptyToSkyler(Mono<User> mono) {
        //這兩個(gè)api都可以實(shí)現(xiàn)
        return mono.defaultIfEmpty(User.SKYLER);
        //return mono.switchIfEmpty(Mono.just(User.SKYLER));
    }

Sometimes you want to capture all values emitted by Flux into separate List.
In this case you can use collectList operator that would return Mono
containing that List.

有時(shí),您希望將Flux發(fā)出的所有值捕獲到單獨(dú)的List中说榆。在這種情況下虚吟,
您可以使用collectList運(yùn)算符返回包含該List的Mono。
    // Convert the input Flux<User> to a Mono<List<User>> containing list of collected flux values
    Mono<List<User>> fluxCollection(Flux<User> flux) {
        return flux.collectList();
    }

There are more operators belonging to the collect family. You can check them
out in Flux documentation.

有更多的運(yùn)算符屬于collect系列签财。您可以在Flux文檔中查看它們稍味。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市荠卷,隨后出現(xiàn)的幾起案子模庐,更是在濱河造成了極大的恐慌,老刑警劉巖油宜,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掂碱,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡慎冤,警方通過(guò)查閱死者的電腦和手機(jī)疼燥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蚁堤,“玉大人醉者,你說(shuō)我怎么就攤上這事。” “怎么了撬即?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵立磁,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我剥槐,道長(zhǎng)唱歧,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任粒竖,我火速辦了婚禮颅崩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蕊苗。我一直安慰自己沿后,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布朽砰。 她就那樣靜靜地躺著得运,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锅移。 梳的紋絲不亂的頭發(fā)上熔掺,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音非剃,去河邊找鬼置逻。 笑死,一個(gè)胖子當(dāng)著我的面吹牛备绽,可吹牛的內(nèi)容都是我干的券坞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼肺素,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼恨锚!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起倍靡,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤猴伶,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后塌西,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體他挎,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年捡需,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了办桨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡站辉,死狀恐怖呢撞,靈堂內(nèi)的尸體忽然破棺而出损姜,到底是詐尸還是另有隱情,我是刑警寧澤殊霞,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布摧阅,位于F島的核電站,受9級(jí)特大地震影響脓鹃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜古沥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一瘸右、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧岩齿,春花似錦太颤、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至乞封,卻和暖如春做裙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肃晚。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工锚贱, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人关串。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓拧廊,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親晋修。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吧碾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容