Created
May 23, 2016 13:35
-
-
Save oleksii-demedetskyi/db226193b1a32af74f2073294f3a3f1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //: Playground - noun: a place where people can play | |
| import Cocoa | |
| // Operations | |
| protocol Stringable { | |
| func stringify() -> String | |
| } | |
| protocol Evaluatable { | |
| func evaluate() -> Double | |
| } | |
| // Types | |
| protocol Expr {} | |
| struct Constant: Expr { | |
| let value: Double | |
| } | |
| struct BinaryPlus<LH: Expr, RH: Expr>: Expr { | |
| let lh: LH, rh: RH | |
| } | |
| // Add implementations | |
| extension Constant: Evaluatable { | |
| func evaluate() -> Double { | |
| return value | |
| } | |
| } | |
| extension Constant: Stringable { | |
| func stringify() -> String { | |
| return String(value) | |
| } | |
| } | |
| extension BinaryPlus: Stringable where LH: Stringable, RH: Stringable { | |
| func stringify() -> String { | |
| return "\(lh.stringify()) + \(rh.stringify())" | |
| } | |
| } | |
| extension BinaryPlus: Evaluatable where LH: Evaluatable, RH: Evaluatable { | |
| func evaluate() -> Double { | |
| return lh.evaluate() + rh.evaluate() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment