swift 递归枚举
1.什么是递归枚举 (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.如何定义递归枚举案例 (2. How to define Recursive Enumeration case)
You can indicate that an enum case is recursive by writing indirect
before it. For example:
您可以通过在其之前编写indirect
语句来表明该枚举用例是递归的。 例如:
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
指令,以为具有关联值的所有枚举情况启用间接功能:
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
上面的枚举可以存储三种算术表达式:素数,两个表达式的加法和两个表达式的乘法。 addition
和multiplication
案例的关联值也是算术表达式
3.如何使用递归枚举 (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.
要使用ArithmeticExpression枚举,将举一个简单的算术运算示例。 我们将使用递归枚举计算(5 + 4)* 3表达式值。
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:
递归函数是使用具有递归结构的数据的直接方法。 例如,下面是一个计算算术表达式的函数:
完整的代码: (Complete Code:)
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.
我相信这很简单。 但是,如果您需要任何澄清或问题,请在下面给我留言,我将为您解答。 谢谢阅读。
If you like what you’ve read today you can check out my other article on Explained Enum in Seven Steps
如果您喜欢今天阅读的内容,可以 通过七个步骤 查看我的另 一篇 关于 解释枚举的 文章
Thanks for reading.
谢谢阅读。
翻译自: https://levelup.gitconnected.com/swift-understand-recursive-enum-in-five-minutes-d4aff8bd50bb
swift 递归枚举