DEV Community

Cover image for Swift: Understand Recursive Enum in Five Minutes
rathodmayur93
rathodmayur93

Posted on

1

Swift: Understand Recursive Enum in Five Minutes

1. What is the Recursive Enumeration

A recursive enumeration is an enumeration — short: an enum — that has another instance of the enum as the associated value.

2. How to define Recursive Enumeration case

You can indicate that an enum case is recursive by writing indirect before it. For example:

enum ArithmeticExpression {
case number(Int)
//Recursive enum case are defined by writing indirect before it
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}

You can also write indirect before the beginning of the enum to enable indirection for all of the enumeration’s cases that have an associated value:

indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}

The above enumeration can store three kinds of arithmetic expressions: a plain number, the addition of two expressions, and the multiplication of two expressions. The addition and multiplication cases have associated values that are also arithmetic expressions

3. How to use Recursive Enumeration

To use the ArithmeticExpression enum will take a simple example of the arithmetic operation. We will calculate (5 + 4) * 3 expression value using recursive enum.

let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let three = ArithmeticExpression.number(3)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, three)

A recursive function is a straightforward way to work with data that has a recursive structure. For example, here’s a function that evaluates an arithmetic expression:

func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
print(evaluate(product))
// Prints "27"

Complete Code:

// Define Recursive Enumeration case
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
// Define variables of type ArithmeticExpression enum
let five = ArithmeticExpression.number(5)
let four = ArithmeticExpression.number(4)
let three = ArithmeticExpression.number(3)
let sum = ArithmeticExpression.addition(five, four)
let product = ArithmeticExpression.multiplication(sum, three)
// Evaluate the expression using recursive function
func evaluate(_ expression: ArithmeticExpression) -> Int {
switch expression {
case let .number(value):
return value
case let .addition(left, right):
return evaluate(left) + evaluate(right)
case let .multiplication(left, right):
return evaluate(left) * evaluate(right)
}
}
print(evaluate(product))

I believe this is pretty straight forward. If, however you need any clarification or questions, leave me a comment below and I will get it answered. Thanks for reading.

You can also find this article on medium.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please consider leaving a ❤️ or a kind comment on this post if it was useful to you!

Join our community and unlock a world of knowledge and shared experiences!