In this series, I am talking about what I am learning from ‘Functional Programming’ by In chapter 1 I was confronted with a whole new way of thinking: returning functions from functions. Now this idea vaguely made sense on some level, but the synthesis of several other concepts at the same time, such as closures and their intricacies, made it hard to proceed without breaking things down.

The Problem – Why would you return functions from functions?

In all honesty I had never written code like this so I wanted to break the problem down a bit:

  • Investigate closures a lot more and practice them
  • Read up on completion blocks more
  • Go back to the Swift documentation
  • Theeeeeeen think about the problem in the book again!

The first chapter exposed some things that I thought I was familiar with, but didn’t fully understand and when this happens it is a great opportunity to focus on improving those areas first; it will pay off in the long run. Things such as escaping closures and autoclosures and closures in general were more complex than I thought.

Let’s look at a super-simple example of a function returning a function and how that could be useful:

typealias messageGenerator = (String) -> String

A typealias in Swift is a way of declaring your own type. Think of it like a placeholder of sorts and in this case we’ve called it messageGenerator. Under the hood, the compiler sees this as a closure (like an anonymous function) that takes in a String as it’s only argument and returns a string.

func generateMessage() -> CodeDescipleMessageMorph {
   return { message in
      return "Code Disciple has altered \(message)"

Now look above – we have a generateMessage function that returns another function. This is the part I was confused about – why on earth would you want to do that? Bare with me. You’ll notice our function doesn’t take any parameters, but it looks like it alters a string of some sort.

When you declare a return type of a closure, you write -> and the declare the return type. This is like a blueprint of what to expect next inside the brackets {}. When inside we return a function.

If you are unfamiliar with closure syntax, message in is just like writing (message: String) { – ie, the parameter is declared above as a string and you use a name for it inside the body; in this case ‘message’.

So, we have a generateMessage function that will return a function for us that we can use later once it’s done it’s thing.

If you now called generateMessage() what would you get? You would see nothing because it’s gone away, done it’s work and is ready to be called. Wait? How do you call it?

var readyToCall = generateMessage()

As you can see we have called it and assigned it to readyToCall. It still does nothing… yet. So how do you call it? This is the part which blew my mind:

readyToCall("Mutate Me!")

You’ve made a variable that has returned a function readyToCall. You’ve then called it by passing in a String and that function returns another String. ‘message’ is now “Mutate Me!” and then it gets altered, returning: "Code Disciple has altered Mutate Me!"

This took me a while to grasp at first, but think of it this way: you’ve made a function that goes off and does something and is ready to do something else when you pass it a variable: like a remote control – you go away and program it, it does it’s thing, then returns you a remote control function that takes a number. Once you pass it a number it then does it’s thing with the tv and it works.

Let me know if this makes sense… sort of 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *