Advertisement
HXXXXJ

Binary expression - Not compile

Apr 12th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.28 KB | None | 0 0
  1.  
  2. protocol BinaryFunction {
  3.     associatedtype T
  4.     typealias function = (T, T) -> T
  5.     static func process(_ operator: binaryOperator) -> function
  6. }
  7.  
  8.  
  9.  
  10. extension Int : BinaryFunction{
  11.     typealias T = Int
  12.     static func process(_ oper: binaryOperator) -> (Int, Int) -> Int {
  13.         var res : function
  14.         switch oper {
  15.             case .add:
  16.                 res = {( a1, a2) -> Int in
  17.                     return a1 + a2
  18.                 }
  19.         case .minus:
  20.             res = {( a1, a2) -> Int in
  21.                 return a1 - a2
  22.             }
  23.         case .multi:
  24.             res = {( a1, a2) -> Int in
  25.                 return a1 * a2
  26.             }
  27.        
  28.         }
  29.         return res
  30.     }
  31. }
  32.  
  33. enum binaryOperator{
  34.     case add
  35.     case minus
  36.     case multi
  37. }
  38.  
  39.  
  40.  
  41.  
  42. indirect enum CalcNode <T : BinaryFunction>{
  43.     typealias fuction = (T, T) -> T
  44.     case node(CalcNode <T>, CalcNode <T>, binaryOperator)
  45.     case leaf(T)
  46.     func MyValue() -> T{
  47.         switch self {
  48.         case let .node(left, right, oper):
  49.             let leftR = left.MyValue()
  50.             let rightR = right.MyValue()
  51.             let equation = T.process(oper)
  52.             return equation(leftR, rightR)
  53.         case let .leaf(value):
  54.             return value
  55.         }
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement