First simple version of samplesMap

This commit is contained in:
Ozzie Gooen 2022-05-15 17:24:54 -04:00
parent 80fed66efe
commit 15965b0b05
2 changed files with 26 additions and 0 deletions

View File

@ -83,6 +83,17 @@ let sampleN = (t: t, n) => {
} }
} }
let samplesMap = (~fn: (float) => result<float, Operation.Error.t>, t: t): result<
t,
Operation.Error.t,
> => {
let samples = T.get(t)->E.A2.fmap(fn)
E.A.R.firstErrorOrOpen(samples)->E.R2.fmap(x =>
E.R.toExn("Input of samples should be larger than 5", make(x))
)
}
//TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this. //TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this.
let map2 = (~fn: (float, float) => result<float, Operation.Error.t>, ~t1: t, ~t2: t): result< let map2 = (~fn: (float, float) => result<float, Operation.Error.t>, ~t1: t, ~t2: t): result<
t, t,

View File

@ -99,6 +99,19 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
rMappedList->Result.map(mappedList => mappedList->Belt.List.toArray->EvArray) rMappedList->Result.map(mappedList => mappedList->Belt.List.toArray->EvArray)
} }
let doMapSampleSetDist = (sampleSetDist: SampleSetDist.t, aLambdaValue) => {
let fn = r =>
switch Lambda.doLambdaCall(aLambdaValue, list{EvNumber(r)}, environment, reducer) {
| Ok(EvNumber(f)) => Ok(f)
| _ => Error(Operation.NotYetImplemented)
}
let newDist = SampleSetDist.samplesMap(~fn, sampleSetDist)
switch newDist {
| Ok(r) => Ok(EvDistribution(SampleSet(r)))
| Error(r) => Error(RETodo(""))
}
}
let doReduceArray = (aValueArray, initialValue, aLambdaValue) => { let doReduceArray = (aValueArray, initialValue, aLambdaValue) => {
aValueArray->Belt.Array.reduce(Ok(initialValue), (rAcc, elem) => aValueArray->Belt.Array.reduce(Ok(initialValue), (rAcc, elem) =>
rAcc->Result.flatMap(acc => rAcc->Result.flatMap(acc =>
@ -128,6 +141,8 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
| ("keep", [EvArray(aValueArray), EvLambda(aLambdaValue)]) => | ("keep", [EvArray(aValueArray), EvLambda(aLambdaValue)]) =>
doKeepArray(aValueArray, aLambdaValue) doKeepArray(aValueArray, aLambdaValue)
| ("map", [EvArray(aValueArray), EvLambda(aLambdaValue)]) => doMapArray(aValueArray, aLambdaValue) | ("map", [EvArray(aValueArray), EvLambda(aLambdaValue)]) => doMapArray(aValueArray, aLambdaValue)
| ("mapSamples", [EvDistribution(SampleSet(dist)), EvLambda(aLambdaValue)]) =>
doMapSampleSetDist(dist, aLambdaValue)
| ("reduce", [EvArray(aValueArray), initialValue, EvLambda(aLambdaValue)]) => | ("reduce", [EvArray(aValueArray), initialValue, EvLambda(aLambdaValue)]) =>
doReduceArray(aValueArray, initialValue, aLambdaValue) doReduceArray(aValueArray, initialValue, aLambdaValue)
| ("reduceReverse", [EvArray(aValueArray), initialValue, EvLambda(aLambdaValue)]) => | ("reduceReverse", [EvArray(aValueArray), initialValue, EvLambda(aLambdaValue)]) =>