Created
April 9, 2019 05:09
-
-
Save twof/677b93e38e37933c6faec61feb339614 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Node { | |
associatedtype Value: Numeric | |
func execute() -> Value | |
} | |
protocol Operator: Node { | |
associatedtype LeftNode: Node | |
associatedtype RightNode: Node | |
var left: LeftNode { get } | |
var right: RightNode { get } | |
} | |
class PlusOper<Left: Node, Right: Node, ValueT>: Operator where | |
Left.Value == ValueT, | |
Right.Value == ValueT | |
{ | |
var left: Left | |
var right: Right | |
init(left: Left, right: Right) { | |
self.left = left | |
self.right = right | |
} | |
func execute() -> ValueT { | |
return self.left.execute() + self.right.execute() | |
} | |
} | |
class ValueNode<T: Numeric>: Node { | |
let value: T | |
init(val: T) { | |
self.value = val | |
} | |
func execute() -> T { | |
return self.value | |
} | |
} | |
let tree = PlusOper(left: ValueNode(val: 10), right: ValueNode(val: 20)) | |
print(tree.execute()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment