Functional Programming in Swift
In this intensive two-day course, we’ll cover Functional Programming essentials in Swift. By the end of this course, you will have a solid grasp on the Fundamentals of the language.
Course content of Functional Programming in Swift with Bow
Functional Programming Fundamentals
Part 1 of this training covers fundamentals of Functional Programming.
Introduction to Functional Programming
This lesson introduces FP and the Swift features that enable it, and the benefits we obtain from writing functional code. We will show examples of what can be considered a function (being total, deterministic and pure) and provide examples where these requirements are violated. Throughout the course, we will show how we can address those cases.
- What is Functional Programming
- Swift features for Functional Programming: high-order functions, value types, protocols, optionals, functional combinators.
- Referential Transparency.
Domain modeling with Algebraic Data Types
An important part of writing functional code is having a proper model for the domain. This lesson presents the Swift Type System and how it relates to Algebraic Data Types. We will present code examples of OO domain models, usual problems we can have with that approach, and how they can be modeled with ADTs to reduce those issues.
- Swift Type System: reference objects vs. value objects.
- Product types: structs, tuples and classes.
- Coproduct types: enumerations.
- Making illegal states impossible to represent.
Error handling is a pervasive topic across applications and libraries. This lesson will present how Swift deals with errors (using the Error protocol and throwing functions), and we will progressively transition towards having typed errors using different data types provided both by the Swift standard library, and by Bow. We will learn how to operate with these types, the differences between each one of them, and the benefits that they provide.
- From throwing functions to typed errors.
- Types for error handling: Option, Try, Result, Either, Validated, ValidatedNEA.
- Fail fast and error accumulation strategies for error handling.
Working with side-effects
For our code to be functional, one of the most important requirements is purity; that is, the execution of a function should not have side effects. However, side-effects are necessary for our programs to be useful. This lesson covers what are side-effects and how they can be suspended by using the IO type. Once we have made our side-effects explicit, we will learn how to work with them to transform and compose them, both sequentially and in parallel.
- Side effects.
- Suspending side effects with IO.
- Wrapping synchronous and asynchronous side-effectful operations.
- Concurrency and parallelism.
- Working with resources.
- Shared concurrent state.
Applied Functional Programming
Part 2 of this training covers practical applications of Functional Programming in iOS / macOS.
A common concern in many applications is networking. Although the standard library already provides they machinery to do networking operations, it has some limitations. In this lesson we will review those limitations and progressively work towards a more declarative way of writing our networking code. Then, we will cover how to compose the primitives we have written in order to make the rest of our application.
- From imperative to declarative network calls.
- Composing network calls.
- Switching execution contexts.
- Retrial and repetition of network calls.
In order to manage the complexity of a software application, we tend to divide our code into modules that need to communicate to each other. Each module may have multiple dependencies that should be easily replaced to change the implementation or for testing purposes, with no impact on the rest of the code. This technique is usually known as dependency injection. This lesson will cover how we can transition from the usual constructor-based way of injecting dependencies, into a functional style using Kleisli functions and the Reader pattern.
- From constructor-based DI to environmental effects.
- Kleisli functions and composition.
- Modeling dependencies as algebras.