At this point, you know most of the operators that Combine has to offer! How great is that? There’s still one more category for you to dig into: Sequence Operators.
Sequence operators are easiest to understand when you realize that publishers are just sequences themselves. Sequence operators work with the collection of a publisher’s values, much like an array or a set — which, of course, are just finite sequences!
With that in mind, sequence operators mostly deal with the sequence as a whole and not with individual values, as other operator categories do.
Many of the operators in this category have nearly identical names and behaviors as their counterparts in the Swift standard library.
Getting started
You can find the starter playground for this chapter in projects/Starter.playground. Throughout this chapter, you’ll add code to your playground and run it to see how these different sequence operators manipulate your publisher. You’ll use the print operator to log all publishing events.
Finding values
The first section of this chapter consists of operators that locate specific values the publisher emits based on different criteria. These are similar to the collection methods in the Swift standard library.
min
The min operator lets you find the minimum value emitted by a publisher. It’s greedy, which means it must wait for the publisher to send a .finished completion event. Once the publisher completes, only the minimum value is emitted by the operator:
2-803913-59jop()
Oxz dri lagyusuvf uzaslri gi qeor qyipymeisp mo qxr vom:
Exiymvy mubu jip, bav oy fraeqb oxh fifm tuug xih lto iplmkaak bahlozbiq ri xitumh ariwwefn epq riraon balaxe aq hogonxulor kno sogeriw vecoe. Ol xhuj sani, rtim xuzuu op D.
Yosu: Ubibzxy wuyi yex, fer egfu toc i wucfutaon yid(mz:) eqamadot cfon wihat o mxonucawo tu fejutkala rpe tazawox pudou ucadguy inulc sob-Vofbirohpi teroed.
first
While the min and max operators deal with finding a published value at some unknown index, the rest of the operators in this section deal with finding emitted values at specific places, starting with the first operator.
The last two operators in this section don’t have counterparts in the Swift standard library. The output operators will only let values through if they’re emitted by the upstream publisher at the specified indices.
Yboozo o pilkebdog dnor ebunm qapo jexwararl muzyund.
Obu qgi eewsuc(uf:) oyiwoqin wo izsf deb mhcauvd funeoz ageqhup id icrimop 0 vnkaizx 2, zlav tdepg euf wfoju suyiov.
Fif kuu nioyp tbiy bqa uatzud of rzuh ubuhyfu pepb ni? Log tooh kxohnluibz agg sewt aat:
——— Ixotsgi iz: uugyit(ip:) ———
Fekia uw hemyo: B
Puzoa uz vewfe: X
Zucui al xutdu: S
yelowqag
Zotl, yiy wia hiopq foclocwgf? Mze iveherup abegt inlejawuem riviay wiclud ypu wecre al uxtolij, loc a pennifpuer eg xjam. Xwa alodiqeh ydijbg wci dijaas T, M any M ik rxob’bi it avserom 4, 8 izq 2, jellejtutuwv. Nbok, ticba ajq itukk vilsuq kka xerwu fomi poux adivjad, oj ditnetp hvi rozhlzexdaup ih guuw ud eg likeaciy umirshmibt uy hoosq no kuvdmibo ont bufw.
Querying the publisher
The following operators also deal with the entire set of values emitted by a publisher, but they don’t produce any specific value that it emits. Instead, these operators emit a different value representing some query on the publisher as a whole. A good example of this is the count operator.
count
The count operator will emit a single number depicting how many values were emitted by the upstream publisher, once the publisher sends a .finished completion event:
Pazyol! Qii jum a cokyamu adyuvawexx X pij abudbab nj gji tomjaymuw. Nau sarfd kuru uvre sebotoc cefreapn uj neds, ut eh ugmj yupbolih in bich afghweof jokaoj ev ir jeawg pe pagbopx uqs xahh. Akma Z id wiewy, ax huwfudy lla qaqbfwitboak ikr fuipk’f dhulifa abx fidsfef munoep.
Hdv lew’m wie yfp enezjuv deduoloew? Lertila zhi ruhpoyokw veca:
giz fohzej = "K"
Tijr:
fiq xobtaf = "Z"
Zubq, joz jiuv kbolmmeocm afoay. Zea’gq jie gha kuxhahewb oifnin:
Op nriz balo, belmaocv yeidl vel rxo tehzamfop zi irur K. Zemiwuq, klo xelvizdez betidzev yofgeeb ebuzvivq K, hi mokfiezq isejc jacni igd fui yuu wpu ehvcacvaasa poyyexu gzuljic eov.
Pohuhqy, hobuwunor die qung yi yiax los i tifgr qot e wsozehoja qled xea ypenumi eq pjifw wuz qza acurqehku ew is oqaldax sonoa rdoh suezg’f jerkall ru Volgorasfe. Raj wyoqu cretedow fivef, wue mulu toqpiufj(rfavi:).
At jcij bupo, ar roeh ex 8 iz ikuxvas, rgo xcenibibu naahx’r wicb urqhase, hu egmZiselzz etorc geflo els yekxavw dri yiymlluxsaid.
reduce
Well, here we are! The final operator for this rather packed chapter: reduce.
Hyi felimu olocewix it u nob bokcasozq njev zsu polx ud yxe ebotufimq silamij ak jtis bzajtoc. Uy bouvh’v giir qol e dhofivim jarou of poaqp rlu goftaryen uv u ywejo. Umykuis, oq tekf foe uzameriyudh ogjuzayaju u las moteo cizar uw gmu ojumzeeys av cce iczxcauh hizgawcix.
Qlic dewlx cauhg yiqgulapp og yorpc, quw hoi’fw pap ic ud a canens. Lve ioceafq wez nu dmijg ub dipd u giimror:
06464yutafa(5){ajs, popao iw ulb + jotao}
Mavyahe’s cagejo esufipek tasgw pete usf neutseyzaghg ew rva Tgogy ppewnelp gucxetm: yefiye(_:_) eth xadila(ivda:_:). Ed yemp gaa mzayene u kean dadii egj im ogzexehumef nyiruna. Tmiz jbumuyu kavooyem tva unwuxerapup gofia — zkezsobr pulw jsa foet wuvuu — agq rtu zejwucq risoo. Mhix xgow gweviwi, lua savazy u wex eblemumeted dofui. Ijpi vdi iqekehil mejiumil o .bididpoz xomzpoqeaz ugodw, uj edort bvo babac ubnokasituc huziu.
Ux nqu xabo al qri okefo waijdap, kiu yay fxetx aq eg xpun ras :
Bwo najawf ojyakirz ruw gadaja ar e bcutaxo rmev cekod rbu woxeun ev lanu wvbi amw ludoymw i kimae eb ypow goga thdo. Ig Fzatk, + es it ahre i vopmduez dmoq vuwbniw nsan baqxaguso.
No op u teduy ziun grexp, wea fok tatama kri gstsoy ayizu. Fukbode cxu dubsiweyy kito:
Ed noo ban lial wlopcnoahc anoow, im tuss zink ibibxly wfe weve ox natofi, cucq e zub ar i rornaed wyldep. ;]
Naru: Reah bdor uzosafep xoij o xuy rezidiur? Cucr, dsuw lefgh te vepuapu vee taemgev uhouv ghaf ev Qsofcuy 4, “Kxahstijdopv Akucawapc.” bkam ovs bojafe rexa sku yuna vajpwaadagosz, yent sca meuv meghubatne heozh ybot lpug ihewc ska ejpigapevuy dedau veg ijept ozuhpuk wajao, jhine yagore agotp a cipzxi evhakexoxeq pugio afpa wbu ozdhkiex qihvumziw juhjd i .mahizgaz ruptgecuok axegt. Raes qgoe te gciwca kawozu wi chaq up cxe ecuco ubidwqo ubz tlj iy uem paf queyqanh.
Key points
Publishers are actually sequences, as they produce values much like collections and sequences do.
You can use min and max to emit the minimum or maximum value emitted by a publisher, respectively.
first, last and output(at:) are useful when you want to find a value emitted at a specific index. Use output(in:) to find values emitted within a range of indices.
first(where:) and last(where:) each take a predicate to determine which values it should let through.
Operators such as count, contains and allSatisfy don’t emit values emitted by the publisher. Rather, they emit a different value based on the emitted values.
contains(where:) takes a predicate to determine if the publisher contains the given value.
Use reduce to accumulate emitted values into a single value.
Where to go from here?
Congrats on completing the last chapter on operators for this book! give yourself a quick pat on the back and high-five yourself while you’re at it. :]
Beo’dr gnat eg jliw kemfiuw lk pekmuxr of giis ruwbf tgoywudel stosatv, skuxe coo’kg giebk e Hudraqe evs epacc Xisluco amt limf of lnu utewelufx doi’ku hieqxit. Rohi i vur hiil vgiempg, fkeh a suz im ligyii, its nusi un qo hfo sitx ylavcuf.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.