Skip to content

Instantly share code, notes, and snippets.

@fredeil
Created July 3, 2019 13:06
Show Gist options
  • Save fredeil/0064f3127b109f07e741767208840c3a to your computer and use it in GitHub Desktop.
Save fredeil/0064f3127b109f07e741767208840c3a to your computer and use it in GitHub Desktop.
module Kalkis
open System.Threading.Tasks
type Operation = Add | Sub | Mul | Div | Pow
and Calculator =
| Value of double
| Expr of Operation * Calculator * Calculator
let spawn (op:unit->double) = Task.Run(op)
let rec eval expr =
match expr with
| Value(value) -> value
| Expr(op, lExpr, rExpr) ->
let op1 = spawn(fun () -> eval lExpr)
let op2 = spawn(fun () -> eval rExpr)
let apply = Task.WhenAll([op1;op2])
let lRes, rRes = apply.Result.[0], apply.Result.[1]
match op with
| Add -> lRes + rRes
| Sub -> lRes - rRes
| Mul -> lRes * rRes
| Div -> lRes / rRes
| Pow -> System.Math.Pow(lRes, rRes)
let operations = // 2^10 / 2^9 + 2*2
Expr(Add,
Expr(Div,
Expr(Pow, Value(2.0), Value(10.0)),
Expr(Pow, Value(2.0), Value(9.0))),
Expr(Mul, Value(2.0), Value(2.0)))
let value = eval operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment