Skip to content

Commit

Permalink
docs: enumerations 5
Browse files Browse the repository at this point in the history
์žฌ๊ท€ ์—ด๊ฑฐํ˜• (Recursive Enumerations)
  • Loading branch information
mazdah committed Mar 4, 2022
1 parent a3755ae commit 00d733b
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions _posts/Swift/2022-03-04-Enumerations_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
layout: article
title: Swift - Enumeration 5
excerpt: Swift์˜ Enumeration ์ •๋ฆฌ 5 - Recursive Enumerations
mathjax: true
tags:
- [Swift, Enumeration, switch, Recursive Enumerations]
categories: Swift
comments: true
header:
theme: dark
background: 'linear-gradient(135deg, rgb(34, 139, 87), rgb(139, 34, 139))'
article_header:
type: overlay
theme: dark
background_color: '#203028'
background_image:
gradient: 'linear-gradient(135deg, rgba(34, 139, 87 , .4), rgba(139, 34, 139, .4))'
src: /swift.jpg
---

# Enumeration #5

---

### ์žฌ๊ท€ ์—ด๊ฑฐํ˜• (Recursive Enumerations)

- ์—ด๊ฑฐํ˜•์˜ ์ผ€์ด์Šค์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์—ฐ๊ด€ ๊ฐ’์œผ๋กœ ๋‹ค๋ฅธ ์—ด๊ฑฐํ˜• ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ
- indirect ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •ํ•œ๋‹ค.

```swift
enum ArithmaticExpression {
case number (Int)
indirect case addition(ArithmaticExpression, ArithmaticExpression)
indirect case multiplication(ArithmaticExpression, ArithmaticExpression)
}
```

- ์—ด๊ฑฐํ˜• ์•ž์— indirect๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ indirect๋กœ ๋งŒ๋“ ๋‹ค.

```swift
indirect enum ArithmaticExpression {
case number (Int)
case addition(ArithmaticExpression, ArithmaticExpression)
case multiplication(ArithmaticExpression, ArithmaticExpression)
}
```

- ์œ„ ์˜ˆ์ œ๋Š” 1 ๊ฐœ์˜ ์ˆซ์ž, ๋ง์…ˆ ์‹, ๊ณฑ์…ˆ ์‹ 3๊ฐ€์ง€์˜ ์‚ฐ์ˆ  ํ‘œํ˜„์‹์„ ์ €์žฅํ•œ๋‹ค.
- addition๊ณผ multiflication ์ผ€์ด์Šค๋Š” ์‚ฐ์ˆ  ํ‘œํ˜„์‹์ด ์—ฐ๊ด€๊ฐ’์ด๋‹ค.
- (5 + 4) * 2์™€ ๊ฐ™์€ ์‹์„ ๋ณด๋ฉด ๊ณฑ์…ˆ์˜ ์šฐํ•ญ์€ ํ•˜๋‚˜์˜ ์ˆซ์ž์ด์ง€๋งŒ ์ขŒํ•ญ์€ ๋˜๋‹ค๋ฅธ ํ‘œํ˜„์‹์ด๋‹ค.
- ์ด์™€ ๊ฐ™์€ ํ‘œํ˜„์„ ์œ„ํ•ด ์žฌ๊ท€ ์—ด๊ฑฐํ˜•์ด ํ•„์š”ํ•˜๋‹ค.

```swift
let five = ArithmaticExpression.number(5)
let four = ArithmaticExpression.number(4)
let sum = ArithmaticExpression.addition(five, four)
let product = ArithmaticExpression.multiplication(sum, ArithmaticExpression.number(2))
```

- ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ์‘์šฉ ์˜ˆ์ œ

```swift
func evaluate(_ expression: ArithmaticExpression) -> Int {
// value, left, right๋Š” ์—ด๊ฑฐํ˜• case์˜ ์—ฐ๊ด€๊ฐ’์„ ๋ฐ›์•„์˜ค๊ธฐ์œ„ํ•œ ์ƒ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์œผ๋กœ
// ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊พธ์–ด๋„ ๋œ๋‹ค.
switch expression {
case let .number(value): // ์„ธ ๋ฒˆ์งธ ~ ๋‹ค์„ฏ ๋ฒˆ์งธ๋กœ ์‹คํ–‰
// ์•„๋ž˜ case๋ฌธ์˜ ์ฒซ ๋ฒˆ์งธ ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ 3๋ฒˆ์œผ๋กœ ์‹คํ–‰
// ์•„๋ž˜ case๋ฌธ์˜ ๋‘ ๋ฒˆ์งธ ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ 4๋ฒˆ์œผ๋กœ ์‹คํ–‰๋จ
// ๋งˆ์ง€๋ง‰ case๋ฌธ์˜ ๋‘ ๋ฒˆ์งธ ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๋งˆ์ง€๋ง‰ 5๋ฒˆ์œผ๋กœ ์‹คํ–‰๋จ
return value
case let .addition(left, right): // ๋‘ ๋ฒˆ์งธ๋กœ ์‹คํ–‰
// ์•„๋ž˜ ์ผ€์ด์Šค์˜ ์ฒซ ๋ฒˆ์งธ ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ 2๋ฒˆ์œผ๋กœ ์‹คํ–‰๋จ
// left๋Š” number(5) ์ด๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ ์‹œ ๋ฐ”๋กœ ์œ„์˜ case๋ฌธ ์‹คํ–‰๋จ
// right๋Š” number(4) ์ด๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ ์‹œ ๋ฐ”๋กœ ์œ„์˜ case๋ฌธ ์‹คํ–‰๋จ
return evaluate(left) + evaluate(right)
case let .multiplication(left, right): // ์ฒซ ๋ฒˆ์งธ๋กœ ์‹คํ–‰
// product๊ฐ€ multiplication์ด๋ฏ€๋กœ 1๋ฒˆ์œผ๋กœ ์‹คํ–‰
// left๋Š” addition์ด๋ฏ€๋กœ ๋ฐ”๋กœ ์œ„์˜ ์ผ€์ด์Šค๋ฌธ์ด ์‹คํ–‰๋จ
// right๋Š” number(2) ์ด๋ฏ€๋กœ ์žฌ๊ท€ ํ˜ธ์ถœ ์‹œ ์ฒซ ๋ฒˆ์งธ case๋ฌธ ์‹คํ–‰๋จ
return evaluate(left) * evaluate(right)
}
}

print(evaluate(product))
// "18" ์ถœ๋ ฅ
```

0 comments on commit 00d733b

Please sign in to comment.