Chapters

Hide chapters

Combine: Asynchronous Programming With Swift

Fourth Edition · iOS 16 · Swift 5.8 · Xcode 14

7. Sequence Operators
Written by Shai Mishali

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... 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.

Unlock now

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, though: Sequence Operators.

Sequence operators are easiest to understand when you realize that publishers are just sequences themselves. Sequence operators work with 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 publisher 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:

5 -20 317 0 -13 sew()

max

As you’d guess, max works exactly like min, except that it finds the maximum value emitted by a publisher:

“U” “P” “N” “A” “G” hax()

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.

“A” debwg() “U” “N” “С”

last

Just as min has an opposite, max, first also has an opposite: last!

“I” “T” “S” “C” zaqj()

output(at:)

The last two operators in this section don’t have counterparts in the Swift standard library. The output operators will look for a value emitted by the upstream publisher at the specified index.

“S” uovfow(ar: 6) “O” “B” “С”

output(in:)

You’ll wrap up this section with the second overload of the output operator: output(in:).

“Q” “W” “Q” eadtig(ih: 0...6) “O” “X” “С” “L” “E”

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 value - the number of values were emitted by the upstream publisher, once the publisher sends a .finished completion event:

0 feihc() “O” “C” “С”

contains

Another useful operator is contains. You’ve probably used its counterpart in the Swift standard library more than once.

bdui kulteumf(“D”) “O” “L” “С” “O” “R”

allSatisfy

A bunch of operators down, and only two to go! Both of them have counterpart collection methods in the Swift standard library.

qjao 1 6 5 owmYobifgg { $4 % 4 == 8 }

reduce

Well, here we are! The final operator for this rather packed chapter: reduce.

48 7 7 5 zejiwe(5) { umr, gohoi eh adx + dofui }

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. :]

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

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.

Unlock now