Advertisement
HXXXXJ

Binary expression - string out

Apr 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.02 KB | None | 0 0
  1.  
  2.  
  3. indirect enum CalcNode <T >{
  4.     typealias function = (T, T) -> T
  5.     case node(CalcNode <T>, CalcNode <T>, function)
  6.     case leaf(T)
  7.     func MyValue() -> T{
  8.         switch self {
  9.         case let .node(left, right, oper):
  10.             let leftR = left.MyValue()
  11.             let rightR = right.MyValue()
  12.             return oper(leftR, rightR)
  13.         case let .leaf(value):
  14.             return value
  15.         }
  16.     }
  17. }
  18.  
  19.  
  20. //let add = { ( a1, a2) -> Int in
  21. //    return a1 + a2
  22. //}
  23. //
  24. //let minus = { ( a1, a2) -> Int in
  25. //    return a1 - a2
  26. //}
  27. //let multi = { ( a1, a2) -> Int in
  28. //    return a1 * a2
  29. //}
  30.  
  31. let add = { ( a1, a2) -> String in
  32.     return a1 + a2 + "+"
  33. }
  34.  
  35. let minus = { ( a1, a2) -> String in
  36.     return a1 + a2 + "-"
  37. }
  38. let multi = { ( a1, a2) -> String in
  39.     return a1 + a2 + "*"
  40. }
  41.  
  42.  
  43.  
  44. let node1 = CalcNode.leaf("a")
  45. let node2 = CalcNode.leaf("4")
  46. let node3 = CalcNode.node(node1, node2, add)
  47. let node5 = CalcNode.leaf("5")
  48. let node4 = CalcNode.node(node3, node5, multi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement