2022-09-04 13:21:07 +00:00
|
|
|
open FunctionRegistry_Core
|
|
|
|
open FunctionRegistry_Helpers
|
|
|
|
|
|
|
|
let nameSpace = "Danger"
|
|
|
|
let requiresNamespace = true
|
|
|
|
|
2022-09-04 15:12:18 +00:00
|
|
|
module NumberToNumber = {
|
|
|
|
let make = (name, fn) =>
|
|
|
|
FnDefinition.make(
|
|
|
|
~name,
|
|
|
|
~inputs=[FRTypeNumber],
|
|
|
|
~run=(_, inputs, _, _) => {
|
|
|
|
inputs
|
|
|
|
->getOrError(0)
|
|
|
|
->E.R.bind(Prepare.oneNumber)
|
|
|
|
->E.R2.fmap(fn)
|
|
|
|
->E.R2.fmap(Wrappers.evNumber)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-04 13:21:07 +00:00
|
|
|
module TwoNumbersToNumber = {
|
|
|
|
let make = (name, fn) =>
|
|
|
|
FnDefinition.make(
|
|
|
|
~name,
|
|
|
|
~inputs=[FRTypeNumber, FRTypeNumber],
|
|
|
|
~run=(_, inputs, _, _) => {
|
|
|
|
inputs->Prepare.ToValueTuple.twoNumbers->E.R2.fmap(fn)->E.R2.fmap(Wrappers.evNumber)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
module ThreeNumbersToNumber = {
|
|
|
|
let make = (name, fn) =>
|
|
|
|
FnDefinition.make(
|
|
|
|
~name,
|
|
|
|
~inputs=[FRTypeNumber, FRTypeNumber, FRTypeNumber],
|
|
|
|
~run=(_, inputs, _, _) => {
|
|
|
|
inputs->Prepare.ToValueTuple.threeNumbers->E.R2.fmap(fn)->E.R2.fmap(Wrappers.evNumber)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-04 16:53:57 +00:00
|
|
|
module FunctionToNumberZero = {
|
|
|
|
let make = (name, fn) =>
|
|
|
|
FnDefinition.make(
|
|
|
|
~name,
|
|
|
|
~inputs=[FRTypeLambda],
|
|
|
|
~run=(_, inputs, _, _) => {
|
|
|
|
Ok(0.0)->E.R2.fmap(Wrappers.evNumber)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:12:18 +00:00
|
|
|
module Internals = {
|
|
|
|
let factorial = Stdlib.Math.factorial
|
|
|
|
let choose = ((n, k)) => factorial(n) /. (factorial(n -. k) *. factorial(k))
|
|
|
|
let pow = (base, exp) => Js.Math.pow_float(~base, ~exp)
|
|
|
|
let binomial = ((n, k, p)) => choose((n, k)) *. pow(p, k) *. pow(1.0 -. p, n -. k)
|
2022-09-04 17:04:13 +00:00
|
|
|
let map = (array: array<internalExpressionValue>, environment, eLambdaValue, reducer): result<
|
2022-09-04 16:53:57 +00:00
|
|
|
ReducerInterface_InternalExpressionValue.t,
|
|
|
|
Reducer_ErrorValue.errorValue,
|
|
|
|
> => {
|
2022-09-04 17:04:13 +00:00
|
|
|
let x = array[0]
|
2022-09-04 16:53:57 +00:00
|
|
|
let wrappedY = {
|
|
|
|
let result2 = Reducer_Expression_Lambda.doLambdaCall(
|
|
|
|
eLambdaValue,
|
|
|
|
list{x},
|
|
|
|
environment,
|
|
|
|
reducer,
|
|
|
|
)
|
|
|
|
let result3 = switch(result2){
|
|
|
|
| Ok(a) => Ok(list{a})
|
|
|
|
| Error(b) => Error(b)
|
|
|
|
}
|
|
|
|
result3
|
|
|
|
}
|
|
|
|
wrappedY->E.R2.fmap(mappedList => mappedList->Belt.List.toArray->Wrappers.evArray)
|
|
|
|
}
|
|
|
|
|
2022-09-04 15:12:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 13:21:07 +00:00
|
|
|
let library = [
|
|
|
|
Function.make(
|
|
|
|
~name="laplace",
|
|
|
|
~nameSpace,
|
|
|
|
~requiresNamespace,
|
|
|
|
~output=EvtNumber,
|
2022-09-04 15:12:18 +00:00
|
|
|
~examples=[`Danger.laplace(1, 20)`],
|
2022-09-04 13:21:07 +00:00
|
|
|
~definitions=[
|
|
|
|
TwoNumbersToNumber.make("laplace", ((successes, trials)) =>
|
2022-09-04 15:12:18 +00:00
|
|
|
(successes +. 1.0) /. (trials +. 2.0)
|
2022-09-04 13:21:07 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
(),
|
|
|
|
),
|
2022-09-04 15:12:18 +00:00
|
|
|
Function.make(
|
|
|
|
~name="factorial",
|
|
|
|
~nameSpace,
|
|
|
|
~requiresNamespace,
|
|
|
|
~output=EvtNumber,
|
|
|
|
~examples=[`Danger.factorial(20)`],
|
|
|
|
~definitions=[NumberToNumber.make("factorial", Internals.factorial)],
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
Function.make(
|
|
|
|
~name="choose",
|
|
|
|
~nameSpace,
|
|
|
|
~requiresNamespace,
|
|
|
|
~output=EvtNumber,
|
|
|
|
~examples=[`Danger.choose(1, 20)`],
|
|
|
|
~definitions=[TwoNumbersToNumber.make("choose", Internals.choose)],
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
Function.make(
|
|
|
|
~name="binomial",
|
|
|
|
~nameSpace,
|
|
|
|
~requiresNamespace,
|
|
|
|
~output=EvtNumber,
|
|
|
|
~examples=[`Danger.binomial(1, 20, 0.5)`],
|
|
|
|
~definitions=[ThreeNumbersToNumber.make("binomial", Internals.binomial)],
|
|
|
|
(),
|
|
|
|
),
|
2022-09-04 16:53:57 +00:00
|
|
|
Function.make(
|
|
|
|
~name="functionToZero",
|
|
|
|
~nameSpace,
|
|
|
|
~requiresNamespace,
|
|
|
|
~output=EvtNumber,
|
|
|
|
~examples=[`Danger.functionToZero({|x| x})`],
|
|
|
|
~definitions=[FunctionToNumberZero.make("functionToZero", x => x)],
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
Function.make(
|
|
|
|
~name="map",
|
|
|
|
~nameSpace,
|
|
|
|
~output=EvtArray,
|
|
|
|
~requiresNamespace=false,
|
|
|
|
~examples=[`List.map([1,4,5], {|x| x+1})`],
|
|
|
|
~definitions=[
|
|
|
|
FnDefinition.make(
|
|
|
|
~name="map",
|
2022-09-04 17:04:13 +00:00
|
|
|
~inputs=[FRTypeArray(FRTypeAny), FRTypeLambda],
|
2022-09-04 16:53:57 +00:00
|
|
|
~run=(inputs, _, env, reducer) =>
|
|
|
|
switch inputs {
|
2022-09-04 17:04:13 +00:00
|
|
|
| [IEvArray(array), IEvLambda(lambda)] =>
|
|
|
|
Internals.map(array, env, lambda, reducer)->E.R2.errMap(_ => "Error!")
|
2022-09-04 16:53:57 +00:00
|
|
|
| _ => Error(impossibleError)
|
|
|
|
},
|
|
|
|
(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
(),
|
|
|
|
),
|
2022-09-04 13:21:07 +00:00
|
|
|
]
|