The filter operator

The filter operator applies a predicate to each emitted element and only allows them through if they pass:

Let's say that we have a sequence of integers and we want to only work with prime integers, that is, the integer should be greater than one and only divisible by itself and 1.

Let's add a helper extension to our Helper file in the playground. This will be an extension on the Int type to determine whether an integer is a prime number:

// Extension on Int to check if a number is prime or not public extension Int {        func isPrime() -> Bool {        guard self > 1 else { return false }                var isPrimeFlag = true                for index in 2

Get Reactive Programming with Swift 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.