Move error types to types modules
This commit is contained in:
parent
79af95ed78
commit
ad593e659b
|
@ -88,10 +88,7 @@ describe("eval on distribution functions", () => {
|
|||
|
||||
describe("log", () => {
|
||||
testEval("log(2, uniform(5,8))", "Ok(Sample Set Distribution)")
|
||||
testEval(
|
||||
"log(normal(5,2), 3)",
|
||||
"Error(Math Error: Operation returned complex result)",
|
||||
)
|
||||
testEval("log(normal(5,2), 3)", "Error(Math Error: Operation returned complex result)")
|
||||
testEval(
|
||||
"log(normal(5,2), normal(10,1))",
|
||||
"Error(Math Error: Operation returned complex result)",
|
||||
|
|
|
@ -11,16 +11,11 @@ type error =
|
|||
| DistributionVerticalShiftIsInvalid
|
||||
| TooFewSamples
|
||||
| ArgumentError(string)
|
||||
| OperationError(Operation.invalidOperationError)
|
||||
| OperationError(Operation.Error.invalidOperationError)
|
||||
| PointSetConversionError(SampleSetDist.pointsetConversionError)
|
||||
| SparklineError(PointSetDist.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented
|
||||
| SparklineError(PointSetTypes.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented
|
||||
| Other(string)
|
||||
|
||||
let sampleErrorToDistErr = (err: SampleSetDist.sampleSetError): error =>
|
||||
switch err {
|
||||
| TooFewSamples => TooFewSamples
|
||||
}
|
||||
|
||||
@genType
|
||||
module Error = {
|
||||
type t = error
|
||||
|
@ -35,14 +30,19 @@ module Error = {
|
|||
| DistributionVerticalShiftIsInvalid => "Distribution Vertical Shift is Invalid"
|
||||
| ArgumentError(s) => `Argument Error ${s}`
|
||||
| TooFewSamples => "Too Few Samples"
|
||||
| OperationError(err) => Operation.invalidOperationErrorToString(err)
|
||||
| OperationError(err) => Operation.Error.invalidOperationErrorToString(err)
|
||||
| PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err)
|
||||
| SparklineError(err) => PointSetDist.sparklineErrorToString(err)
|
||||
| SparklineError(err) => PointSetTypes.sparklineErrorToString(err)
|
||||
| Other(s) => s
|
||||
}
|
||||
|
||||
let resultStringToResultError: result<'a, string> => result<'a, error> = n =>
|
||||
n->E.R2.errMap(r => r->fromString)
|
||||
|
||||
let sampleErrorToDistErr = (err: SampleSetDist.sampleSetError): error =>
|
||||
switch err {
|
||||
| TooFewSamples => TooFewSamples
|
||||
}
|
||||
}
|
||||
|
||||
@genType
|
||||
|
|
|
@ -14,7 +14,7 @@ let sampleN = (t: t, n) =>
|
|||
}
|
||||
|
||||
let toSampleSetDist = (t: t, n) =>
|
||||
SampleSetDist.make(sampleN(t, n))->E.R2.errMap(DistributionTypes.sampleErrorToDistErr)
|
||||
SampleSetDist.make(sampleN(t, n))->E.R2.errMap(DistributionTypes.Error.sampleErrorToDistErr)
|
||||
|
||||
let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f))
|
||||
|
||||
|
@ -151,7 +151,7 @@ module AlgebraicCombination = {
|
|||
arithmeticOperation: DistributionTypes.Operation.arithmeticOperation,
|
||||
t1: t,
|
||||
t2: t,
|
||||
): option<result<SymbolicDistTypes.symbolicDist, Operation.invalidOperationError>> =>
|
||||
): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.invalidOperationError>> =>
|
||||
switch (arithmeticOperation, t1, t2) {
|
||||
| (arithmeticOperation, Symbolic(d1), Symbolic(d2)) =>
|
||||
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) {
|
||||
|
|
|
@ -215,16 +215,8 @@ let operate = (distToFloatOp: Operation.distToFloatOperation, s): float =>
|
|||
| #Mean => T.mean(s)
|
||||
}
|
||||
|
||||
@genType
|
||||
type sparklineError = CannotSparklineDiscrete
|
||||
|
||||
let sparklineErrorToString = (err: sparklineError): string =>
|
||||
switch err {
|
||||
| CannotSparklineDiscrete => "Cannot find the sparkline of a discrete distribution"
|
||||
}
|
||||
|
||||
let toSparkline = (t: t, bucketCount): result<string, sparklineError> =>
|
||||
let toSparkline = (t: t, bucketCount): result<string, PointSetTypes.sparklineError> =>
|
||||
T.toContinuous(t)
|
||||
->E.O2.fmap(Continuous.downsampleEquallyOverX(bucketCount))
|
||||
->E.O2.toResult(CannotSparklineDiscrete)
|
||||
->E.O2.toResult(PointSetTypes.CannotSparklineDiscrete)
|
||||
->E.R2.fmap(r => Continuous.getShape(r).ys->Sparklines.create())
|
||||
|
|
|
@ -94,3 +94,11 @@ module MixedPoint = {
|
|||
|
||||
let add = combine2((a, b) => a +. b)
|
||||
}
|
||||
|
||||
@genType
|
||||
type sparklineError = CannotSparklineDiscrete
|
||||
|
||||
let sparklineErrorToString = (err: sparklineError): string =>
|
||||
switch err {
|
||||
| CannotSparklineDiscrete => "Cannot find the sparkline of a discrete distribution"
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
@genType
|
||||
module Error = {
|
||||
@genType
|
||||
type sampleSetError = TooFewSamples
|
||||
|
||||
|
@ -6,6 +8,17 @@ let sampleSetErrorToString = (err: sampleSetError): string =>
|
|||
| TooFewSamples => "Too few samples when constructing sample set"
|
||||
}
|
||||
|
||||
@genType
|
||||
type pointsetConversionError = TooFewSamplesForConversionToPointSet
|
||||
|
||||
let pointsetConversionErrorToString = (err: pointsetConversionError) =>
|
||||
switch err {
|
||||
| TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set"
|
||||
}
|
||||
}
|
||||
|
||||
include Error
|
||||
|
||||
/*
|
||||
This is used as a smart constructor. The only way to create a SampleSetDist.t is to call
|
||||
this constructor.
|
||||
|
@ -33,14 +46,6 @@ include T
|
|||
|
||||
let length = (t: t) => get(t)->E.A.length
|
||||
|
||||
@genType
|
||||
type pointsetConversionError = TooFewSamplesForConversionToPointSet
|
||||
|
||||
let pointsetConversionErrorToString = (err: pointsetConversionError) =>
|
||||
switch err {
|
||||
| TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set"
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: Refactor to get a more precise estimate. Also, this code is just fairly messy, could use
|
||||
some refactoring.
|
||||
|
@ -79,10 +84,10 @@ let sampleN = (t: t, n) => {
|
|||
|
||||
//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.invalidOperationError>,
|
||||
~fn: (float, float) => result<float, Operation.Error.invalidOperationError>,
|
||||
~t1: t,
|
||||
~t2: t,
|
||||
): result<t, Operation.invalidOperationError> => {
|
||||
): result<t, Operation.Error.invalidOperationError> => {
|
||||
let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b))
|
||||
|
||||
// This assertion should never be reached. In order for it to be reached, one
|
||||
|
|
|
@ -45,6 +45,6 @@ type symbolicDist = [
|
|||
|
||||
type analyticalSimplificationResult = [
|
||||
| #AnalyticalSolution(symbolicDist)
|
||||
| #Error(Operation.invalidOperationError)
|
||||
| #Error(Operation.Error.invalidOperationError)
|
||||
| #NoSolution
|
||||
]
|
||||
|
|
|
@ -37,6 +37,8 @@ module Convolution = {
|
|||
}
|
||||
}
|
||||
|
||||
@genType
|
||||
module Error = {
|
||||
@genType
|
||||
type invalidOperationError =
|
||||
| DivisionByZeroError
|
||||
|
@ -47,10 +49,11 @@ let invalidOperationErrorToString = (err: invalidOperationError): string =>
|
|||
| DivisionByZeroError => "Cannot divide by zero"
|
||||
| ComplexNumberError => "Operation returned complex result"
|
||||
}
|
||||
}
|
||||
|
||||
module Algebraic = {
|
||||
type t = algebraicOperation
|
||||
let toFn: (t, float, float) => result<float, invalidOperationError> = (x, a, b) =>
|
||||
let toFn: (t, float, float) => result<float, Error.invalidOperationError> = (x, a, b) =>
|
||||
switch x {
|
||||
| #Add => Ok(a +. b)
|
||||
| #Subtract => Ok(a -. b)
|
||||
|
@ -70,8 +73,7 @@ module Algebraic = {
|
|||
| #Logarithm =>
|
||||
if b == 1. {
|
||||
Error(DivisionByZeroError)
|
||||
}
|
||||
else if a > 0.0 && b > 0.0 {
|
||||
} else if a > 0.0 && b > 0.0 {
|
||||
Ok(log(a) /. log(b))
|
||||
} else {
|
||||
Error(ComplexNumberError)
|
||||
|
@ -119,7 +121,7 @@ module DistToFloat = {
|
|||
// Note that different logarithms don't really do anything.
|
||||
module Scale = {
|
||||
type t = scaleOperation
|
||||
let toFn = (x: t, a: float, b: float): result<float, invalidOperationError> =>
|
||||
let toFn = (x: t, a: float, b: float): result<float, Error.invalidOperationError> =>
|
||||
switch x {
|
||||
| #Multiply => Ok(a *. b)
|
||||
| #Divide =>
|
||||
|
|
Loading…
Reference in New Issue
Block a user