Swift Book: Functional Programming in Swift

Functional Programming in Swift

Learning Swift has been an interesting and enjoyable journey. However, layering functional programming on top of Swift presents a whole new challenge – at least to those of us unfamiliar with functional coding. Good news is, there’s an excellent book that can help:

Functional Programming in Swift written by Chris Eidhof, Florian Kugler, and Wouter Swierstra.

I’ve been reading the book over the past several weeks. Let me share a few thoughts on why I think this book has a bright future…

Descriptions and Code Examples

A personal pet peeve is reading a developer book that skimps on code examples. As the author of a (dated) technical book (Core J2ME), I do understand the additional time commitment to write/test code, on an otherwise already hectic schedule. Yet, I believe the extra effort is paid back many fold for readers.

Functional Programming in Swift never let me down. Begining within the first few pages, code examples abound. And rather than simply explain concepts at the highest level, more often than not, great detail is provided and relevant code examples follow.

Along the same line of thinking, in addition solid examples, the authors explain the implications of your design choices and how that translates to writing good code. To give you an idea, here’s a line from the chapter The Value of Immutability:

“In this chapter, we will explain how these different mechanisms work, distinguish between value types and reference types, and argue why it is a good idea to limit the usage of mutable state.”

Compare and Contrast

As with the above, the same philospohy applies to explaining the relevance of Swift concepts with a compare and contrast to Objective-C. For example, when talking about optionals, it’s agreed that Objective-C offers more flexibility in dealing with nil values (from the perspective of sending messages to a nil object). Whereas Swift employs a much tighter type system, and with that an entire class of errors have been wiped off the map.

Another good example is how Objective-C manages various encoding options on NSString objects using enumerations. This is contrasted with the Swift implementation that provides flexible enumeration types.

Relevance and Familiarity

I appreciate how the authors integrate functional programming in a manner that makes sense, with examples that are familiar territory for Objective-C developers. From the chapter on Diagrams:

“In this chapter, we’ll look at a functional way to describe diagrams, and discuss how to draw them with Core Graphics. By wrapping Core Graphics with a functional layer, we get an API that’s simpler and more composable.”

Working with concepts that are familiar (Core Graphics) and creating something of value in the process (a wrapper to simplify usage), is certainly a win.

Functional Programming

As I eluded to previously, my experience with functional languages and programming is minimal. With that said, the authors provide background information on a broad range of functional topics, explaining why they are relevant as a developer, and follow with very good examples focusing on Swift.

Summary:

Although expertise in Swift is not a requirement before reading the book, the more comfortable you are with Swift, the quicker you can put the ideas presented in the book to work.

Overall the book is very well put together. As I become more adept with Swift, and continue to work through the code examples, I’m confident my expertise with functional programming will follow.

I look forward to the next edition, which will include support for Swift 2.0.

Enter Drawing For A Copy of Functional Programming in Swift

The authors of Functional Programming in Swift have generously donated two copies of the book for the Swift Sandbox newsletter giveaway. Register for the newsletter and you are good to go.

Next Week’s Swift Book

Interested in developing OS X apps in Swift?