Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- indirect enum CalcNode <T >{
- typealias function = (T, T) -> T
- case node(CalcNode <T>, CalcNode <T>, function)
- case leaf(T)
- func MyValue() -> T{
- switch self {
- case let .node(left, right, oper):
- let leftR = left.MyValue()
- let rightR = right.MyValue()
- return oper(leftR, rightR)
- case let .leaf(value):
- return value
- }
- }
- }
- //let add = { ( a1, a2) -> Int in
- // return a1 + a2
- //}
- //
- //let minus = { ( a1, a2) -> Int in
- // return a1 - a2
- //}
- //let multi = { ( a1, a2) -> Int in
- // return a1 * a2
- //}
- let add = { ( a1, a2) -> String in
- return a1 + a2 + "+"
- }
- let minus = { ( a1, a2) -> String in
- return a1 + a2 + "-"
- }
- let multi = { ( a1, a2) -> String in
- return a1 + a2 + "*"
- }
- let node1 = CalcNode.leaf("a")
- let node2 = CalcNode.leaf("4")
- let node3 = CalcNode.node(node1, node2, add)
- let node5 = CalcNode.leaf("5")
- let node4 = CalcNode.node(node3, node5, multi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement