Last active
July 9, 2017 19:02
-
-
Save nomisRev/88961508c21a786047d0de7abbb0d3f0 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
sealed class MathExpression { | |
fun eval(): Result = when (this) { | |
is Addition -> left.eval().flatMap { ll -> right.eval().map { rr -> ll + rr } } | |
is Subtraction -> left.eval().flatMap { ll -> right.eval().map { rr -> ll - rr } } | |
is Division -> right.eval().flatMap { rr -> if (rr == 0) Failure("Division by zero") else left.eval().map { ll -> ll/rr }} | |
is Number -> Success(value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment