Cleaned up resultStringToResultError

This commit is contained in:
Ozzie Gooen 2022-04-09 20:34:21 -04:00
parent f17a842c52
commit db104694fd
2 changed files with 18 additions and 6 deletions

View File

@ -5,15 +5,14 @@ type toPointSetFn = t => result<PointSetTypes.pointSetDist, error>
type toSampleSetFn = t => result<SampleSetDist.t, error> type toSampleSetFn = t => result<SampleSetDist.t, error>
type scaleMultiplyFn = (t, float) => result<t, error> type scaleMultiplyFn = (t, float) => result<t, error>
type pointwiseAddFn = (t, t) => result<t, error> type pointwiseAddFn = (t, t) => result<t, error>
let mapStringErrors = n => n->E.R2.errMap(r => Error(GenericDist_Types.Other(r)))
let sampleN = (t: t, n) => let sampleN = (t: t, n) =>
switch t { switch t {
| PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r)) | PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r))
| Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r)) | Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r))
| SampleSet(r) => Ok(SampleSetDist.sampleN(r, n)) | SampleSet(r) => Ok(SampleSetDist.sampleN(r, n))
} }
let toSampleSetDist = (t: t, n) => sampleN(t, n)->E.R.bind(SampleSetDist.make)->mapStringErrors let toSampleSetDist = (t: t, n) =>
let mapStringErrors = n => n->E.R2.errMap(r => Error(GenericDist_Types.Other(r))) sampleN(t, n)->E.R.bind(SampleSetDist.make)->GenericDist_Types.Error.resultStringToResultError
let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f)) let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f))
@ -74,7 +73,7 @@ let toPointSet = (
kernelWidth: None, kernelWidth: None,
}, },
(), (),
)->mapStringErrors )->GenericDist_Types.Error.resultStringToResultError
} }
} }
@ -88,7 +87,7 @@ let toSparkline = (t: t, ~sampleCount: int, ~bucketCount: int=20, unit): result<
t t
->toPointSet(~xSelection=#Linear, ~xyPointLength=bucketCount * 3, ~sampleCount, ()) ->toPointSet(~xSelection=#Linear, ~xyPointLength=bucketCount * 3, ~sampleCount, ())
->E.R.bind(r => ->E.R.bind(r =>
r->PointSetDist.toSparkline(bucketCount)->E.R2.errMap(r => Error(GenericDist_Types.Other(r))) r->PointSetDist.toSparkline(bucketCount)->GenericDist_Types.Error.resultStringToResultError
) )
module Truncate = { module Truncate = {
@ -165,7 +164,11 @@ module AlgebraicCombination = {
) => { ) => {
let arithmeticOperation = Operation.Algebraic.toFn(arithmeticOperation) let arithmeticOperation = Operation.Algebraic.toFn(arithmeticOperation)
E.R.merge(toSampleSet(t1), toSampleSet(t2))->E.R.bind(((a, b)) => { E.R.merge(toSampleSet(t1), toSampleSet(t2))->E.R.bind(((a, b)) => {
SampleSetDist.map2(~fn=arithmeticOperation, ~t1=a, ~t2=b)->mapStringErrors SampleSetDist.map2(
~fn=arithmeticOperation,
~t1=a,
~t2=b,
)->GenericDist_Types.Error.resultStringToResultError
}) })
} }

View File

@ -10,6 +10,15 @@ type error =
| DistributionVerticalShiftIsInvalid | DistributionVerticalShiftIsInvalid
| Other(string) | Other(string)
module Error = {
type t = error
let fromString = (s: string): t => Other(s)
let resultStringToResultError: result<'a, string> => result<'a, error> = n =>
n->E.R2.errMap(r => r->fromString->Error)
}
module Operation = { module Operation = {
type direction = type direction =
| Algebraic | Algebraic