Swift 3 Functional Programming

4 (1 reviews total)
By Dr. Fatih Nayebi
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with Functional Programming in Swift

About this book

This book is based on Swift 3 Developer preview version and aims at simplifying the functional programming (FP) paradigms making it easily usable, by showing you how to solve many of your day-to-day development problems.

Whether you are new to functional programming and Swift or experienced, this book will strengthen the skills you need to design and develop high-quality, scalable, and efficient applications.

The book starts with functional programming concepts, the basics of Swift 3, and essential concepts such as functions, closures, optionals, enumerations, immutability, and generics in detail with coding examples.

Furthermore, this book introduces more advanced topics such as function composition, monads, functors, applicative functors, memoization, lenses, algebraic data types, functional data structures, functional reactive programming (FRP), protocol-oriented programming (POP) and mixing object-oriented programming (OOP) with functional programming (FP) paradigms.

Finally, this book provides a working code example of a front-end application developed with these techniques and its corresponding back-end application developed with Swift.

Publication date:
June 2016
Publisher
Packt
Pages
296
ISBN
9781785883880

 

Chapter 1. Getting Started with Functional Programming in Swift

In this chapter, we will be introduced to functional programming paradigms, such as immutability, stateless programming, pure, first-class, and higher-order functions. This chapter will give an introduction to the Swift programming language and functional programming paradigms in Swift. The following topics will be covered along with examples:

  • Why functional programming matters?

  • What is functional programming?

  • Swift language basics

  • Immutability

  • First-class, higher-order, and pure functions

  • Optionals and pattern matching

  • Closures

  • Type aliasing

 

Why functional programming matters?


Software solutions are becoming complex and it is necessary to structure them very well for future maintenance and extension. Software engineers try to modularize software to smaller pieces and abstract away complexities in different pieces and layers. Dividing the code into smaller pieces makes it possible to tackle each problem individually. This approach improves collaboration because different engineers can take responsibility for different pieces. Also, they can work on specific parts of software without being concerned about the other parts.

Dividing software into smaller pieces is not the biggest challenge in most of the projects and programming languages. For instance, in object-oriented programming (OOP), software is divided into smaller pieces such as packages, classes, interfaces, and methods. Engineers tend to divide the software into these building blocks by domains, logic, and layers. Classes are recipes to create instances and objects. As the name suggests, the most important building blocks in OOP are objects. Engineers deal with objects and the role and responsibility for them should be clear and understandable.

In OOP, connecting the building blocks to each other is not as easy as dividing them. Connection between different objects may propose strong coupling between them. Coupling is the biggest source of complexity in OOP. A change in a module or class could force change in all coupled modules and classes. Also, a particular module or class might be harder to reuse and test because of coupled modules or classes.

Software engineers try to loosen coupling by structuring the software well and applying different principles and design patterns. For instance, single responsibility, open-closed, Liskov substitution, interface segregation and dependency inversion (SOLID) principles when applied together properly tend to make software easy to maintain and extend.

Even though it is possible to decrease the coupling and simplify software structures, managing the memory, referencing to instances, and testing different objects remains difficult because, in OOP, objects are open to change and mutation.

In functional programming, pure functions are the most important building blocks. Pure functions do not rely on data outside of themselves and they do not change data that exists outside of them. Pure functions are easy to test because they will always provide the same results.

Pure functions can be executed on different threads or cores without any mechanisms to handle multithreading and multiprocessing. This is a very important benefit of functional programming over OOP as multicore programming mechanisms are very complex to handle in OOP. Also, programming for multicore computers is becoming more important day by day because hardware engineers have finally hit the speed limit of light. Computer clocks will not be getting faster in the near future so, in order to have more cycles per second, hardware engineers are adding more processors to chips. There seems no end to how many processors we will have in our computers. A higher number of processors to be used for a program means a more complex multithreading and multicore mechanism to handle.

Functional programming eliminates the need for a complex multicore programming mechanism, and as pure functions are not dependent on any instances or data outside of themselves, it is easy to change them without changing other parts.

 

What is functional programming?


We know functional programming matters, but what is it really? There is a lot of hype related to functional programming and there are a lot of definitions about it, but simply it is a style of programming that models computations as the evaluation of expressions. Functional programming is a declarative programming style, as opposed to OOP that is categorized as imperative programming.

Theoretically, functional programming employs the concepts of category theory, which is a branch of mathematics. It is not necessary to know the category theory to be able to program functionally but studying it will help us grasp some of the more advanced concepts such as functors, applicative functors, and monads. We will get into category theory and its relationship with functional programming later, so for now we are not going to talk math and we will scratch the surface of functional programming pragmatically.

Let's start with an example to understand the differences between functional programming and OOP styles. The following example gives two different approaches to array element multiplication:

let numbers = [9, 29, 19, 79]

// Imperative example
var tripledNumbers:[Int] = []
for number in numbers {
    tripledNumbers.append(number * 3)
}
print(tripledNumbers)

// Declarative example
let tripledIntNumbers = numbers.map({ number in 3 * number })
print(tripledIntNumbers)

In the imperative example, we give a command to go through each item in the array, multiply each item by 3, and add it to a new array. In the declarative example, we declare how numbers should be mapped. We will have more examples of declarative programming in upcoming chapters.

In functional programming, functions are the fundamental building blocks. In OOP, programs are composed of classes and statements, which change the state of classes when executed.

Functional programming avoids using mutable states. Avoiding mutable states makes it easier to test, read, and understand the code although it is not easy to avoid mutable states in some cases such as file and database operations.

Functional programming requires functions to be first-class. First-class functions are treated like any other values and can be passed to other functions or returned as a result of a function.

Functions can be formed as higher-order functions that take other functions as their arguments. Higher-order functions are used to refactor code and reduce the amount of repetition. Higher-order functions can be used to implement domain-specific languages (DSL).

Functions are pure so they do not depend on any data outside of themselves and do not change any data outside of themselves. Pure functions provide the same result each time that they are executed. This property of pure functions is called referential transparency and makes it possible to conduct equational reasoning on the code.

In functional programming, expressions can be evaluated lazily. For instance, in the following code example, only the first element in the array is evaluated:

let oneToFour = [1, 2, 3, 4]
let firstNumber = oneToFour.lazy.map({ $0 * 3}).first!
print(firstNumber) // The result is going to be 3

The lazy keyword is used to get a lazy version of the collection in this example so only the first item in the array is multiplied by 3 and the rest of the items are not mapped.

 

The Swift programming language


Swift is an open source hybrid language developed by Apple that combines OOP and protocol-oriented programming with functional programming paradigms. Swift can be used along with Objective-C to develop macOS, iOS, tvOS, and watchOS applications. Swift can also be used on Ubuntu Linux to develop web applications. This book explains Swift 3.0 Preview 1 and utilizes Xcode 8.0 beta. Source code at GitHub repository will be updated frequently to catch up with Swift changes.

Swift features

Swift has borrowed many concepts from other programming languages such as Scala, Haskell, C#, Rust, and Objective-C and has the following features.

Modern syntax

Swift has a modern syntax that eliminates the verbosity of programming languages such as Objective-C. For instance, the following code example shows an Objective-C class with a property and method. Objective-C classes are defined in two separate files (interface and implementation). The VerboseClass.h file defines an interface as a subclass of the NSObject class. It defines a property, ourArray, and a method, aMethod.

The implementation file imports the header class and provides an implementation for aMethod:

// VerboseClass.h
@interface VerboseClass: NSObject
@property (nonatomic, strong) NSArray *ourArray;
- (void)aMethod:(NSArray *)anArray;
@end

// VerboseClass.m
#import "VerboseClass.h"

@implementation VerboseClass

- (void)aMethod:(NSArray *)anArray {
    self.ourArray = [[NSArray alloc] initWithArray:anArray];
}

@end

// TestVerboseClass.m
#import "VerboseClass.h"

@interface TestVerboseClass : NSObject

@end

@implementation TestVerboseClass

- (void)aMethod {
    VerboseClass *ourOBJCClass = [[VerboseClass alloc] init];
    [ourOBJCClass aMethod: @[@"One", @"Two", @"Three"]];
    NSLog(@"%@", ourOBJCClass.ourArray);
}

@end

A similar functionality in Swift can be achieved as follows:

class ASwiftClass {
    var ourArray: [String] = []

    func aMethod(anArray: [String]) {
        self.ourArray = anArray
    }
}

let aSwiftClassInstance = ASwiftClass()
aSwiftClassInstance.aMethod(anArray: ["one", "Two", "Three"])
print(aSwiftClassInstance.ourArray)

As seen from this example, Swift eliminates a lot of unnecessary syntax and keeps code very clean and readable.

Type safety and type inference

Swift is a type safe language unlike languages such as Ruby and JavaScript. As opposed to type variant collections in Objective-C, Swift provides type safe collections. Swift automatically deducts types by the type inference mechanism, a mechanism that is present in languages such as C# and C++ 11. For instance, constString in the following example is inferred as String during the compile time and it is not necessary to annotate the type:

let constString = "This is a string constant" 

Immutability

Swift makes it easy to define immutable values—in other words, constants—and empowers functional programming as immutability is one of the key concepts in functional programming. Once constants are initialized, they cannot be altered. Although it is possible to achieve immutability in languages such as Java, it is not as easy as Swift. To define any immutable type in Swift, the let keyword can be used no matter if it is a custom type, collection type, or a Struct type.

Stateless programming

Swift provides very powerful structures and enumerations that are passed by values and can be stateless; therefore, they are very efficient. Stateless programming simplifies the concurrency and multithreading.

First-class functions

Functions are first-class types in Swift as they are in languages such as Ruby, JavaScript, and Go so they can be stored, passed, and returned. First-class functions empower functional programming style in Swift.

Higher-order functions

Higher-order functions can receive other functions as their parameters. Swift provides higher-order functions such as map, filter, and reduce. Also, in Swift, we can develop our own higher-order functions and DSLs.

Pattern matching

Pattern matching is the ability to destructure values and match different switch cases based on correct value matches. Pattern matching capabilities are existent in languages such as Scala, Erlang, and Haskell. Swift provides powerful switch-cases and if-cases with where clauses as well.

Generics

Swift provides generics that make it possible to write code that is not specific to a type and can be utilized for different types.

Closures

Closures are blocks of code that can be passed around. Closures capture the constants and variables of the context in which they are defined. Swift provides closures with a simpler syntax compared to Objective-C blocks.

Subscripts

Swift provides subscripts that are shortcuts to access members of collections, lists, sequences, or custom types. Subscripts can be used to set and get values by an index without needing separate methods for the setting and getting.

Optional chaining

Swift provides optional types that can have some or none values. Swift also provides optional chaining to use optionals safely and efficiently. Optional chaining empowers us to query and call properties, methods, and subscripts on optional types that may be nil.

Extensions

Swift provides extensions that are similar to categories in Objective-C. Extensions add new functionality to an existing class, structure, enumeration, or protocol type even if it is closed source.

Objective-C and Swift bridging headers

Bridging headers empower us to mix Swift with Objective-C in our projects. This functionality makes it possible to use our previously written Objective-C code in Swift projects and vice versa.

Automatic Reference Counting

Swift handles memory management through Automatic Reference Counting (ARC), like Objective-C and unlike languages such as Java and C# that utilize garbage collection. ARC is used to initialize and deinitialize resources, thereby releasing memory allocations of the class instances when they are no longer required. ARC tracks retains and releases in the code instances to manage the memory resources effectively.

REPL and Playground

Xcode provides the Read Eval Print Loop (REPL) command-line environment to experiment with the Swift programming language without the need to write a program. Also, Swift provides playgrounds that enable us to test Swift code snippets quickly and see the results in real time via a visual interface. Playgrounds will be used extensively in this book. Also, most of code examples from all the chapters can be experimented on Swift Playgrounds App (https://developer.apple.com/swift/playgrounds/)

Language basics

This section will provide a brief introduction to the basics of Swift programming language. Topics in the upcoming subsections of this chapter will be explained in detail in the later chapters.

Type safety and type inference

Swift is a type safe language. This means that we cannot change the type of a constant, variable, or expression once we define it. Also, the type safe nature of Swift empowers us to find type mismatches during compile time.

Swift provides type inference. Swift infers the type of a variable, constant, or expression automatically so we do not need to specify the types while defining them. Let's examine the following expressions:

let pi = 3.14159 
var primeNumber = 691 
let name = "my name" 

In these expressions, Swift infers pi as Double, primeNumber as Int, and name as String. In case we need special types such as Int64, we will need to annotate the type.

Type annotation

In Swift, it is possible to annotate types, in other words, explicitly specify the type of a variable or expression. Let's see the following example:

let pi: Double = 3.14159 
let piAndPhi: (Double, Double) = (3.14159, 1.618) 
func ourFunction(a: Int) { /* ... */ } 

In this example, we define a constant (pi) annotated as Double, a tuple named piAndPhi annotated as (Double, Double), and a parameter of ourFunction as Int.

Tip

Downloading the example code The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Swift-3-Functional-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Type aliases

Type aliases define an alternative name for an existing type. We define type aliases with the typealias keyword. Type aliases are useful when we want to refer to an existing type by a name that is contextually more appropriate, such as when working with data of a specific size from an external source. For instance, in the following example, we provide an alias for an unsigned 32-bit integer that can be used later in our code:

typealias UnsignedInteger = UInt32 

The typealias definitions can be used to simplify the closure and function definitions as well.

Immutability

Swift makes it possible to define variables as mutable and immutable. The let keyword is used for immutable declarations and the var keyword is used for mutable declarations. Any variable that is declared with the let keyword will not be open to change. In the following examples, we define aMutableString with the var keyword so that we will be able to alter it later on; in contrast, we will not be able to alter aConstString that is defined with the let keyword:

var aMutableString = "This is a variable String" 
let aConstString = "This is a constant String" 

In functional programming, it is recommended to define properties as constants or immutables with let as much as possible. Immutable variables are easier to track and less error-prone. In some cases, such as CoreData programming, SDK requires mutable properties; however, in these cases it is recommended to use mutable variables.

Tuples

Swift provides tuples so that they can be used to group multiple values into a single compound value. Let's consider the following example:

// Tuples 
let http400Error = (400, "Bad Request")
// http400Error is of type (Int, String), and equals (400, "Bad Request")
 
// Decompose a Tuple's content 
let (requestStatusCode, requestStatusMessage) = http400Error 

Tuples can be used as return types in functions to implement multireturn functions as well.

Optionals

Swift provides optionals so they can be used in situations where a value may be absent. An optional will have some or none values. The ? symbol is used to define a variable as optional. Let's consider the following example:

// Optional value either contains a value or contains nil
var optionalString: String? = "A String literal"
optionalString = nil

The ! symbol can be used to forcefully unwrap the value from an optional. For instance, the following example forcefully unwraps the optionalString variable:

optionalString = "An optional String"
print(optionalString!)

Force-unwrapping the optionals may cause errors if the optional does not have a value so it is not recommended to use this approach as it is very hard to be sure if we are going to have values in optionals in different circumstances. The better approach would be to use the optional binding technique to find out whether an optional contains a value. Let's consider the following example:

let nilName:String? = nil
if let familyName = nilName {
    let greetingfamilyName = "Hello, Mr. \(familyName)"
} else {
    // Optional does not have a value
}

Basic operators

Swift provides the following basic operations:

  • The = operator for assignments like so many different programming languages.

  • The + operator for addition, - for subtraction, * for multiplication, / for division, and % for remainders. These operators are functions that can be passed to other functions.

  • The -i operator for unary minus, +i for unary plus operations.

  • The +=, -=, and *= operators for compound assignments.

  • The a == b operator for equality, a != b for inequality, and a > b, a < b, and a <= b for greatness comparison.

  • The ternary conditional operator, question ? answer1: answer2.

  • Nil coalescing a ?? b unwraps optional a if it has a value and returns a default value b if a is nil.

  • Range operators:

    • Closed range (a...b) includes the values a and b

    • Half-open range (a..<b) includes a but does not include b

  • Logical operators:

    • The !a operator is NOT a

    • The a && b operator is logical AND

    • The a || b operator is logical OR

Strings and characters

In Swift, String is an ordered collection of characters. String is a structure and not a class. Structures are value types in Swift; therefore, any String is a value type and passed by values, not by references.

Immutability

Strings can be defined with let for immutability. Strings defined with var will be mutable.

String literals

String literals can be used to create an instance of String. In the following coding example, we define and initialize aVegetable with the String literal:

let aVegetable = "Arugula" 
Empty Strings

Empty Strings can be initialized as follows:

var anEmptyString = ""
var anotherEmptyString = String()

These two strings are both empty and equivalent to each other. To find out whether a String is empty, the isEmpty property can be used as follows:

if anEmptyString.isEmpty {
    print("String is empty")
}
Concatenating strings and characters

Strings and characters can be concatenated as follows:

let string1 = "Hello"
let string2 = " Mr"
var welcome = string1 + string2

var instruction = "Follow us please"
instruction += string2

let exclamationMark: Character = "!"
welcome.append(exclamationMark)
String interpolation

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a String literal. Let's consider the following example:

let multiplier = 3
let message = "\(multiplier) times 7.5 is \(Double (multiplier) * 7.5)"
// message is "3 times 7.5 is 22.5"
String comparison

Strings can be compared with == for equality and != for inequality.

The hasPrefix and hasSuffix methods can be used for prefix and suffix equality checking.

Collections

Swift provides typed collections such as arrays, dictionaries, and sets. In Swift, unlike Objective-C, all elements in a collection will have the same type and we will not be able to change the type of a collection after defining it.

We can define collections as immutables with let and mutables with var, as shown in the following example:

// Arrays and Dictionaries
var cheeses = ["Brie", "Tete de Moine", "Cambozola", "Camembert"]
cheeses[2] = "Roquefort"
var cheeseWinePairs = [
    "Brie":"Chardonnay",
    "Camembert":"Champagne",
    "Gruyere":"Sauvignon Blanc"
]

cheeseWinePairs ["Cheddar"] = "Cabarnet Sauvignon"
// To create an empty array or dictionary
let emptyArray = [String]()
let emptyDictionary = Dictionary<String, Float>()
cheeses = []
cheeseWinePairs = [:]

The for-in loops can be used to iterate over the items in collections.

Control flows

Swift provides different control flows that are explained in the following subsections.

for loops

Swift provides for and for-in loops. We can use the for-in loop to iterate over items in a collection, a sequence of numbers such as ranges, or characters in a string expression. The following example presents a for-in loop to iterate through all items in an Int array:

let scores = [65, 75, 92, 87, 68]
var teamScore = 0

for score in scores {
    if score > 70 {
        teamScore = teamScore + 3
    } else {
        teamScore = teamScore + 1
    }
}

The following is to iterate over dictionaries:

for (cheese, wine) in cheeseWinePairs {
    print("\(cheese): \(wine)")
}

We can use for loops with a condition and an incrementer/decrementer. The following example presents a for loop example:

var count = 0
for var i = 0; i < 3; ++i {
    count += i
}

As C style for loops with incrementers/decrementers are removed from Swift 3.0, it is recommended to use for-in loops with ranges instead, as follows:

var count = 0
for i in 0...3 {
    count += i
}
while loops

Swift provides while and repeat-while loops. A while or repeat-while loop performs a set of expressions until a condition becomes false. Let's consider the following example:

var n = 2
while n < 100 {
    n = n * 2
}

var m = 2
repeat {
    m = m * 2
} while m < 100

The while loop evaluates its condition at the beginning of each iteration. The repeat-while loop evaluates its condition at the end of each iteration.

stride

The stride functions enable us to iterate through ranges with a step other than one. There are two stride functions: the stride to function, which iterates over exclusive ranges, and stride through, which iterates over inclusive ranges. Let's consider the following example:

let fourToTwo = Array(stride(from: 4, to: 1, by: -1)) // [4, 3, 2]
let fourToOne = Array(stride(from:4, through: 1, by: -1)) // [4, 3, 2, 1]
if

Swift provides if to define conditional statements. It executes a set of statements only if the condition statement is true. For instance, in the following example, the print statement will be executed because anEmptyString is empty:

var anEmptyString = ""
if anEmptyString.isEmpty {
    print("An empty String")
} else {
    // String is not empty.
}
switch

Swift provides the switch statement to compare a value against different matching patterns. The related statement will be executed once the pattern is matched. Unlike most other C-based programming languages, Swift does not need a break statement for each case and supports any value types. Switch statements can be used for range matching and where clauses in switch statements can be used to check for additional conditions. The following example presents a simple switch statement with an additional conditional checking:

let aNumber = "Four or Five"
switch aNumber {
    case "One":
        let one = "One"
    case "Two", "Three":
        let twoOrThree = "Two or Three"
    case let x where x.hasSuffix("Five"):
        let fourOrFive = "it is \(x)"
    default:
        let anyOtherNumber = "Any other number"
}
guard

A guard statement can be used for early exits. We can use a guard statement to require that a condition must be true in order for the code after the guard statement to be executed. The following example presents the guard statement usage:

func greet(person: [String: String]) {
    guard let name = person["name"] else {
        return
    }
    print("Hello Ms \(name)!")
}

In this example, the greet function requires a value for a person's name. Therefore, it checks whether it is present with the guard statement, otherwise it will return and not continue to execute.

Functions

Functions are self-contained blocks of code that perform a specific task.

In Swift, function are first-class citizens, meaning that they can be stored, passed, and returned. Functions can be curried and defined as higher-order functions that take other functions as their arguments. Functions in Swift can have multiple input parameters and multiple returns using tuples. Let's see the following example:

func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)"
}

greet(name: "Ted", day:"Saturday")

Functions can have variadic parameters. Let's consider the following example:

// Variable number of arguments in functions - Variadic Parameters
func sumOf(numbers:Int...) -> (Int, Int) {
    var sum = 0
    var counter = 0
    for number in numbers {
        sum += number
        counter += 1
    }
    return (sum, counter)
}

sumOf()
sumOf(numbers: 7, 9, 45)

Prior to Swift 3.0, functions could have mutable and immutable parameters. Let's consider the following example:

func alignRight(var string: String, count: Int, pad: Character) -> String {
    let amountToPad = count - string.characters.count
    if amountToPad < 1 {
        return string
    }
    let padString = String(pad)
    for _ in 1...amountToPad {
        string = padString + string
    }
    return string
} 

Mutable parameters are not favorable in Swift functional programming and are removed from Swift 3.0.

Functions can have inout parameters. Let's consider the following example:

func swapTwoInts( a: inout Int, b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

The inout parameters are not favorable in functional Swift as they mutate states and make functions impure.

In Swift, we can define nested functions. The following example presents a function named add nested inside another function. Nested functions can access the data in scope of their parent function. In this example, the add function has access to the y variable:

func returnTwenty() -> Int {
    var y = 10
    func add() {
        y += 10
    }
    add()
    return y
}

returnTwenty()

In Swift, functions can return other functions. In the following example, the makeIncrementer function returns a function that receives an Int value and returns an Int value (Int -> Int):

// Return another function as its value
func makeIncrementer() -> (Int -> Int) {
    func addOne(number: Int) -> Int {
        return 1 + number
    }
    return addOne
}

var increment = makeIncrementer()
increment(7)

Closures

Closures are self-contained blocks of code that provide a specific functionality and can be stored, passed around, and used in the code. Closures are equivalent of blocks in C and Objective-C. Closures can capture and store references to any constants and variables from the context in which they are defined. Nested functions are special cases of closures. Closures are reference types that can be stored as variables, constants, and type aliases. They can be passed to and returned from functions.

The following examples present different declarations of closures in Swift from the website, http://goshdarnclosuresyntax.com:

// As a variable:
var closureName: (parameterTypes) -> (returnType)

//As an optional variable:
var closureName: ((parameterTypes) -> (returnType))?

//As a type alias:
typealias closureType = (parameterTypes) -> (returnType)

Map, filter, and reduce

Swift provides map, filter, and reduce functions that are higher-order functions.

Map

The map function solves the problem of transforming the elements of an array using a function. Let's consider the following example:

// Return an `Array` containing the results of calling `transform(x)` on
  each element `x` of `self`
// func map<U>(transform: (T) -> U) -> [U]
let numbers = [10, 30, 91, 50, 100, 39, 74]
var formattedNumbers: [String] = []

for number in numbers {
    let formattedNumber = "\(number)$"
    formattedNumbers.append(formattedNumber)
}

let mappedNumbers = numbers.map { "\($0)$" }
Filter

The filter function takes a function that, given an element in the array, returns Bool indicating whether the element should be included in the resulting array. Let's consider the following example:

// Return an Array containing the elements x of self for which
  includeElement(x)` is `true`
// func filter(includeElement: (T) -> Bool) -> [T]
let someEvenNumbers = numbers.filter { $0 % 2 == 0 }
Reduce

The reduce function reduces an array to a single value. It takes two parameters: a starting value and a function, which takes a running total and an element of the arrays as parameters and returns a new running total. Let's consider the following example:

// Return the result of repeatedly calling `combine` with an accumulated
  value initialized to `initial` and each element of `self`, in turn,
  that is return `combine(combine(...combine(combine(initial, self[0]),
  self[1]),...self[count-2]), self[count-1])`.
// func reduce<U>(initial: U, combine: (U, T) -> U) -> U
let total = numbers.reduce(0) { $0 + $1 }

Enumerations

In Swift, an enumeration defines a common type for related values and enables us to work with those values in a type-safe way. Values provided for each enumeration member can be a String, Character, Int, or any floating-point type. Enumerations can store associated values of any given type, and the value types can be different for each member of the enumeration if needed. Enumeration members can come prepopulated with default values (called raw values), which are all of the same type. Let's consider the following example:

enum MLSTeam {
    case montreal
    case toronto
    case newYork
    case columbus
    case losAngeles
    case seattle
}

let theTeam = MLSTeam.montreal

Enumeration values can be matched with a switch statement, which can be seen in the following example:

switch theTeam {
    case .montreal:
        print("Montreal Impact")
    case .toronto:
        print("Toronto FC")
    case .newYork:
        print("Newyork Redbulls")
    case .columbus:
        print("Columbus Crew")
    case .losAngeles:
        print("LA Galaxy")
    case .seattle:
        print("Seattle Sounders")
}

Enumerations in Swift are actually algebraic data types created by combining other types. Let's consider the following example:

enum NHLTeam { case canadiens, senators, rangers, penguins, blackHawks,
  capitals}

enum Team {
    case hockey(NHLTeam)
    case soccer(MLSTeam)
}

struct HockeyAndSoccerTeams {
    var hockey: NHLTeam
    var soccer: MLSTeam
}

The MLSTeam and NHLTeam enumerations each have six potential values. If we combine them, we will have two new types. A Team enumeration can be either NHLTeam or MLSTeam so it has 12 potential values—that is, the sum of the NHLTeam and MLSTeam potential values. Therefore, Team, an enumeration, is a sum type.

To have a HockeyAndSoccerTeams structure, we need to choose one value for NHLTeam and one for MLSTeam so that it has 36 potential values—that is, the product of the NHLTeam and MLSTeam values. Therefore, HockeyAndSoccerTeams is a product type.

In Swift, an enumeration's option can have multiple values. If it happens to be the only option, then this enumeration becomes a product type. The following example presents an enumeration as a product type:

enum HockeyAndSoccerTeams {
    case Value(hockey: NHLTeam, soccer: MLSTeam)
}

As we can create sum or product types in Swift, we can say that Swift has first-class support for algebraic data types.

Generics

Generic code enables us to write flexible and reusable functions and types that can work with any type, subject to requirements that we define. For instance, the following function that uses inout parameters to swap two values can only be used with Int values:

func swapTwoIntegers( a: inout Int, b: inout Int) {
    let tempA = a
    a = b
    b = tempA
}

To make this function work with any type, generics can be used, as shown in the following example:

func swapTwoValues<T>( a: inout T, b: inout T) {
    let tempA = a
    a = b
    b = tempA
}

Classes and structures

Classes and structures are general-purpose, flexible constructs that become the building blocks of a program's code. They have the following features:

  • Properties can be defined to store values

  • Methods can be defined to provide functionality

  • Subscripts can be defined to provide access to their values using subscript syntax

  • Initializers can be defined to set up their functionality beyond a default implementation

  • They can conform to protocols to provide standard functionality of certain kinds

Classes versus structures

This section compares classes and structures:

  • Inheritance enables one class to inherit the characteristics of another

  • Type casting enables us to check and interpret the type of a class instance at runtime

  • Deinitializers enable an instance of a class to free any resources it has assigned

  • Reference counting allows more than one reference to a class instance

  • Structures are value types so they are always copied when they are passed around in code

  • Structures do not use reference counting

  • Classes are reference types

Choosing between classes and structures

We consider creating a structure when one or more of the following conditions apply:

  • The structure's primary purpose is to encapsulate a few relatively simple data values

  • It is reasonable to expect that the encapsulated values will be copied rather than referenced when we assign or pass around an instance of the structure

  • Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced

  • The structure does not need to inherit properties or behavior from another existing type

Example of good candidates for structures include the following:

  • The size of a geometric shape

  • A point in a 3D coordinate system

Identity operators

As classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of class behind the scenes. To find out if two constants or variables refer to the same instance of a class exactly, Swift provides the following identity operators:

  • Identical to (===)

  • Not identical to (!==)

Properties

Properties associate values with a particular class, structure, or enumeration. Swift enables us to set subproperties of a structure property directly without needing to set the entire object property to a new value. All structures have an automatically generated memberwise initializer, which can be used to initialize the member properties of new structure instances. This is not true for class instances.

Property observers

Property observers are used to respond to change in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value. We have the option to define either or both of the following observers on a property:

  • The willSet observer is called just before the value is stored

  • The didSet observer is called immediately after the new value is stored

The willSet and didSet observers are not called when a property is set in an initializer before delegation takes place.

Methods

Methods are functions that are associated with a particular type. Instance methods are functions that are called on an instance of a particular type. Type methods are functions that are called on the type itself.

The following example presents a class containing a type method that is named as someTypeMethod():

class AClass {
    class func someTypeMethod() {
        // type method body
    }
}

We can call this method as follows:

AClass.someTypeMethod()

Subscripts

Subscripts are shortcuts to access the member elements of a collection, list, sequence, or any custom type that implements subscripts. Let's consider the following example:

struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
        return multiplier * index
    }
}

let fiveTimesTable = TimesTable(multiplier: 5)
print("six times five is \(fiveTimesTable[6])")
// prints "six times five is 30"

Inheritance

A class can inherit methods, properties, and other characteristics from another class:

class SomeSubClass: SomeSuperClass

Swift classes do not inherit from a universal base class. Classes that we define without specifying a superclass automatically become base classes for us to build on. To override a characteristic that would otherwise be inherited, we prefix our overriding definition with the override keyword. An overridden method, property, or subscript can call the superclass version by calling super. To prevent overrides, the final keyword can be used.

Initialization

The process of preparing an instance of a class, structure, or enumeration for use is called initialization. Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an intermediate state. We can modify the value of a constant property at any point during initialization as long as it is set to a definite value by the time initialization finishes. Swift provides a default initializer for any structure or base class that provides default values for all of its properties and does not provide at least one initializer itself. Let's consider the following example:

class ShoppingItem {
    var name: String?
    var quantity = 1
    var purchased = false
}

var item = ShoppingItem()

The struct types automatically receive a memberwise initializer if we do not define any of our own custom initializers, even if the struct's stored properties do not have default values.

Swift defines two kinds of initializers for class types:

  • Designated initializers: Methods that are able to fully initialize the object

  • Convenience initializers: Methods that rely on other methods to complete initialization

Deinitialization

A deinitializer is called immediately before a class instance is deallocated. Swift automatically deallocates instances when they are no longer needed in order to free up resources (ARC).

Automatic Reference Counting

Reference counting only applies to instances of classes. Structures and enumerations are value types, not reference types, and are not stored and passed by reference.

Weak references can be used to resolve strong reference cycles and can be defined as follows:

weak var aWeakProperty 

An unowned reference does not keep a strong reference hold on the instance it refers to. Unlike a weak reference, however, an unowned reference is always defined as a non-optional type. A closure capture list can be used to resolve closure strong reference cycles.

A capture in a closure can be defined as an unowned reference when the closure and the instance that it captures will always refer to each other and be deallocated at the same time.

A capture as a weak reference can be defined when the capture's reference may become nil at some point in the future. Weak references are always of an optional type. Let's consider the following example:

class AClassWithLazyClosure {
    lazy var aClosure: (Int, String) -> String = {
        [unowned self] (index: Int, stringToProcess: String) -> String in
        // closure body goes here
        return ""
    }
}

Optionals and optional chaining

Optionals are Swift types that can have some or none values. Optional chaining is a process to query and call properties, methods, and subscripts on an optional that might currently be nil. Optional chaining in Swift is similar to messaging nil in Objective-C, but in a way that works for any type and can be checked for success or failure. Let's consider the following example:

// Optional chaining
class Residence {
    var numberOfRooms = 1
}

class Person {
    var residence: Residence?
}

let jeanMarc = Person()
// This can be used for calling methods and subscripts through optional
  chaining too
if let roomCount = jeanMarc.residence?.numberOfRooms {
    // Use the roomCount
}

In this example, we were able to access numberOfRooms, which was a property of an optional type (Residence), using optional chaining.

Error handling

Swift provides support to throw, catch, propagate, and manipulate recoverable errors at runtime.

Value types should conform to the ErrorType protocol to be represented as errors. The following example presents some 4xx and 5xx HTTP errors as enum:

enum HttpError: ErrorType {
    case badRequest
    case unauthorized
    case forbidden
    case requestTimeOut
    case unsupportedMediaType
    case internalServerError
    case notImplemented
    case badGateway
    case serviceUnavailable
}

We will be able to throw errors using the throw keyword and mark functions that can throw errors with the throws keyword.

We can use a do-catch statement to handle errors by running a block of code. The following example presents JSON parsing error handling in a do-catch statement:

protocol HttpProtocol{
    func didRecieveResults(results:NSDictionary)
}

struct WebServiceManager {
    var delegate:HttpProtocol?
    let data: NSData
    func test() {
        do {
            let jsonResult: NSDictionary = try
              NSJSONSerialization.JSONObjectWithData(self.data,
              options: NSJSONReadingOptions.MutableContainers) as!
              NSDictionary
            self.delegate?.didRecieveResults(jsonResult)
        } catch let error as NSError {
            print("json error" + error.localizedDescription)
        }
    }
}

We can use a defer statement to execute a set of statements just before code execution leaves the current code block, regardless of how the execution leaves the current block of code.

Type casting

Type casting is a way to check the type of an instance and/or to deal with that instance as if it is a different superclass or subclass from somewhere else in its class hierarchy. There are two types of operators to check and cast types:

  • Type check operator (is): This checks whether an instance is of a definite subclass type.

  • Type cast operator (as and as?): A constant or variable of a definite class type may refer to an instance of a subclass under the hood. If this is the case, we can try to downcast it to the subclass type with as.

Any and AnyObject

Swift provides two special type aliases to work with non-specific types:

  • AnyObject can represent an instance of any class type

  • Any can represent an instance of any type, including structs, enumerations, and function types

The Any and AnyObject type aliases must be used only when we explicitly require the behavior and capabilities that they provide. Being precise about the types we expect to work with in our code is a better approach than using the Any and AnyObject types as they can represent any type and pose dynamism instead of safety. Let's consider the following example:

class Movie {
    var director: String
    var name: String
    init(name: String, director: String) {
        self.director = director
        self.name = name
    }
}

let objects: [AnyObject] = [
    Movie(name: "The Shawshank Redemption", director: "Frank Darabont"),
    Movie(name: "The Godfather", director: "Francis Ford Coppola")
]

for object in objects {
    let movie = object as! Movie
    print("Movie: '\(movie.name)', dir. \(movie.director)")
}

// Shorter syntax
for movie in objects as! [Movie] {
    print("Movie: '\(movie.name)', dir. \(movie.director)")
}

Nested types

Enumerations are often created to support a specific class or structure's functionality. Likewise, it can be convenient to declare utility classes and structures purely to use within the context of a complex type.

Swift enables us to declare nested types whereby we nest supporting enumerations, classes, and structures within the definition of the type that they support. The following example, borrowed from The Swift Programming Language (Swift 3.0) by Apple Inc., presents nested types:

struct BlackjackCard {
    // nested Suit enumeration
    enum Suit: Character {
        case spades = "â™ ",
        hearts = "♡",
        diamonds = "♢",
        clubs = "♣"
    }
 
    // nested Rank enumeration
    enum Rank: Int {
        case two = 2, three, four, five, six, seven, eight, nine, ten
        case jack, queen, king, ace
 
        // nested struct
        struct Values {
            let first: Int, second: Int?
        }
 
        var values: Values {
            switch self {
            case .ace:
                return Values(first: 1, second: 11)
            case .jack, .queen, .king:
                return Values(first: 10, second: nil)
            default:
                return Values(first: self.rawValue, second: nil)
            }
        }
    }
 
    let rank: Rank, suit: Suit
 
    var description: String {
        var output = "suit is \(suit.rawValue),"
        output += " value is \(rank.values.first)"
        if let second = rank.values.second {
            output += " or \(second)"
        }
        return output
    }
}

Extensions

Extensions add new functionality to an existing class, structure, or enumeration type. This includes the ability to extend types for which we do not have access to the original source code.

Extensions in Swift enable us to perform the following:

  • Define instance methods and type methods

  • Provide new initializers

  • Define and use new nested types

  • Define subscripts

  • Add computed properties and computed static properties

  • Make an existing type conform to a new protocol

Extensions enable us to add new functionality to a type but we will not be able to override the existing functionality.

In the following example, we extend AType by making it conform to two protocols:

extension AType: AProtocol, BProtocol {
}

The following example presents an extension to Double by adding computed properties:

// Computed Properties
extension Double {
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.2884 }
}

let threeInch = 76.2.mm
let fiveFeet = 5.ft

Protocols

A protocol defines signatures or types of methods, properties, and other requirements that fit to a specific task or piece of functionality. The protocol doesn't actually implement any functionality. It only describes what an implementation will look like. A class, structure, or enumeration that provides an actual implementation of requirements can adopt the protocol. Protocols use the same syntax as normal methods but are not allowed to specify default values for method parameters. The is operator can be used to check whether an instance conforms to a protocol. We can check for protocol conformance only if our protocol is marked with @objc for classes. The as operator can be used to cast to a specific protocol.

Protocols as types

Any protocol that we define will become a fully-fledged type to use in our code. We can use a protocol as follows:

  • A parameter type or return type in a function, method, or initializer

  • The type of a constant, variable, or property

  • The type of items in an array, dictionary, or other container

Let's see the following example:

protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}

// Classes, enumerations and structs can all adopt protocols.
class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class example"
    var anotherProperty: Int = 79799
 
    func adjust() {
        simpleDescription += " Now 100% adjusted..."
    }
}
 
var aSimpleClass = SimpleClass()
aSimpleClass.adjust()
let aDescription = aSimpleClass.simpleDescription

struct SimpleStructure: ExampleProtocol {
    var simpleDescription: String = "A simple struct"
    // Mutating to mark a method that modifies the structure - For classes
      we do not need to use mutating keyword
    mutating func adjust() {
        simpleDescription += " (adjusted)"
    }
}

var aSimpleStruct = SimpleStructure()
aSimpleStruct.adjust()
let aSimpleStructDescription = aSimpleStruct.simpleDescription
Protocol extensions

Protocol extensions allow us to define behavior on protocols rather than in each type's individual conformance or global function. By creating an extension on a protocol, all conforming types automatically gain this method implementation without any additional modification. We can specify constraints that conforming types must satisfy before the methods and properties of the extensions are available when we define a protocol extension. For instance, we can extend our ExampleProtocol to provide default functionality as follows:

extension ExampleProtocol {
    var simpleDescription: String {
        get {
            return "The description is: \(self)"
        }
        set {
            self.simpleDescription = newValue
        }
    }
 
    mutating func adjust() {
        self.simpleDescription = "adjusted simple description"
    }
}

Access control

Access control restricts access to parts of our code from code in other source files and modules:

  • Public: This enables entities to be used within any source file from their defining module and also in a source file from another module that imports the defining module

  • Internal: This enables entities to be used within any source file from their defining module, but not in any source file outside of this module

  • Private: This restricts the use of an entity to its own defining source file

 

Summary


This chapter started by explaining why functional programming matters, and then it introduced key paradigms in functional programming. Furthermore, it introduced the basics of the Swift programming language with code examples. At this point, we should have a broad view of functional programming concepts and the Swift programming language. All the topics in this chapter will be covered in detail in upcoming chapters.

We will begin to dive deeper into these topics by functions as they are the most essential building blocks in functional programming. Therefore, the following chapter will explain functions and give examples for pure, first-class, higher-order, and nested functions. Also, it will explain slightly more advanced topics such as memoization, function currying, and composition.

About the Author

  • Dr. Fatih Nayebi

    Dr. Fatih Nayebi has more than 15 years of industry experience in software engineering and architecture in various fields. He has developed numerous applications with Visual Basic, C++, C#, Java, MATLAB, Python, Objective-C, and Swift. He has been designing and developing enterprise and consumer iOS applications since the release of first iOS SDK. He is also an enthusiastic Node, Scala, and Haskell developer.

    Aside from industry, Fatih earned his Ph.D. degree in software engineering from École de technologie supérieure, Université du Québec by researching on Mobile Human-Computer Interaction, Software Engineering, and Machine Learning.

    His specialties include applied predictive and optimization models, human-computer interaction, functional programming, machine learning, and mobile application architecture and development.

    Fatih currently works as a Director, Consulting at CGI Group Inc, Montreal, and continues to his academic research and publications as a postdoctoral researcher at École de technologie supérieure.

    You can find him talking on Swift and Functional Programming at meetups such as http://www.meetup.com/swift-mtl/, on GitHub at https://github.com/conqueror, on Twitter as @thefatih, and on Instagram as @thefatih.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Good
Book Title
Access this book, plus 7,500 other titles for FREE
Access now