Swift high-order reduce usage with custom types

What is Reduce?

Map is a general term that has roots in other technologies (such as Hadoop big data processing, which is where I've used it before 🙂).  Reduce means to iterate over a sequence of objects, and perform some operation on each one, returning some summary value computed from the entire set.   Map and Reduce are often used together, as they will be in the last example of this post.

If you're not familiar with the purpose and syntax of .map, see this related post for more details.

The canonical example of reduce is adding up an array of integers to calculate a sum. The most concise example of this is:

 1    let pageCounts = [992, 108, 378, 
 2                      1152, 244, 206, 
 3                      164, 544, 336, 312]
 5    let totalPages = pageCounts.reduce(0, +)
 7    print("Total Pages: \(totalPages)")
 9This results in the output:
11    Total Pages: 4436

This code essential instructs the computer to:

  1. Initialize a temporary variable to zero
  2. Iterate over the pageCounts array
  3. For each item int he input array, use the + operator to add each element to the temporary variable's running total.
  4. After all members are iterated over, assign the result to the totalPages constant.

The operator doesn't have to be +. For example:

1    let pageCounts = [992, 108, 378, 1152, 244, 206, 164, 544, 336, 312]
2    let totalPages = pageCounts.reduce(0, -)
4    print("Total Pages: \(totalPages)")
8    Total Pages: -4436

This much brevity is possible only when reducing types that have a unary operator that can be applied to each member. Integers have a + operator, so this works.  With custom types, which is the focus of this post, it usually takes more code to get the job done.

Let's look at some other examples.

The Object

In this article I'll be working with an Array of custom object types (rather than simple types), since this is more representative of my daily workflow.

This is the model object I'll work with for most examples:

1    struct Book {
2        let title: String
3        let price: Double
4        let pageCount: Int
5    }

The Collection

And for most of the code samples, I'll start with an unordered Array of Book objects:

 1    let books:[Book] = [
 2        Book(title: "Don Quixote", price: 9.99, pageCount: 992),
 3        Book(title: "The Great Gatsby", price: 14.99, pageCount: 108),
 4        Book(title: "Moby Dick", price: 13.45, pageCount: 378),
 5        Book(title: "War and Peach", price: 10.89, pageCount: 1152),
 6        Book(title: "Hamlet", price: 11.61, pageCount: 244),
 7        Book(title: "The Odyssey", price: 14.99, pageCount: 206),
 8        Book(title: "The Abbot", price: 8.49, pageCount: 164),
 9        Book(title: "Catch-22", price: 20.06, pageCount: 544),
10        Book(title: "To Kill a Mockingbird", price: 7.19, pageCount: 336),
11        Book(title: "Gulliver's Travels", price: 13.99, pageCount: 312)
12    ]

Calculate the total pages from within the custom model object array

Using the books array declared above, the following would calculate the same page count. However since the Book model struct doesn't have a unary operator, so the .reduce function needs more specific information about what property is being added to the initial accumulator.

1    let totalPages = books.reduce(0) { (current, next) -> Int in
2        return (current + next.pageCount)
3    }
5    print("Total Pages: \(totalPages)")
9    Total Pages: 4436

The only difference is that the Reduce is initialized, but then a closure is used to add the values.

Note that there's a bit of syntax that, in this case is optional:

1    -> Int 

This part of the closure syntax tells .reduce what will be returned from the closure.  Logically, the return type specified here should match the initial accumulator value (0).  Normally this bit of syntax is optional, since Swift can infer the return type of the closure. However in some cases where there's ambiguity it may be needed (the compiler will flag this scenario).  In further examples below I'll exclude it where it's not necessary.

Calculate the average price of all books in the inventory

Here's a similar .reduce example to calculate the average price of the books in the inventory array.  Note that .reduce returns only the total, so we have to be a bit clever about calculating the average entirely on the fly...rather than dividing by the number of items in the end.  

1    let averagePrice = books.reduce(0.0) { (current, next) in
2        return current + (next.price / Double(books.count))
3    }
5    print("Average Price: \(averagePrice)")
9    Average Price: 12.565000000000001

Using Reduce for String Processing

Is .reduce is only useful for doing math or statistical analysis? Not at all!  Reduce can be used to perform all kinds of data processing against collection objects.  

For example, let's say we'd like to reverse the words in the book titled "The Great Gatsby".  You can probably think of various ways to do this. For example, you could split the words into an array of String, reverse the array, then iterate over the array to combine the (reversed) words into a new string.

This is a reasonable approach, and .reduce can be used to elegantly execute the iteration and result build into a single function call. Take a look at this code:

 1    let titleWords = "The Great Gatsby".split(separator: " ")
 3    let reversedWords = titleWords.reversed().reduce("") { 
 4        (result, next) -> String in
 5            return "\(result)\(result.count > 0 ? " " : "")\(next)"
 6        }
 8    print(reversedWords)
12    Gatsby Great The

Let's take it one step further: combine Map and Reduce into a single expression.

Bonus: Combine Map and Reduce

Let's extend on the previous example of reversing title words to perform an operation on the entire inventory.

In this example, we'll create a result set that:

  1. Returns all books with titles of 2 or more words as an Array of Book.
  2. For each member in the result set, reverse the title (i.e. title words are in reverse order).
  3. Exclude books from the result where the book title has only one word.

To accomplish all the objectives, we'll actually combine .compactMap and .reduce.

  • .compactMap will be used to transform the input from all books to book that have more than one word in the title.
  • .reduce will be used to reverse the words in the title in the output array.

.compactMap is really just a version of .map that omits nil results in its output. If you're not familiar with the purpose and syntax of .map or .compactMap, see this related post for more details.

Here's the code:

 1    let newBooksArray = books.compactMap { (book) -> Book? in
 3        let titleWords = book.title.split(separator: " ")
 5        if titleWords.count > 1 {
 6            return Book(
 7                title: titleWords.reversed()
 8                                 .reduce("") { (result, next) -> String in
 9                    return "\(result) \(next)"
10                           .trimmingCharacters(in: .whitespaces)
11                }
12                , price: book.price, pageCount: book.pageCount)
13        } else {
14            return nil
15        }
16    }

The result of this code is the following array:

1    Quixote Don               9.99   992   
2    Gatsby Great The          14.99  108   
3    Dick Moby                 13.45  378   
4    Peach and War             10.89  1152  
5    Odyssey The               14.99  206   
6    Abbot The                 8.49   164   
7    Mockingbird a Kill To     7.19   336   
8    Travels Gulliver's        13.99  312
  • All titles present have been reversed
  • The two titles (Hamlet and Catch-22) that had only one word were skipped

Some notes about this solution:

  • If the number  of words is < 2 then the new Book object isn't built at all–compactMap is provided a nil value. As we learned in this related post, compactMap excludes nil members from its results.
  • .reduce is used to concatenate the reversed words into the output string.  This is actually not very different than adding integers or doubles, but illustrates the flexibility that* .reduce* provides.