-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Learning Swift Second Edition
By :

The first thing to realize is that Swift is not a functional programming language. At its core, it will always be an object-oriented programming language. However, since functions in Swift are first-class citizens, we can use some of the core techniques. Swift provides some built-in methods to get us started.
The first method we are going to discuss is called filter. As the name suggests, this method is used to filter elements in a list. For example, we can filter our numbers
array to include only even numbers:
var evenNumbers = numbers.filter({ element in element % 2 == 0 }) // [2, 4]
The closure we provide to filter will be called once for each element in the array. It is tasked with returning true
if the element needs to be included in the result and false
otherwise. The preceding closure takes advantage of the implied return value and simply returns true
if the number has a remainder of zero when being divided by two.
Note that...
Change the font size
Change margin width
Change background colour