No Fear Closure in Swift 4 with Bob

So, you wanna be a functional programmer?

Bob Lee
Bob the Developer
3 min readDec 31, 2016

--

Closures and Functions are like a machine

Last update on May 20th, 2017 | Swift 3.1

Back in the days — not so long ago — I dreaded Closures. I avoided it as much as possible. Those curly braces and weird looking words like in, completionHandler, @escaping seemed daunting and insurmountable. 😶

If you aren’t familiar with closure, don’t you dare worry. I will walk you through. However, I expect my readers to understand what it means to return a value with a function. If not, you may start off with my YouTube lessons on basic stuff and come back after.

Resource

Swift 3 Tutorial with Bob (YouTube)

Motivation

Functional programming is trending in iOS, and closure has to do with it. What? Why? How? Well, I think we are just a bit too early to discuss the relationship. It’s like eating meat without teeth. It’s hard to swallow much of it without a robust foundation. Hopefully, I can make a real smooth transition in Part 2.

What I think you will learn

The meaning of closure, higher order function, first class function. These words may come across foreign to you. Well, let’s learn together.

Who are you, Closure?

When I explain it to my 13-year old sister, I just say that it is a function without the keyword func and without a name. From the newcomer’s perspective, however, Closures seem naked and even incomplete.

So, let’s compare how you would add two numbers in a function vs closure. Both will take two Int parameters and return one Int. Let’s begin with a function.

func addTwoNumbers(number1: Int, number2: Int) -> Int {
return number1 + number2
}
var storedFunc = addTwoNumbers
storedFunc(5, 9) // 14

I’ve stored the addTwoNumber function into the newly created var called, storedFunc. But, how is this possible? Well, in Swift 3, just like many other programming languages, Swift functions are described as a first-class function. I don’t know why it is called that way, but you can store a function to a variable/constant.

However, we don’t have to use the func keyword to store a function. Indeed, we can use closure instead.

var storedClosure: (Int, Int) -> Int = { (number1, number2) in
return number1 + number2
}
storedClosure(number1: 5, number2: 9) // 14

The above example is identical to the first example. The in keyword is used to separate the input parameters,number1 andnumber2from the return part. Also, we’ve stated that the type of storedClosure is (Int, Int) -> Int. The pre-stated type tells the variable that it takes two parameters and return one Int.

I hope by now, you’ve started to grasp the idea that closure is a function without name and the func keyword. Or, you can say a function is another verbose closure. 🤔

But, the example above can be simplied, and, yes, it is still called closure.

// Shorter
var storedClosure: (Int, Int) -> Int = { return $0 + $1 }
// Super Short
var storedClosure: (Int, Int) -> Int = { $0 + $1 }

The content has been migrated from Medium to the personal blog. If you wish to read the full tutorial on closures, feel free to visit new platform here.

--

--