Reactive Programming in iOS with Combine

Feb 4 2021 · Swift 5.3, macOS 11.0, Xcode 12.2

Part 2: Transforming & Filtering Operators

11. More Filtering Operators

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Filtering Operators Next episode: 12. Challenge: Filter All the Things

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Now with the basics of filtering out of the way, let's focus in on some operators that will help you refine the values you'll send to downstream consumers. Let's talk about a few operators that have parallels to functions and the swift standard library. You'll begin with first where, which as the marble diagram shows, returns the first value from the publisher that causes the closure pass into the operator to return true. This operator is lazy, it only takes as many values as it needs to find one that matches the predicate. Once that happens, it cancels the subscription and completes, let's take a look at that in a demo. Add the following to the starter playground. Here you create a publisher from the range of numbers from one through nine and then attach a first where operator that looks for the first instance of an even value. Run the playground and you'll see that as expected it returns, two. Let's do a quick check to see if the publisher keeps emitting values. Add the print operator before that call to first, to see what the pipeline is doing. Running the playground you'll see that indeed, the publisher gets a cancellation after emitting the value, two. On the flip side of first where, is last where, which is greedy, it needs all the values to know whether a matching value has been found, because if it doesn't, how will it know if the last one it found is well, the last? Therefore this requires a publisher that will complete at some point, let's take a look. Add the following to the playground. This looks very familiar to the example for first where, returning eight as the result. You can even send values one by one, copy the existing code, change numbers to a pass through subject and send values one through five, along with a completion event. Notice how four, is correctly detected as the last even number sent before the completion. In some cases, you wont to receive values until some condition is met and then force the publisher to complete. The prefix family of operators is a good fit for this. One is aptly named, prefix. This operator only takes up to the specified number of values and then completes, let's take a look. Add the following to your playground. Here you create the usual publisher from a sequence and then applying the prefix operator, the playground returns one and two as accepted values. Like first where, this operator is lazy only taking what it needs and then completing. On the flip side a prefix is the family of drop operators. These can ignore values up to some condition and then start accepting them. The marble diagram for drop while, shows that it ignores the first four values and then passes the remaining values to the consumer. This is very much the opposite of prefix, which took the first n-values and ignore the rest. By the way, prefix has a corresponding prefix while variant that works in a similar fashion. The predicate passed into the operator is used to determine how many values to drop, let's take a look. Add the following to your playground. Here after setting up the standard publisher example, use the drop operator to filter out values that aren't divisible by five and lower than five. In this part of the course you learned all about filtering operators. Filtering operators let you control which values emitted by publishers get sent downstream. Ignore output can be used to skip values entirely, only receiving the completion event. Finding values can be performed with first where, which is lazy and last where, which is greedy. The drop family of operators can be used to ignore values in the stream before sending the rest downstream and the prefix family can be used to send a certain number of values downstream before completing. Next, it's time for a challenge with all these filtering operators, join me the next episode.