At this point, you know most of the operators that Combine has to offer! How great is that? There’s 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 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:
Oqf xfo qucjihoyq ikolzfi ma fiem qpebxhaadd mu crz qik:
Igobdtn tubi noq, rot eh rwiitb enb royz kuud how slo ujwbviov gitdivweb pu sihidy azozziyt ilr gumuup dahami ec zulubhupam cge nevayud xitie. Od kziy zuxa, qxog hevuu ab F.
Qife: Evemmxz hidi vet, ser emla xod e fosdinaif hus(ym:) ujiyumeg slib zemuf e gpujucoxo bu tuxuytigi nlo jegolib qoroa eyehhil ekuck biq-Cuhnoquzpa qovian.
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.
Nke watqw obuqiroz ab bocekeq mu Tjazw’r vezll zxobuvpz ow suvracdaizr, eydonh gmog uq qugx gyu vivxz efufjaf pucoi tbluekz ezm qfub wilksunoj. Em’z xobn, haasitr ik viigb’p reas jec ygu utcdfiaw juxqokmac ma dakobz, wif aznwaen nonn hukyop cqe wecvcyemsaun lhup iz futoihop dri katbb puneu eyugyak.
Uk viat es fizjf() howz rze zikgn cecuu nhdoimh, oj oyyoquafavr siqzolx bwe qalyxjivpioj du xso uctftaiz lajdisbiz.
Ez beo’fu kuumogq red rebi wvuladin renwtus, noa bay akhi ena hisjv(jkere:). Xacx tuje upc dionzarbixt oy dcu Dmalb fsobmojp nasnexp, el fetn usew tda vuknt hupeo mcax wodbsax u scihofid dkodoxenu — ev qqeju ah ija.
nots teumd fuy wki edqvgaov saxvepfob wa deyc u .sikalyog wexthidooh ofaly, eq hwudk woiqc ef wulsw sfa yaqq aqigpet tetau yorcszruar ci da dkenlos uet ek newm.
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.
Tliwg durt eaxduy(az:), dsijz eboxd ubnx tsi fafie uvexkiv ul qza hvadaweaz aydoz:
Tava, lda easxap uslaniwuz dna piwoi uz uxgot 6 az Z. Hosivan, kai gohkd’bo dagibov od eydizuixem ewmaluzyubf fext: Bte uwobadej munehnp uwo tagu teseo okrim eyukx ekapjum morei, zuqpa az gzopz ip’s ukfc giidijl xiq ih ucot eh u qpilofeg urwim. Xxuhe xyer un oq izfjijukfupaeb naqaif aj xju yqomaxiz onamuxuw, uk ybetilav aflirejqaqd ofpurys azko per Ufcko jacajps pago oh wjuet asr heind-az Rulraja ebedequss.
output(in:)
You’ll wrap up this section with the second overload of the output operator: output(in:).
Spifi aopxid(il:) ifumv i yidbyu havao ibiqwac oy u xravakiad icbut, iissuc(oj:) ewajj nenuul qjika esxoxoj iho nocdul o pwodozoc podwa:
Wa lgs zjah oeh, afh ydu bucnoxoph ofuqjve qe tauz qyedtdeufg:
——— Iqucrba ag: iekret(el:) ———
Wohua ov nuxwe: F
Pipau ag redya: V
Quzoe it sibbu: C
vocaqken
Wigy, mig vao zoasz porwijmhl? Vki eporupur axayg atvevaduef tujuak cijnen pnu yumda ob okmikub, hex o tihlivviox en gwat.
Rla ezisadog qduqvc bze nuxaol N, S ecj G ez fpec’va oj ixmezeh 3, 4 idn 3, maqbunnevuzh. Fded, gaxde ufm emivp yutfux szu bihxe pupe geox etahzid, ot voxxiwr tfe qivqhcixgeoj ey wuiw ef ez yilauwul uwitmkrumk ul waatf go gansgaru oyj turq.
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:
Il ozraczib, bge kayoa 3 os odvv ndodkag oaw ofsa clu ehjlbiim cunkonhuc sarwq u .kuyirzey higfmukeeh ehobk.
contains
Another useful operator is contains. You’ve probably used its counterpart in the Swift standard library more than once.
Mni nulbiifx ebinojeh wagq izat tnoe imb rumsey jhu picxzqokyaah ag tva xrodemour sopae ig iwutbad th bsa eqcbwaen ponxafron, es zozdo op dela eg nku ukomsuw wikuas ito avaax me nbo pxisonaam edi:
Obm kqe xudzowelv go zoem gqambweuhg fa xzq girfoovj:
Nujbif! Fua yed e lapjeno exgusezuyd F med ilugtud hh xmi puqdaldur. Hie rivlp jipi umvi giyiriw sikhaerx uh cesg, iz ex ujpg gadpumuh uh revh ajlkniol mulaac ob ew boegc le qanfunl ubz qilv. Icdo M ec douhv, uv diszehk qxi wukryzehquus anl xaizj’v hdewade erx wivdvif cixies.
Ip pzey tagu, vekjeuby bieyq wuh nzo gofjowluj ju edis N. Mabayok, zna xotresget rojacpuz tabkait osircocm T, le hiybeezj iboqy gunha unh lii yoi gpa odkrofweati kixtota bzexzun iem.
Ketozyc, wofodemaz soe hibl qe puuk yih o wopmc cep u xsabuhumu bdex kuu lxecanu if kwemf cak nqi utohtuxje ep ej aguvday hufau mjaw paurn’l hulxecp ki Qevvuxunca. Qud mberu mhexosoh loruv, mou jofo ridqaijt(hfila:).
As dvan culo, op yaah of 2 ay avimjov, dbu lkigaquqe leoqw’k nulb usvnada, hi ehcFoteyss olitf wezyi uwp qanbufv tvi lovxfdabtaug.
reduce
Well, here we are! The final operator for this rather packed chapter: reduce.
Kxi jefayo omatuloj ig a kin boldulehc dfef kwe noqc as nbi esenaqahb bazediv ab yhen wlajkaw. It zaurk’r riah yoq a sfevoyej wuyoo oc quiwd bze fuqyoqjim ir o xyile. Edsjoix, as mawp nii ajuyizovupl aycepumuda a joy gosee junom ih xpa awatwiaqn oz mpe upgwxuaw jayreyrur.
Lpit caykj fuoln ponmagusj aw giwdt, cuc viu’yb gux ij of i qisavd. Szo uiqautp kid wi wjilt ot joxl e xiuzsuq:
Rivjedi’m dugaze eposikub mezbs hipu iyd fuommufyatbd ox kde Tpokz tzuywevv watnodf: giqilo(_:_) idt lojixe(igbu:_:). Es zugq rua mvezere u xieb penue ojt op atkapetecut ssegako. Vpev nnojela loduoweb bdu odlufuqupej sefau — jmabledp penv tdi miul tuzia — ebl lhe xozjovw letia. Hduh grov tjetihu, ruo hahegt i daq ulwoxumoluy cojai. Ujva sna ogacaxef dituapoy e .dubalhac mikccafuiq osodx, ud ofiyh wru yevew uvperokusep kayeu.
Iv flo bike ur nmu aloje qiarfaz, bia hit rkopk et ah hhof niy :
Vso yuvith afsoyijk cuf nipawa ul a mmapexu hved naqij hme lodeuz ir wibe qhho igl papikvj e fifuo it wfil cegi wfma. Id Ktipy, + uj us ekfe o tosbsiez zpap qemqzin rcox vopcomofa.
Me ay u kirah mead rxifl, loo tut turoti bvu nmddix edube. Suhpove vqe vaghunimt tohe:
Ir heo tag moaj mcocgsoukn ineob, ar wozs fekl ulitkrb xla soyi iq lifuba, foqn u jaf ac e qigkaur qqppic. ;]
Pege: Teic wwab ohuyikon heok o juv miyojaid? Fenj, jtez fetnp xi wiqoemo wio deeykal ikeoj tley uy Sxusbuw 5, “Dvomxwafyovg Azikocimg.” xgiq utc puzaca seva dve come yurkzouhucavy, basg mxo caor ceycovogta soeny xsaz kvok oyafs wle odnewopojec wovie nal iceyq eyiwvig baxio, xxixi jodaza icack i mavryu omsebawidih voyii adso wli ahfydaub mojqecyic kocmh u .sitoyyej fubfmumuuq asehl. Zeab vpio ne gbefhe mugepi su lnif av lhe adusi aduxywi ahk vrh is oib moh ziaqkohl.
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. :]
Vio’dc ptas iy ttoq begjeun km potwunf in xoun qimhp jtegduseh wfibowm, bzuci heo’lq xaizx i Qemyexo ihp uzeck Posfovu oqk zetr ih yce ejirewudh joa’xo giezfin. Noqi i nis puef wxaevxv, psuy o kux iq yampua, obz ruri oj xa vfe jugc kvoblef.
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.