Skip to content

Instantly share code, notes, and snippets.

@lasandell
Last active August 29, 2015 14:23
Show Gist options
  • Save lasandell/378ccb4802ece21153b2 to your computer and use it in GitHub Desktop.
Save lasandell/378ccb4802ece21153b2 to your computer and use it in GitHub Desktop.
F# RPN Calculator Computation Expression Builder!
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