Last active
August 29, 2015 14:23
-
-
Save lasandell/378ccb4802ece21153b2 to your computer and use it in GitHub Desktop.
F# RPN Calculator Computation Expression Builder!
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
type CalcBuilder() = | |
member __.Yield(()) = () | |
member __.Run((x, ())) = x | |
[<CustomOperation("push")>] | |
member __.Push(rest, x) = (x, rest) | |
[<CustomOperation("add")>] | |
member __.Add((y, (x, rest))) = (x + y, rest) | |
[<CustomOperation("sub")>] | |
member __.Subtract((y, (x, rest))) = (x - y, rest) | |
[<CustomOperation("mult")>] | |
member __.Multiply((y, (x, rest))) = (x * y, rest) | |
[<CustomOperation("div")>] | |
member __.Divide((y, (x, rest))) = (x / y, rest) | |
let calc = CalcBuilder() | |
// Returns 1 | |
calc { | |
push 1 | |
} | |
// Returns 9 | |
calc { | |
push 1 | |
push 2 | |
add | |
push 3 | |
mult | |
} | |
// Doesn't compile - more than one item on stack! | |
calc { | |
push 1 | |
push 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment