Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- indirect enum CalcNode <T>{
- typealias fuction = (T, T) -> T
- case node(CalcNode <T>, CalcNode <T>, fuction)
- case leaf(T)
- func MyValue() -> T{
- switch self {
- case let .node(left, right, function):
- let leftR = left.MyValue()
- let rightR = right.MyValue()
- return function(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 node1 = CalcNode.leaf(3)
- let node2 = CalcNode.leaf(4)
- let node3 = CalcNode.node(node1, node2, add)
- print(node3.MyValue())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement