Recursive Enums

Introduction

Welcome to Lesson 5 of Advanced Enum. In this lesson, you will discover how to create a complex enum structure using a recursive. Some may have not heard the term, “recursive” before. I will briefly show you what a recursive function is. Nonetheless, this lesson may be challenging.

Problem

Enum contains its own enum type

Recursive Function

Let us begin this lesson by learning about what a recursive function is.

  1. func recursiveFunction() {
  2. print("Hi!")
  3. recursiveFunction() // loop
  4. }

When you execute the recursiveFunction, it never ends.

  1. recursiveFunction()
  2. // "Hi'"
  3. // "Hi'"
  4. // ...
  5. // "Hi"

Design Enum

Create an enum, ArithmeticExpression. It contains three cases with associated types. Two of the cases contain its own enum type, ArithmeticExpression. The indirect keyword is needed to indicate that the enum is a special animal.

  1. indirect enum ArithmeticExpressions {
  2. case number(Int)
  3. case addition(ArithmeticExpressions, ArithmeticExpressions)
  4. case multiplication(ArithmeticExpressions, ArithmeticExpressions)
  5. }

You may initialize using the number case or with addition or multiplication.

  1. ArithmeticExpressions.number(4)
  2. ArithmeticExpressions.addition(.number(4), .number(4))
  3. let five = ArithmeticExpressions.number(5)
  4. let four = ArithmeticExpressions.number(4)

You may create a recursive function to execute, addition and multiplication.

  1. func evaluate(_ expression: ArithmeticExpressions) -> Int {
  2. switch expression {
  3. case let .number(value):
  4. return value
  5. case let .addition(left, right):
  6. return evaluate(left) + evaluate(right)
  7. case let .multiplication(left, right):
  8. return evaluate(left) * evaluate(right)
  9. }
  10. }

Important: You may have to look at the function multiple times until you get it. Don’t give up.

Let us execute the function.

  1. evaluate(ArithmeticExpressions.number(4))
  2. evaluate(ArithmeticExpressions.addition(.number(4), .number(4))) // 8
  3. let multiplicationEnumType = ArithmeticExpressions.multiplication(.number(4), .number(4))
  4. evaluate(multiplicationEnumType) // 16

Alternative Normal Enum

A recursive enum to do addition and multiplication is an overkill. Let us use a normal one.

  1. enum Operation {
  2. case addition(Int, Int)
  3. case multiplication(Int, Int)
  4. var result: Int {
  5. switch self {
  6. case let .addition(number1, number2):
  7. return number1 + number2
  8. case let .multiplication(number1, number2):
  9. return number1 * number2
  10. }
  11. }
  12. }

Execute.

  1. Operation.addition(4, 4).result

However, I personally don’t like the you have to use result to get the final value.

Another Alternative

You may create a struct along with functions.

  1. struct StructOperation {
  2. func addTwoNumbers(_ number1: Int, _ number2: Int) -> Int {
  3. return number1 + number2
  4. }
  5. func multiplfyNumbers(number1: Int, number2: Int) -> Int {
  6. return number1 * number2
  7. }
  8. }

Execute

  1. StructOperation().addTwoNumbers(4, 9)

This feels better. Yet, let us make it more zen.

My Choice

Use closures instead.

  1. struct MathOperation {
  2. static let addition: (Int, Int) -> Int = { $0 + $1 }
  3. static let multiplication: (Int, Int) -> Int = { $0 * $1 }
  4. }

Execute

  1. MathOperation.addition(10, 10) // 20

The best.

Source Code

7005_recursive_enum.playground

References

The Swift Programming Language (Swift 3.1)

Conclusion

One of the code smells is “needless complexity”. You don’t need to be fancy. Remember, protocols and enums are double edged swords. If you don’t know how handle them well, it may hurt or even kill you. In Chapter 8, you will learn in-detail about how an indirect enum works using a binary tree data structure.

In the following lesson, you will learn how to combine enums with generics.

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