more builtin functions

This commit is contained in:
Vyacheslav Matyukhin 2022-09-13 03:51:49 +04:00
parent 36127d8947
commit 02c84983a4
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C

View File

@ -38,10 +38,39 @@ let makeBinaryFn = (name: string, fn: (float, float) => float) => {
)
}
let makeBinaryCmpFn = (name: string, fn: (float, float) => bool) => {
makeFn(
name,
[FRTypeNumber, FRTypeNumber],
inputs => {
switch inputs {
| [IEvNumber(x), IEvNumber(y)] => fn(x, y)->IEvBool->Ok
| _ => Error(impossibleError)
}
}
)
}
let library = [
// TODO - other MathJS
makeBinaryFn("add", (x, y) => x +. y),
makeBinaryFn("subtract", (x, y) => x -. y),
makeBinaryFn("multiply", (x, y) => x *. y),
makeBinaryFn("divide", (x, y) => x /. y),
makeBinaryFn("pow", (x, y) => Js.Math.pow_float(~base=x, ~exp=y)),
makeBinaryCmpFn("equal", (x, y) => x == y),
makeBinaryCmpFn("smaller", (x, y) => x < y),
makeBinaryCmpFn("smallerEq", (x, y) => x <= y),
makeBinaryCmpFn("larger", (x, y) => x > y),
makeBinaryCmpFn("largerEq", (x, y) => x >= y),
makeFn(
"unaryMinus",
[FRTypeNumber],
inputs => {
switch inputs {
| [IEvNumber(x)] => IEvNumber(-.x)->Ok
| _ => Error(impossibleError)
}
}
)
]