Cleanup to samplesMap() code

This commit is contained in:
Ozzie Gooen 2022-05-16 20:11:38 -04:00
parent 606f24ff24
commit eafdfdc7b7
4 changed files with 15 additions and 10 deletions

View File

@ -37,6 +37,7 @@ module Error = {
| LogarithmOfDistributionError(s) => `Logarithm of input error: ${s}` | LogarithmOfDistributionError(s) => `Logarithm of input error: ${s}`
| SampleSetError(TooFewSamples) => "Too Few Samples" | SampleSetError(TooFewSamples) => "Too Few Samples"
| SampleSetError(NonNumericInput(err)) => `Found a non-number in input: ${err}` | SampleSetError(NonNumericInput(err)) => `Found a non-number in input: ${err}`
| SampleSetError(OperationError(err)) => Operation.Error.toString(err)
| OperationError(err) => Operation.Error.toString(err) | OperationError(err) => Operation.Error.toString(err)
| PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err) | PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err)
| SparklineError(err) => PointSetTypes.sparklineErrorToString(err) | SparklineError(err) => PointSetTypes.sparklineErrorToString(err)

View File

@ -1,12 +1,14 @@
@genType @genType
module Error = { module Error = {
@genType @genType
type sampleSetError = TooFewSamples | NonNumericInput(string) type sampleSetError =
TooFewSamples | NonNumericInput(string) | OperationError(Operation.operationError)
let sampleSetErrorToString = (err: sampleSetError): string => let sampleSetErrorToString = (err: sampleSetError): string =>
switch err { switch err {
| TooFewSamples => "Too few samples when constructing sample set" | TooFewSamples => "Too few samples when constructing sample set"
| NonNumericInput(err) => `Found a non-number in input: ${err}` | NonNumericInput(err) => `Found a non-number in input: ${err}`
| OperationError(err) => Operation.Error.toString(err)
} }
@genType @genType
@ -16,6 +18,8 @@ module Error = {
switch err { switch err {
| TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set" | TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set"
} }
let fromOperationError = e => OperationError(e)
} }
include Error include Error
@ -85,13 +89,10 @@ 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, t,
Operation.Error.t, sampleSetError,
> => { > => {
let samples = T.get(t)->E.A2.fmap(fn) let samples = T.get(t)->E.A2.fmap(fn)
E.A.R.firstErrorOrOpen(samples)->E.R2.errMap(Error.fromOperationError) |> E.R2.bind(make)
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.
@ -107,7 +108,7 @@ let map2 = (~fn: (float, float) => result<float, Operation.Error.t>, ~t1: t, ~t2
// I could prove this to the type system (say, creating a {first: float, second: float, ..., fifth: float, rest: array<float>} // I could prove this to the type system (say, creating a {first: float, second: float, ..., fifth: float, rest: array<float>}
// But doing so would take too much time, so I'll leave it as an assertion // But doing so would take too much time, so I'll leave it as an assertion
E.A.R.firstErrorOrOpen(samples)->E.R2.fmap(x => E.A.R.firstErrorOrOpen(samples)->E.R2.fmap(x =>
E.R.toExn("Input of samples should be larger than 5", make(x)) E.R.toExnFnString(Error.sampleSetErrorToString, make(x))
) )
} }

View File

@ -107,7 +107,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
} }
switch SampleSetDist.samplesMap(~fn, sampleSetDist) { switch SampleSetDist.samplesMap(~fn, sampleSetDist) {
| Ok(r) => Ok(EvDistribution(SampleSet(r))) | Ok(r) => Ok(EvDistribution(SampleSet(r)))
| Error(r) => Error(REOperationError(r)) | Error(r) => Error(REDistributionError(SampleSetError(r)))
} }
} }

View File

@ -235,13 +235,16 @@ module R = {
| Ok(a) => f(a) | Ok(a) => f(a)
| Error(err) => Error(err) | Error(err) => Error(err)
} }
let toExn = (msg: string, x: result<'a, 'b>): 'a => let toExn = (msg: string, x: result<'a, 'b>): 'a =>
switch x { switch x {
| Ok(r) => r | Ok(r) => r
| Error(_) => raise(Assertion(msg)) | Error(_) => raise(Assertion(msg))
} }
let toExnFnString = (errorToStringFn, o) =>
switch o {
| Ok(r) => r
| Error(r) => raise(Assertion(errorToStringFn(r)))
}
let default = (default, res: Belt.Result.t<'a, 'b>) => let default = (default, res: Belt.Result.t<'a, 'b>) =>
switch res { switch res {
| Ok(r) => r | Ok(r) => r