O'Reilly logo

Reactive Programming with Swift 4 by Navdeep Singh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required