diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res index a69d7f5d..772e83f7 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist.res @@ -5,15 +5,14 @@ type toPointSetFn = t => result type toSampleSetFn = t => result type scaleMultiplyFn = (t, float) => result type pointwiseAddFn = (t, t) => result -let mapStringErrors = n => n->E.R2.errMap(r => Error(GenericDist_Types.Other(r))) let sampleN = (t: t, n) => switch t { | PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r)) | Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r)) | SampleSet(r) => Ok(SampleSetDist.sampleN(r, n)) } -let toSampleSetDist = (t: t, n) => sampleN(t, n)->E.R.bind(SampleSetDist.make)->mapStringErrors -let mapStringErrors = n => n->E.R2.errMap(r => Error(GenericDist_Types.Other(r))) +let toSampleSetDist = (t: t, n) => + sampleN(t, n)->E.R.bind(SampleSetDist.make)->GenericDist_Types.Error.resultStringToResultError let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f)) @@ -74,7 +73,7 @@ let toPointSet = ( kernelWidth: None, }, (), - )->mapStringErrors + )->GenericDist_Types.Error.resultStringToResultError } } @@ -88,7 +87,7 @@ let toSparkline = (t: t, ~sampleCount: int, ~bucketCount: int=20, unit): result< t ->toPointSet(~xSelection=#Linear, ~xyPointLength=bucketCount * 3, ~sampleCount, ()) ->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 = { @@ -165,7 +164,11 @@ module AlgebraicCombination = { ) => { let arithmeticOperation = Operation.Algebraic.toFn(arithmeticOperation) 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 }) } diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res index c3c923a4..96e7d3f8 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res @@ -10,6 +10,15 @@ type error = | DistributionVerticalShiftIsInvalid | 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 = { type direction = | Algebraic