Cleanup and addition of toInternalSampleArray

This commit is contained in:
Ozzie Gooen 2022-05-15 21:34:36 -04:00
parent 15965b0b05
commit 606f24ff24
6 changed files with 14 additions and 5 deletions

View File

@ -17,6 +17,10 @@ describe("builtin", () => {
testEval("1-1", "Ok(0)")
testEval("2>1", "Ok(true)")
testEval("concat('a','b')", "Ok('ab')")
testEval(
"addOne(t)=t+1; toInternalSampleArray(mapSamples(fromSamples([1,2,3,4,5,6]), addOne))",
"Ok([2,3,4,5,6,7])",
)
})
describe("builtin exception", () => {

View File

@ -83,7 +83,7 @@ let sampleN = (t: t, n) => {
}
}
let samplesMap = (~fn: (float) => result<float, Operation.Error.t>, t: t): result<
let samplesMap = (~fn: float => result<float, Operation.Error.t>, t: t): result<
t,
Operation.Error.t,
> => {

View File

@ -103,12 +103,11 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
let fn = r =>
switch Lambda.doLambdaCall(aLambdaValue, list{EvNumber(r)}, environment, reducer) {
| Ok(EvNumber(f)) => Ok(f)
| _ => Error(Operation.NotYetImplemented)
| _ => Error(Operation.SampleMapNeedsNtoNFunction)
}
let newDist = SampleSetDist.samplesMap(~fn, sampleSetDist)
switch newDist {
switch SampleSetDist.samplesMap(~fn, sampleSetDist) {
| Ok(r) => Ok(EvDistribution(SampleSet(r)))
| Error(r) => Error(RETodo(""))
| Error(r) => Error(REOperationError(r))
}
}

View File

@ -4,6 +4,7 @@ type errorValue =
| REArrayIndexNotFound(string, int)
| REAssignmentExpected
| REDistributionError(DistributionTypes.error)
| REOperationError(Operation.operationError)
| REExpressionExpected
| REFunctionExpected(string)
| REJavaScriptExn(option<string>, option<string>) // Javascript Exception
@ -29,6 +30,7 @@ let errorToString = err =>
| REExpressionExpected => "Expression expected"
| REFunctionExpected(msg) => `Function expected: ${msg}`
| REDistributionError(err) => `Distribution Math Error: ${DistributionTypes.Error.toString(err)}`
| REOperationError(err) => `Math Error: ${Operation.Error.toString(err)}`
| REJavaScriptExn(omsg, oname) => {
let answer = "JS Exception:"
let answer = switch oname {

View File

@ -262,6 +262,8 @@ let dispatchToGenericOutput = (
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist, ~env)
| ("toSampleSet", [EvDistribution(dist)]) =>
Helpers.toDistFn(ToSampleSet(env.sampleCount), dist, ~env)
| ("toInternalSampleArray", [EvDistribution(SampleSet(dist))]) =>
Some(FloatArray(SampleSetDist.T.get(dist)))
| ("fromSamples", [EvArray(inputArray)]) => {
let _wrapInputErrors = x => SampleSetDist.NonNumericInput(x)
let parsedArray = Helpers.parseNumberArray(inputArray)->E.R2.errMap(_wrapInputErrors)

View File

@ -56,6 +56,7 @@ type operationError =
| InfinityError
| NegativeInfinityError
| LogicallyInconsistentPathwayError
| SampleMapNeedsNtoNFunction
| NotYetImplemented // should be removed when `klDivergence` for mixed and discrete is implemented.
@genType
@ -70,6 +71,7 @@ module Error = {
| InfinityError => "Operation returned positive infinity"
| NegativeInfinityError => "Operation returned negative infinity"
| LogicallyInconsistentPathwayError => "This pathway should have been logically unreachable"
| SampleMapNeedsNtoNFunction => "SampleMap needs a function that converts a number to a number"
| NotYetImplemented => "This pathway is not yet implemented"
}
}