tweak: Intermediary step while figuring out

function application
This commit is contained in:
NunoSempere 2022-09-04 18:53:57 +02:00
parent 803231d620
commit 85fe4d16a2

View File

@ -44,11 +44,52 @@ module ThreeNumbersToNumber = {
)
}
module FunctionToNumberZero = {
let make = (name, fn) =>
FnDefinition.make(
~name,
~inputs=[FRTypeLambda],
~run=(_, inputs, _, _) => {
Ok(0.0)->E.R2.fmap(Wrappers.evNumber)
},
(),
)
}
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)
let applyFun = (element, lambda, environment, reducer) => {
let rNewElem = Reducer_Expression_Lambda.doLambdaCall(
lambda,
element,
environment,
reducer,
)
}
let map = (array: array<internalExpressionValue>, environment, eLambdaValue, reducer): result<
ReducerInterface_InternalExpressionValue.t,
Reducer_ErrorValue.errorValue,
> => {
let x = array[0]
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)
}
}
let library = [
@ -92,4 +133,34 @@ let library = [
~definitions=[ThreeNumbersToNumber.make("binomial", Internals.binomial)],
(),
),
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",
~inputs=[FRTypeArray(FRTypeAny), FRTypeLambda],
~run=(inputs, _, env, reducer) =>
switch inputs {
| [IEvArray(array), IEvLambda(lambda)] =>
Internals.map(array, env, lambda, reducer)->E.R2.errMap(_ => "Error!")
| _ => Error(impossibleError)
},
(),
),
],
(),
),
]