Intro to Closures Part 2

Introduction

Welcome to Lesson 2 of Intro to Functional Swift. In the previous lesson, you’ve learned how to create a closure block with various forms. Today, you will learn how to create a special function. It not only takes a closure block as its parameter and but also returns it.

Problem

Can you pass/return a function/functions to a function?

Review

Add two numbers using a closure

  1. var addWithClosures: (Int, Int) -> Int = { (number1: Int, number2: Int) in
  2. return number1 + number2
  3. }

Return String

  1. var returnString: () -> String = { () in
  2. "hello"
  3. }
  4. let returnedValue = returnString() // "hello"

First Class Citizen

The Swift functions and closures are often referred as “First Class Citizen”. You may

  1. store in a variable/constant
  2. pass as a parameter
  3. return

Disclosure: The Swift Function is called Global Closure

Return Closure

Let us create a function that returns a closure block. There are two ways.

Return Closure Indirectly

First, design a function whose return type is (Int, Int) -> Int.

  1. func returnClosure() -> ((Int, Int) -> Int) {
  2. return addWithClosures
  3. }
  4. let addClosure = returnClosure()
  5. addClosure(4, 10)
  6. returnClosure()(4, 10) // addWithClosure(4, 10)

You’ve returned addWithClosures whose type is (Int, Int) -> Int as well. If there is no () at the end, it is considered as a constant/variable.

Return Closure Directly

You may return a closure block directly instead of addWithClosures.

  1. func returnClosureDirectly() -> ((Int, Int) -> Int) {
  2. return { (number1, number2) in number1 + number2 }
  3. }
  4. returnClosureDirectly()(4, 10) // 14

You may use the short from instead.

  1. func returnClosureDirectlyTwo() -> ((Int, Int) -> Int) {
  2. return { $0 + $1 }
  3. }
  4. returnClosureDirectlyTwo()(4, 10) // 14

Pass Closure

Create a function whose parameter accepts () -> String. Then, execute the closure block within the function.

  1. func insertClosureBlock(closureBlock: () -> String) -> String {
  2. return closureBlock()
  3. }

Design Closure Block

Create a closure which will be passed in the insertClosureBlock function.

  1. func hello() -> String {
  2. return "hello"
  3. }

Pass Closure Indirectly

  1. insertClosureBlock(closureBlock: hello)
  2. // "hello"

Pass Closure Directly

  1. insertClosureBlock(closureBlock: { _ in return "hello" })
  2. insertClosureBlock(closureBlock: { return "hello" })
  3. insertClosureBlock(closureBlock: { "hello" })

Resources

No Fear Closures with Bob Part 1 and Part 2.

Source Code

3002_intro_closures_part2.playground

Conclusion

You’ve learned how to pass and return a closure block directly and indirectly within a function. The is the fundamental root of functional programming which I’m not going to dive much into in this course. You may sign up for my mailing list if you are interested in learning about reactive programming with RxSwift and MVVM.

We’ve covered the basics of closures in Swift. From now on, difficulty of this chapter will dramatically increase. Before you step onto the arena, I recommend you to read my articles and practice on your own until you become well-versed with the closure syntax. If not, you will suffer.

In the following lesson, you will learn how to initialize an object using a closure block and the meaning of lazy in the Swift Programming Language.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.