Functional Programming in Swift


Thinking Functionally in Swift (One day)

This course is for experienced Swift developers who want to explore Functional Programming. To book this course for your group, company, or public event see Booking and Availability.

What you'll learn


At the end of this day, you'll look at functions differently. Actually, you'll look at functions differently from the very start of the day when we express Sets and Pictures using structs that contain nothing more than a single property that is a closure.

Next, we review the map() function for arrays and generalize the concept to map for Optionals, Swift 5's new Result type, and our own tyoe. The goal is for you to see map as a design pattern.

A type that supports map is a Functor and one that supports flatMap is a Monad. In our third section we'll look at two flatMaps that are part of the Swift standard library and a third that has been renamed to compactMap. You'll understand why this renaming was a good idea and you'll come to appreciate where we want to use a flatMap.

Our final section looks at a higher-order function that is somewhere in between map and flatMap: apply. In some ways apply is more powerful than flatMap and we'll see that in some situations we get a lot of return with very little effort.

We assume that you are an experienced programmer who has spent some time looking at Swift but you are relatively new to Functional Programming.

What you need


You need a Mac running the latest public version of Xcode. You'll create Swift playgrounds.

Syllabus Highlights -- the one day class will include topics taken from the following


Rethinking Functions

Higher Order Functions

Drawing Pictures Demo

Rethinking Sets

Combinators

Functors

Mapping Arrays

Mapping Optionals

Our own map

Swift 5 Result Type

Non-Container Map (Optional)

Monads

FlatMapping Arrays

FlatMapping Optionals

Compact Map

FlatMapping Result Type

Our own flatMap

Pure

FlatMap and Map

Futures (optional)

Random Number Generators (optional)

Applicatives

Applying Optionals

Applying Arrays

Applying Result

Monoids (optional)

Monoids and Applicatives (optional)