Skip to content

Instantly share code, notes, and snippets.

@twof
Created April 9, 2019 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twof/677b93e38e37933c6faec61feb339614 to your computer and use it in GitHub Desktop.
Save twof/677b93e38e37933c6faec61feb339614 to your computer and use it in GitHub Desktop.
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