Minor cleanup

This commit is contained in:
Ozzie Gooen 2022-07-19 08:12:04 -07:00
parent ee244463fa
commit 28036bfb36
2 changed files with 68 additions and 69 deletions

View File

@ -8,71 +8,73 @@ let expectEvalToBeOk = (expr: string) =>
let registry = FunctionRegistry_Library.registry let registry = FunctionRegistry_Library.registry
let examples = E.A.to_list(FunctionRegistry_Core.Registry.allExamples(registry)) let examples = E.A.to_list(FunctionRegistry_Core.Registry.allExamples(registry))
describe("Fn auto-testing", () => { describe("FunctionRegistry Library", () => {
testAll("tests of validity", examples, r => { describe("Regular tests", () => {
expectEvalToBeOk(r) testEvalToBe("List.make(3, 'HI')", "Ok(['HI','HI','HI'])")
testEvalToBe("make(3, 'HI')", "Error(Function not found: make(Number,String))")
testEvalToBe("List.upTo(1,3)", "Ok([1,2,3])")
testEvalToBe("List.first([3,5,8])", "Ok(3)")
testEvalToBe("List.last([3,5,8])", "Ok(8)")
testEvalToBe("List.reverse([3,5,8])", "Ok([8,5,3])")
testEvalToBe("Dist.normal(5,2)", "Ok(Normal(5,2))")
testEvalToBe("normal(5,2)", "Ok(Normal(5,2))")
testEvalToBe("normal({mean:5,stdev:2})", "Ok(Normal(5,2))")
testEvalToBe("-2 to 4", "Ok(Normal(1,1.8238704957353074))")
testEvalToBe("pointMass(5)", "Ok(PointMass(5))")
testEvalToBe("Number.floor(5.5)", "Ok(5)")
testEvalToBe("Number.ceil(5.5)", "Ok(6)")
testEvalToBe("floor(5.5)", "Ok(5)")
testEvalToBe("ceil(5.5)", "Ok(6)")
testEvalToBe("Number.abs(5.5)", "Ok(5.5)")
testEvalToBe("abs(5.5)", "Ok(5.5)")
testEvalToBe("Number.exp(10)", "Ok(22026.465794806718)")
testEvalToBe("Number.log10(10)", "Ok(1)")
testEvalToBe("Number.log2(10)", "Ok(3.321928094887362)")
testEvalToBe("Number.sum([2,5,3])", "Ok(10)")
testEvalToBe("sum([2,5,3])", "Ok(10)")
testEvalToBe("Number.product([2,5,3])", "Ok(30)")
testEvalToBe("Number.min([2,5,3])", "Ok(2)")
testEvalToBe("Number.max([2,5,3])", "Ok(5)")
testEvalToBe("Number.mean([0,5,10])", "Ok(5)")
testEvalToBe("Number.geomean([1,5,18])", "Ok(4.481404746557164)")
testEvalToBe("Number.stdev([0,5,10,15])", "Ok(5.5901699437494745)")
testEvalToBe("Number.variance([0,5,10,15])", "Ok(31.25)")
testEvalToBe("Number.sort([10,0,15,5])", "Ok([0,5,10,15])")
testEvalToBe("Number.cumsum([1,5,3])", "Ok([1,6,9])")
testEvalToBe("Number.cumprod([1,5,3])", "Ok([1,5,15])")
testEvalToBe("Number.diff([1,5,3])", "Ok([4,-2])")
testEvalToBe(
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1), prior: normal(5.5,3)})",
"Ok(-0.33591375663884876)",
)
testEvalToBe(
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1)})",
"Ok(0.32244107041564646)",
)
testEvalToBe("Dist.logScore({estimate: normal(5,2), answer: 4.5})", "Ok(1.6433360626394853)")
testEvalToBe("Dist.klDivergence(normal(5,2), normal(5,1.5))", "Ok(0.06874342818671068)")
}) })
testAll( describe("Fn auto-testing", () => {
"tests of type", testAll("tests of validity", examples, r => {
E.A.to_list( expectEvalToBeOk(r)
FunctionRegistry_Core.Registry.allExamplesWithFns(registry)->E.A2.filter(((fn, _)) => })
E.O.isSome(fn.output)
),
),
((fn, example)) => {
let responseType =
example
->Reducer.evaluate
->E.R2.fmap(ReducerInterface_InternalExpressionValue.externalValueToValueType)
let expectedOutputType = fn.output |> E.O.toExn("")
expect(responseType)->toEqual(Ok(expectedOutputType))
},
)
})
describe("FunctionRegistry Library", () => { testAll(
testEvalToBe("List.make(3, 'HI')", "Ok(['HI','HI','HI'])") "tests of type",
testEvalToBe("make(3, 'HI')", "Error(Function not found: make(Number,String))") E.A.to_list(
testEvalToBe("List.upTo(1,3)", "Ok([1,2,3])") FunctionRegistry_Core.Registry.allExamplesWithFns(registry)->E.A2.filter(((fn, _)) =>
testEvalToBe("List.first([3,5,8])", "Ok(3)") E.O.isSome(fn.output)
testEvalToBe("List.last([3,5,8])", "Ok(8)") ),
testEvalToBe("List.reverse([3,5,8])", "Ok([8,5,3])") ),
testEvalToBe("Dist.normal(5,2)", "Ok(Normal(5,2))") ((fn, example)) => {
testEvalToBe("normal(5,2)", "Ok(Normal(5,2))") let responseType =
testEvalToBe("normal({mean:5,stdev:2})", "Ok(Normal(5,2))") example
testEvalToBe("-2 to 4", "Ok(Normal(1,1.8238704957353074))") ->Reducer.evaluate
testEvalToBe("pointMass(5)", "Ok(PointMass(5))") ->E.R2.fmap(ReducerInterface_InternalExpressionValue.externalValueToValueType)
testEvalToBe("Number.floor(5.5)", "Ok(5)") let expectedOutputType = fn.output |> E.O.toExn("")
testEvalToBe("Number.ceil(5.5)", "Ok(6)") expect(responseType)->toEqual(Ok(expectedOutputType))
testEvalToBe("floor(5.5)", "Ok(5)") },
testEvalToBe("ceil(5.5)", "Ok(6)") )
testEvalToBe("Number.abs(5.5)", "Ok(5.5)") })
testEvalToBe("abs(5.5)", "Ok(5.5)")
testEvalToBe("Number.exp(10)", "Ok(22026.465794806718)")
testEvalToBe("Number.log10(10)", "Ok(1)")
testEvalToBe("Number.log2(10)", "Ok(3.321928094887362)")
testEvalToBe("Number.sum([2,5,3])", "Ok(10)")
testEvalToBe("sum([2,5,3])", "Ok(10)")
testEvalToBe("Number.product([2,5,3])", "Ok(30)")
testEvalToBe("Number.min([2,5,3])", "Ok(2)")
testEvalToBe("Number.max([2,5,3])", "Ok(5)")
testEvalToBe("Number.mean([0,5,10])", "Ok(5)")
testEvalToBe("Number.geomean([1,5,18])", "Ok(4.481404746557164)")
testEvalToBe("Number.stdev([0,5,10,15])", "Ok(5.5901699437494745)")
testEvalToBe("Number.variance([0,5,10,15])", "Ok(31.25)")
testEvalToBe("Number.sort([10,0,15,5])", "Ok([0,5,10,15])")
testEvalToBe("Number.cumsum([1,5,3])", "Ok([1,6,9])")
testEvalToBe("Number.cumprod([1,5,3])", "Ok([1,5,15])")
testEvalToBe("Number.diff([1,5,3])", "Ok([4,-2])")
testEvalToBe(
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1), prior: normal(5.5,3)})",
"Ok(-0.33591375663884876)",
)
testEvalToBe(
"Dist.logScore({estimate: normal(5,2), answer: normal(5.2,1)})",
"Ok(0.32244107041564646)",
)
testEvalToBe("Dist.logScore({estimate: normal(5,2), answer: 4.5})", "Ok(1.6433360626394853)")
testEvalToBe("Dist.klDivergence(normal(5,2), normal(5,1.5))", "Ok(0.06874342818671068)")
}) })

View File

@ -350,9 +350,6 @@ module FnDefinition = {
} }
} }
let toFfiFn = (t: t): Reducer_Expression_T.optionFfiFn =>
(args, environment) => run(t, args, environment)->E.R.toOption
let make = (~name, ~inputs, ~run, ()): t => { let make = (~name, ~inputs, ~run, ()): t => {
name: name, name: name,
inputs: inputs, inputs: inputs,
@ -414,7 +411,7 @@ module Registry = {
let allExamplesWithFns = (r: registry) => let allExamplesWithFns = (r: registry) =>
r.functions->E.A2.fmap(fn => fn.examples->E.A2.fmap(example => (fn, example)))->E.A.concatMany r.functions->E.A2.fmap(fn => fn.examples->E.A2.fmap(example => (fn, example)))->E.A.concatMany
let buildFnNameDict = (r: array<function>): fnNameDict => { let _buildFnNameDict = (r: array<function>): fnNameDict => {
let allDefinitionsWithFns = let allDefinitionsWithFns =
r r
->E.A2.fmap(fn => fn.definitions->E.A2.fmap(definitions => (fn, definitions))) ->E.A2.fmap(fn => fn.definitions->E.A2.fmap(definitions => (fn, definitions)))
@ -441,7 +438,7 @@ module Registry = {
} }
let make = (fns: array<function>): registry => { let make = (fns: array<function>): registry => {
let dict = buildFnNameDict(fns) let dict = _buildFnNameDict(fns)
{functions: fns, fnNameDict: dict} {functions: fns, fnNameDict: dict}
} }