Refactoring GenericOperation to prefer -> operator

This commit is contained in:
Ozzie Gooen 2022-03-29 14:36:54 -04:00
parent 01b80d73be
commit cdbbededa4
7 changed files with 33 additions and 27 deletions

View File

@ -37,9 +37,9 @@ describe("mixture", () => {
test("on two normal distributions", () => {
let result =
run(#mixture([(normalDist10, 0.5), (normalDist20, 0.5)]))
|> fmap(#fromDist(#toFloat(#Mean)))
|> toFloat
|> toExt
->fmap(#fromDist(#toFloat(#Mean)))
->toFloat
->toExt
expect(result)->toBeCloseTo(15.28)
})
})
@ -48,15 +48,15 @@ describe("toPointSet", () => {
test("on symbolic normal distribution", () => {
let result =
run(#fromDist(#toDist(#toPointSet), normalDist))
|> fmap(#fromDist(#toFloat(#Mean)))
|> toFloat
|> toExt
->fmap(#fromDist(#toFloat(#Mean)))
->toFloat
->toExt
expect(result)->toBeCloseTo(5.09)
})
test("on sample set distribution with under 4 points", () => {
let result =
run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0]))) |> fmap(
run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0])))->fmap(
#fromDist(#toFloat(#Mean)),
)
expect(result)->toEqual(#Error(Other("Converting sampleSet to pointSet failed")))
@ -65,11 +65,11 @@ describe("toPointSet", () => {
test("on sample set", () => {
let result =
run(#fromDist(#toDist(#toPointSet), normalDist))
|> fmap(#fromDist(#toDist(#toSampleSet(1000))))
|> fmap(#fromDist(#toDist(#toPointSet)))
|> fmap(#fromDist(#toFloat(#Mean)))
|> toFloat
|> toExt
->fmap(#fromDist(#toDist(#toSampleSet(1000))))
->fmap(#fromDist(#toDist(#toPointSet)))
->fmap(#fromDist(#toFloat(#Mean)))
->toFloat
->toExt
expect(result)->toBeCloseTo(5.09)
})
})

View File

@ -6,7 +6,7 @@ type toSampleSetFn = t => result<array<float>, error>
type scaleMultiplyFn = (t, float) => result<t, error>
type pointwiseAddFn = (t, t) => result<t, error>
let sampleN = (n, t: t) =>
let sampleN = (t: t, n) =>
switch t {
| #PointSet(r) => Ok(PointSetDist.sampleNRendered(n, r))
| #Symbolic(r) => Ok(SymbolicDist.T.sampleN(n, r))
@ -53,7 +53,7 @@ let defaultSamplingInputs: SamplingInputs.samplingInputs = {
kernelWidth: None,
}
let toPointSet = (xyPointLength, t: t): result<PointSetTypes.pointSetDist, error> => {
let toPointSet = (t, xyPointLength): result<PointSetTypes.pointSetDist, error> => {
switch t {
| #PointSet(pointSet) => Ok(pointSet)
| #Symbolic(r) => Ok(SymbolicDist.T.toPointSetDist(xyPointLength, r))

View File

@ -5,7 +5,7 @@ type toSampleSetFn = t => result<array<float>, error>
type scaleMultiplyFn = (t, float) => result<t, error>
type pointwiseAddFn = (t, t) => result<t, error>
let sampleN: (int, t) => result<array<float>, error>
let sampleN: (t, int) => result<array<float>, error>
let fromFloat: float => t
@ -15,7 +15,7 @@ let normalize: t => t
let operationToFloat: (toPointSetFn, Operation.distToFloatOperation, t) => result<float, error>
let toPointSet: (int, t) => result<PointSetTypes.pointSetDist, error>
let toPointSet: (t, int) => result<PointSetTypes.pointSetDist, error>
let truncate: (toPointSetFn, option<float>, option<float>, t) => result<t, error>

View File

@ -16,6 +16,7 @@ type outputType = [
| #String(string)
]
module Output = {
let toDist = (o: outputType) =>
switch o {
@ -82,13 +83,14 @@ let rec run = (extra, fnName: operation): outputType => {
reCall(
~fnName=#fromDist(#toDistCombination(#Pointwise, #Multiply, #Float(weight)), r),
(),
) |> outputToDistResult
) -> outputToDistResult
let pointwiseAdd = (r1, r2) =>
reCall(
~fnName=#fromDist(#toDistCombination(#Pointwise, #Add, #Dist(r2)), r1),
(),
) |> outputToDistResult
) -> outputToDistResult
let fromDistFn = (subFn: GenericDist_Types.Operation.fromDist, dist: genericDist) =>
switch subFn {
@ -96,21 +98,21 @@ let rec run = (extra, fnName: operation): outputType => {
GenericDist.operationToFloat(toPointSet, fnName, dist)
|> E.R.fmap(r => #Float(r))
|> fromResult
| #toString => dist |> GenericDist.toString |> (r => #String(r))
| #toString => dist -> GenericDist.toString -> (r => #String(r))
| #toDist(#consoleLog) => {
Js.log2("Console log requested: ", dist)
#Dist(dist)
}
| #toDist(#normalize) => dist |> GenericDist.normalize |> (r => #Dist(r))
| #toDist(#normalize) => dist -> GenericDist.normalize -> (r => #Dist(r))
| #toDist(#truncate(left, right)) =>
dist |> GenericDist.truncate(toPointSet, left, right) |> E.R.fmap(r => #Dist(r)) |> fromResult
| #toDist(#toPointSet) =>
dist
|> GenericDist.toPointSet(xyPointLength)
-> GenericDist.toPointSet(xyPointLength)
|> E.R.fmap(r => #Dist(#PointSet(r)))
|> fromResult
| #toDist(#toSampleSet(n)) =>
dist |> GenericDist.sampleN(n) |> E.R.fmap(r => #Dist(#SampleSet(r))) |> fromResult
dist -> GenericDist.sampleN(n) |> E.R.fmap(r => #Dist(#SampleSet(r))) |> fromResult
| #toDistCombination(#Algebraic, _, #Float(_)) => #Error(NotYetImplemented)
| #toDistCombination(#Algebraic, operation, #Dist(dist2)) =>
dist
@ -142,8 +144,8 @@ let runFromFloat = (extra, fnName, float) => run(extra, #fromFloat(fnName, float
let fmap = (
extra,
fn: GenericDist_Types.Operation.singleParamaterFunction,
input: outputType,
fn: GenericDist_Types.Operation.singleParamaterFunction,
): outputType => {
let newFnCall: result<operation, error> = switch (fn, input) {
| (#fromDist(fromDist), #Dist(o)) => Ok(#fromDist(fromDist, o))

View File

@ -17,7 +17,7 @@ let runFromDist: (
GenericDist_Types.genericDist,
) => outputType
let runFromFloat: (params, GenericDist_Types.Operation.fromDist, float) => outputType
let fmap: (params, GenericDist_Types.Operation.singleParamaterFunction, outputType) => outputType
let fmap: (params, outputType, GenericDist_Types.Operation.singleParamaterFunction) => outputType
module Output: {
let toDist: outputType => option<GenericDist_Types.genericDist>

View File

@ -78,11 +78,11 @@ module Operation = {
| #toFloat(#Cdf(r)) => `cdf(${E.Float.toFixed(r)})`
| #toFloat(#Inv(r)) => `inv(${E.Float.toFixed(r)})`
| #toFloat(#Mean) => `mean`
| #toFloat(#Pdf(r)) => `pdf${E.Float.toFixed(r)}`
| #toFloat(#Pdf(r)) => `pdf(${E.Float.toFixed(r)})`
| #toFloat(#Sample) => `sample`
| #toDist(#normalize) => `normalize`
| #toDist(#toPointSet) => `toPointSet`
| #toDist(#toSampleSet(r)) => `toSampleSet${E.I.toString(r)}`
| #toDist(#toSampleSet(r)) => `toSampleSet(${E.I.toString(r)})`
| #toDist(#truncate(_, _)) => `truncate`
| #toDist(#consoleLog) => `consoleLog`
| #toString => `toString`

View File

@ -31,6 +31,8 @@ graph TD
D --> Cauchy(Cauchy)
```
## Diagram of Generic Distribution Types
## Todo
- [ ] Lots of cleanup
- [ ] Simple test story
@ -42,3 +44,5 @@ graph TD
- [ ] More functions for Sample Set, which is very minimal now
- [ ] Allow these functions to be run on web workers
- [ ] Refactor interpreter to use GenericDist. This might not be necessary, as the new reducer-inspired interpreter is integrated.
## More todos