NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

< Back to Latest Articles

Functional programming in Swift: Introduction

Before you dive in to the first article in this course, I want to give you a brief overview of our goals, how the content is structured, as well as a rough idea of what you can expect to find.

Watch the video here, or read the article below

Quick links

Why functional?

There are lots of ways of thinking about programming, and you’ll often hear folks use terms such as imperative programming or declarative programming. Functional programming is one such approach, but it doesn’t really have a clear-cut definition – it’s a little bit fuzzy, so that the exact definition one person gives is unlikely to match the definition from someone else.

But that’s okay; definitions don’t really help much anyway, not unless you’re planning to sit an exam. Instead, I’m always much more interested in help you understand things in practice: I want you to see real functional Swift in action so the benefits are obvious to you, I want to give you hands-on examples even for the most advanced topics so you can get them into your own projects straightaway, and although I want to show you lots of “how” I also I want to give you as much why as I can – I want you to understand the underlying principles that are at play, so you feel confident in discussing and using functional programming at every level.

Lightweight encounters

I taught some functional programming in my book Pro Swift, and we’re going to use a similar approach here – at least to start with. That approach is based on something I was introduced to by Andy Matuschak, who used to work on the UIKit team at Apple.

The approach we’ll be starting with is called “lightweight encounters”, and it means we start by looking at functional programming purely in terms of practical benefits – we’ll pick off one small task, look at how we can solve it without functional programming, then upgrade our code to have a more functional approach. This “before and after” should mean you can see the benefits of functional programming for yourself, and also give you lots of places where you can get quick wins in your own code.

So, that’s a very similar approach to Pro Swift. However, here that’s just our starting point – as this course progresses we’re going to push further and further past lightweight encounters so you understand all the theory as well as the practice.

Now, that doesn’t mean we’re going to drift off into academia: everything we do will have a firm basis in Swift code you can try for yourself, and in fact I really do encourage you to try the code yourself so you can toy around with it and get a stronger grasp.

Stop when you’re ready

This course is structured so that it more or less gets steadily more complex as we progress. This means the whole first chunk of videos should be really useful to everyone, so you can just dive in and explore. As we progress you’ll feel the difficulty ramp up little by little and there might come a point where you think you’ve had enough – that you’ve already got enough benefit from the course, and are happy to move on to something else.

What I want to say is this: it’s okay to stop when you’re ready. Whenever that point is – whenever you think you’ve gained enough from this course to make a significant difference to your code – it’s okay to move on to something else. Obviously I hope you’ll continue get to the end and find value all the way, but if you’re sitting there thinking “this is hard” or – worse – “will I ever use this?” then chances are you’re not learning too much.

Don’t beat yourself up. Take a break, apply what you’ve learned, and reap the benefits of that – once you’re a bit further along you might find you can come back with fresh eyes and get a whole new round of benefits.

Releasing installments

This course is being published in installments, and although the exact release schedule will fluctuate a little depending on topic difficulty, you should be able to come and get new videos regularly.

However, if you are following along as videos are being published, this is a great chance for you to write in to ask for something specific. I’m happy to hear suggestions, as long as they align my goal of teaching theory and practice hand in hand.

Take your time

And finally, I want to remind you again that the most important thing you can do in this course is apply what you learn.

We all rely on muscle memory to make our jobs faster and easier, which means when it comes to solving a problem we naturally rely on approaches that worked previously. In this course we’ll be covering a huge range of techniques, and if you just fly through the material as fast as you can chances are you won’t retain much – it might be fun to learn, but it won’t really stick.

Instead, your goal should be to try to build new muscle memory: to slowly retrain your brain that there are newer, better ways of solving problems, so that over time you reach for functional programming techniques first and instinctively. The only way to do this is to apply, apply, and apply some more – as soon as you finish one part of this course, have a think about where you can try it out in your own projects, so that not only can you refer back to these articles in the future but you can also look at your own code.

Anyway, I hope you enjoy the course, and more importantly that you find lots of real-world success with functional programming!

If you liked this, you'd love Hacking with Swift+…

Here's just a sample of the other tutorials, with each one coming as an article to read and as a 4K Ultra HD video.

Find out more and subscribe here


Transforming data with map()

42:32

FUNCTIONAL PROGRAMMING

FREE: Transforming data with map()

In this article we’re going to look at the map() function, which transforms one thing into another thing. Along the way we’ll also be exploring some core concepts of functional programming, so if you read no other articles in this course at least read this one!

Using memoization to speed up slow functions

36:18

HIGH-PERFORMANCE APPS

FREE: Using memoization to speed up slow functions

In this article you’ll learn how memoization can dramatically boost the performance of slow functions, and how easy Swift makes it thanks to its generics and closures.

How to use phantom types in Swift

24:11

ADVANCED SWIFT

FREE: How to use phantom types in Swift

Phantom types are a powerful way to give the Swift compiler extra information about our code so that it can stop us from making mistakes. In this article I’m going to explain how they work and why you’d want them, as well as providing lots of hands-on examples you can try.

Creating a WaveView to draw smooth waveforms

32:08

CUSTOM SWIFTUI COMPONENTS

FREE: Creating a WaveView to draw smooth waveforms

In this article I’m going to walk you through building a WaveView with SwiftUI, allowing us to create beautiful waveform-like effects to bring your user interface to life.

User-friendly network access

14:26

NETWORKING

FREE: User-friendly network access

Anyone can write Swift code to fetch network data, but much harder is knowing how to write code to do it respectfully. In this article we’ll look at building a considerate network stack, taking into account the user’s connection, preferences, and more.

Making the most of optionals

23:07

ADVANCED SWIFT

FREE: Making the most of optionals

Swift’s optionals are implemented as simple enums, with just a little compiler magic sprinkled around as syntactic sugar. However, they do much more than people realize, and in this article I’m going to demonstrate some of their power features that can really help you write better code – and blow your mind along the way.

Trees

31:55

DATA STRUCTURES

FREE: Trees

Trees are an extraordinarily simple, extraordinarily useful data type, and in this article we’ll make a complete tree data type using Swift in just a few minutes. But rather than just stop there, we’re going to do something quite beautiful that I hope will blow your mind while teaching you something useful.

Interview questions: Introduction

3:54

INTERVIEW QUESTIONS

FREE: Interview questions: Introduction

Getting ready for a job interview is tough work, so I’ve prepared a whole bunch of common questions and answers to help give you a jump start. But before you get into them, let me explain the plan in more detail…

Creating a custom property wrapper using DynamicProperty

14:20

INTERMEDIATE SWIFTUI

FREE: Creating a custom property wrapper using DynamicProperty

It’s not hard to make a basic property wrapper, but if you want one that automatically updates the body property like @State you need to do some extra work. In this article I’ll show you exactly how it’s done, as we build a property wrapper capable of reading and writing documents from our app’s container.

Shadows and glows

19:50

SWIFTUI SPECIAL EFFECTS

FREE: Shadows and glows

SwiftUI gives us a modifier to make simple shadows, but if you want something more advanced such as inner shadows or glows, you need to do extra work. In this article I’ll show you how to get both those effects and more in a customizable, flexible way.

Understanding generics – part 1

20:01

INTERMEDIATE SWIFT

FREE: Understanding generics – part 1

Generics are one of the most powerful features of Swift, allowing us to write code once and reuse it in many ways. In this article we’ll explore how they work, why adding constraints actually helps us write more code, and how generics help solve one of the biggest problems in Swift.

Ultimate Portfolio App: Introduction

11:03

ULTIMATE PORTFOLIO APP

FREE: Ultimate Portfolio App: Introduction

UPDATED: While I’m sure you’re keen to get started programming immediately, please give me a few minutes to outline the goals of this course and explain why it’s different from other courses I’ve written.

Understanding assertions

27:33

INTERMEDIATE SWIFT

FREE: Understanding assertions

Assertions allow us to have Swift silently check the state of our program at runtime, but if you want to get them right you need to understand some intricacies. In this article I’ll walk you through the five ways we can make assertions in Swift, and provide clear advice on which to use and when.

Advanced string interpolation, part one

24:23

ADVANCED SWIFT

Advanced string interpolation, part one

String interpolation is easy, right? Wrong! String interpolation is actually a huge power feature in Swift, and we have a massive array of functionality on hand to help us customize it. In this article I’ll show you just how much control we have, and how to use that control to make your code easier to read.

What are property observers?

1:50

INTERVIEW QUESTIONS

What are property observers?

The technical answer here feels a bit like Swift 101, but as with so many good answers the real value comes when you’re able to describe places where you use them and where they can cause problems.

Continuing SwiftUI

1:01:31

HACKING WITH SWIFT LIVE 2021

Continuing SwiftUI

Now that we have covered all the changes in Swift concurrency, we can start to look at the changes in SwiftUI, starting with refreshable() and searchable().

Checkpoint 6

8:34

SOLUTIONS

Checkpoint 6

Checkpoint 6 of Swift for Complete Beginners asks you to create a struct to model a car, adding properties and methods that make sense. Let’s solve that now…

What is the purpose of size classes?

2:04

INTERVIEW QUESTIONS

What is the purpose of size classes?

Whether you use UIKit or SwiftUI, size classes are Apple’s broad-stroked approach to providing information about the available space for our UI.

Existentials and type erasure – part 2

41:03

ADVANCED SWIFT

Existentials and type erasure – part 2

In the first part of this tutorial we looked at the underlying problem that type erasure is trying to solve, and tried out Swift’s approach using AnySequence. In this second part we’re going to adapt Swift’s own solution to get real type erasure for our own code.

Rendering a bar chart

24:57

RENDERING CHARTS IN SWIFTUI

Rendering a bar chart

Bar charts are one of the simplest and most common ways of representing data visually, and are often taught to kids at a young age. In this article I’ll show you how easy it is to render bar charts in SwiftUI, and show you various customization options to bring those charts to life.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.