Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol BinaryFunction {
- associatedtype T
- typealias function = (T, T) -> T
- static func process(_ operator: binaryOperator) -> function
- }
- extension Int : BinaryFunction{
- typealias T = Int
- static func process(_ oper: binaryOperator) -> (Int, Int) -> Int {
- var res : function
- switch oper {
- case .add:
- res = {( a1, a2) -> Int in
- return a1 + a2
- }
- case .minus:
- res = {( a1, a2) -> Int in
- return a1 - a2
- }
- case .multi:
- res = {( a1, a2) -> Int in
- return a1 * a2
- }
- }
- return res
- }
- }
- enum binaryOperator{
- case add
- case minus
- case multi
- }
- indirect enum CalcNode <T : BinaryFunction>{
- typealias fuction = (T, T) -> T
- case node(CalcNode <T>, CalcNode <T>, binaryOperator)
- case leaf(T)
- func MyValue() -> T{
- switch self {
- case let .node(left, right, oper):
- let leftR = left.MyValue()
- let rightR = right.MyValue()
- let equation = T.process(oper)
- return equation(leftR, rightR)
- case let .leaf(value):
- return value
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement