Swift Functional Programming: Pure Functions

January 15th, 2018

Filed under: iOS Development, Mac Development | 2 comments

A pure function doesn’t reference any data outside of the function. If you supply a given input to a pure function, it always returns the same value. The following is a simple example of a pure function:

func increment(value: Int) -> Int {
    return value + 1

The increment function is pure because it increments the value you supply to it. It doesn’t modify any outside variables. If you pass the value 9 to increment, it always returns 10.

The following code is a hypothetical example of an impure function that updates contact information based on the values of text fields in an app’s user interface:

func updateContactInfo() {
    name = nameTextField.text
    address = addressTextField.text
    phone = phoneTextField.text

The updateContactInfo function is impure because it modifies variables outside of the function: name, address, and phone.

Pure Functions Simplify Unit Testing

A big advantage of pure functions is they’re much easier to unit test. To test the increment function, all you have to do is create an integer, call increment, and assert the incremented value is correct.

I know increment is a simple function, but unit testing any pure function follows the same steps.

  1. Create values for the arguments.
  2. Call the function.
  3. Assert the function returns the correct value.

Unit testing the updateContactInfo function would be painful. You would have to perform the following steps:

  1. Create an instance of the data structure that contains the contact information.
  2. Create, mock, or stub three text fields.
  3. Provide text for the three text fields.
  4. Call updateContactInfo.
  5. Assert the name, address, and phone number are correct.

Swift First Class Functions

January 12th, 2018

Filed under: iOS Development, Mac Development | Be the first to comment!

I considered writing an article about first class functions in Swift, but I came across the following article by John Sundell:

First class functions in Swift

Sundell’s article covers pretty much everything I would cover in an article on first class functions. The examples are good too. So I decided to link to his article rather than write my own.


Migrating to Bootstrap 4

January 9th, 2018

Filed under: Uncategorized | Be the first to comment!

I am migrating this website from Bootstrap 2 to Bootstrap 4. Some things may look off due to the migration.


Swift Functional Programming: reduce

January 8th, 2018

Filed under: iOS Development, Mac Development | Be the first to comment!

The reduce function takes all the elements of a collection and combines them into a single value. Supply an initial value and a function or closure (unnamed function) to combine the elements.

The following code demonstrates how to calculate the average for a collection of test scores:

let testScores = [78, 96, 48, 65, 59, 91]

let average = testScores.reduce(0.0, { x, y in
    Double(x) + Double(y)
}) / Double(testScores.count)

The closure inside the call to reduce calculates the sum of the test scores. Divide the sum by the number of scores to calculate the average. I had to convert the scores and the array count to floating-point values to calculate the average accurately. Swift does not let you divide two integers and return a floating-point value. Dividing two integers yields an integer, which you can test in a playground with the following code:

let integerAverage = testScores.reduce(0, { x, y in
    x + y
}) / testScores.count

Dividing the two integers yields an average of 72 instead of 72.833333.

A Cleaner Way

My code example demonstrates the syntax for using reduce with closures, but the code could be cleaner. A cleaner way to calculate the average is to use reduce to calculate the sum and do the floating-point conversion when dividing.

let sum = testScores.reduce(0, +)
let average = Double(sum) / Double(testScores.count)

The code to call reduce is simpler in this version because there’s no closure to write.


Swift Functional Programming: map

January 3rd, 2018

Filed under: iOS Development, Mac Development | Be the first to comment!

The map function takes all the elements in a collection and applies a function or a closure (an unnamed function) to them. The following code demonstrates using map to multiply each element of an array by itself:

let numbers = [1, 2, 3, 4, 5]
let squares = numbers.map {
    return $0 * $0

The $0 expression represents the current item in the array.

The squares array has the following values:

[1, 4, 9, 16, 25]

You could double the items in the numbers array with the following code:

let doubles = numbers.map {
    return $0 * 2

Using map with a Named Function

I used closures in the previous examples. Now it’s time to demonstrate how to use map with a named function. Supply the name of the function as the argument to map. The following example uses a function with map to multiply each element of an array by itself:

func square(value: Int) -> Int {
    return value * value

let numbers = [1, 2, 3, 4, 5]
let squares = numbers.map(square)