Refactored applyFnInternal

This commit is contained in:
Ozzie Gooen 2022-03-23 17:29:20 -04:00
parent 0ded4a404f
commit 540d035b90

View File

@ -1,13 +1,44 @@
type symboliDist = SymbolicDistTypes.symbolicDist type symboliDist = SymbolicDistTypes.symbolicDist
type error =
| NeedsPointSetConversion
| Other(string)
type genericDist = [ type genericDist = [
| #XYShape(PointSetTypes.pointSetDist) | #XYShape(PointSetTypes.pointSetDist)
| #SampleSet(array<float>) | #SampleSet(array<float>)
| #Symbolic(symboliDist) | #Symbolic(symboliDist)
| #Error(string) | #Error(error)
| #Float(float) | #Float(float)
] ]
type combination = [
| #Add
| #Multiply
| #Subtract
| #Divide
| #Exponentiate
]
type toFloat = [
| #Cdf(float)
| #Inv(float)
| #Mean
| #Pdf(float)
| #Sample
]
type toDist = [
| #normalize
| #toPointSet
]
type operation = [
| #toFloat(toFloat)
| #toDist(toDist)
| #toDistCombination(combination, genericDist)
]
type params = { type params = {
sampleCount: int, sampleCount: int,
xyPointLength: int, xyPointLength: int,
@ -33,28 +64,22 @@ let defaultSamplingInputs: SamplingInputs.samplingInputs = {
kernelWidth: None, kernelWidth: None,
} }
let distToFloat = (wrapped: wrapped, fnName) => { let applyFnInternal = (wrapped: wrapped, fnName: operation): wrapped => {
let (v, extra) = wrapped let (v, extra) = wrapped
let newVal = switch (fnName, v) { let newVal: genericDist = switch (fnName, v) {
| (operation, #XYShape(r)) => #Float(PointSetDist.operate(operation, r)) | (#toFloat(n), #XYShape(r)) => #Float(PointSetDist.operate(n, r))
| (operation, #Symbolic(r)) => switch(SymbolicDist.T.operate(operation, r)){ | (#toFloat(n), #Symbolic(r)) =>
| Ok(r) => #SymbolicDist(r) switch SymbolicDist.T.operate(n, r) {
| Error(r) => #Error(r) | Ok(float) => #Float(float)
| Error(e) => #Error(Other(e))
} }
| _ => #Error("No Match") | (#toFloat(n), #SampleSet(_)) => #Error(NeedsPointSetConversion)
} | (#toDist(#normalize), #XYShape(r)) => #XYShape(PointSetDist.T.normalize(r))
(newVal, extra) | (#toDist(#normalize), #Symbolic(_)) => v
} | (#toDist(#normalize), #SampleSet(_)) => v
| (#toDist(#toPointSet), #XYShape(_)) => v
let distToDist = (wrapped: wrapped, fnName): wrapped => { | (#toDist(#toPointSet), #Symbolic(r)) => #XYShape(SymbolicDist.T.toPointSetDist(1000, r))
let (v, extra) = wrapped | (#toDist(#toPointSet), #SampleSet(r)) => {
let newVal = switch (fnName, v) {
| (#normalize, #XYShape(r)) => #XYShape(PointSetDist.T.normalize(r))
| (#normalize, #Symbolic(_)) => v
| (#normalize, #SampleSet(_)) => v
| (#toPointSet, #XYShape(_)) => v
| (#toPointSet, #Symbolic(r)) => #XYShape(SymbolicDist.T.toPointSetDist(1000, r))
| (#toPointSet, #SampleSet(r)) => {
let response = SampleSet.toPointSetDist( let response = SampleSet.toPointSetDist(
~samples=r, ~samples=r,
~samplingInputs=defaultSamplingInputs, ~samplingInputs=defaultSamplingInputs,
@ -62,18 +87,23 @@ let distToDist = (wrapped: wrapped, fnName): wrapped => {
).pointSetDist ).pointSetDist
switch response { switch response {
| Some(r) => #XYShape(r) | Some(r) => #XYShape(r)
| None => #Error("Failed to convert sample into shape") | None => #Error(Other("Failed to convert sample into shape"))
} }
} }
| _ => #Error("No Match") | _ => #Error(Other("No Match or not supported"))
} }
(newVal, extra) (newVal, extra)
} }
// | (#truncateLeft(f), #XYContinuous(r)) => #XYContinuous(Continuous.T.truncate(Some(f), None, r))
// | (#truncateRight(f), #XYContinuous(r)) => #XYContinuous(Continuous.T.truncate(None, Some(f), r))
let foo = let applyFn = (wrapped, fnName): wrapped => {
exampleDist let (v, extra) as result = applyFnInternal(wrapped, fnName)
->wrapWithParams(genericParams) switch v {
->distToDist(#truncateLeft(3.0)) | #Error(NeedsPointSetConversion) => {
->distToDist(#trunctateRight(5.0)) let convertedToPointSet = applyFnInternal(wrapped, #toDist(#toPointSet))
applyFnInternal(convertedToPointSet, fnName)
}
| _ => result
}
}
let foo = exampleDist->wrapWithParams(genericParams)->applyFn(#toDist(#normalize))