From 937a8482fdb4e1912303d365a36642e81ca06af5 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Fri, 8 Apr 2022 09:08:49 -0400 Subject: [PATCH] Added sparkline and toString to ReducerInterface --- .../__tests__/GenericDist/GenericOperation__Test.res | 2 +- .../ReducerInterface_Distribution_test.res | 3 +++ .../DistributionOperation/DistributionOperation.res | 4 ++-- .../Distributions/GenericDist/GenericDist_Types.res | 11 +++++++---- .../ReducerInterface_GenericDistribution.res | 10 ++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/squiggle-lang/__tests__/GenericDist/GenericOperation__Test.res b/packages/squiggle-lang/__tests__/GenericDist/GenericOperation__Test.res index be01c87f..67659a41 100644 --- a/packages/squiggle-lang/__tests__/GenericDist/GenericOperation__Test.res +++ b/packages/squiggle-lang/__tests__/GenericDist/GenericOperation__Test.res @@ -59,7 +59,7 @@ describe("sparkline", () => { expected: DistributionOperation.outputType, ) => { test(name, () => { - let result = DistributionOperation.run(~env, FromDist(ToSparkline(20), dist)) + let result = DistributionOperation.run(~env, FromDist(ToString(ToSparkline(20)), dist)) expect(result)->toEqual(expected) }) } diff --git a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res index 85d80919..096ac360 100644 --- a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res +++ b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res @@ -30,6 +30,9 @@ describe("eval on distribution functions", () => { testEval("mean(normal(5,2))", "Ok(5)") testEval("mean(lognormal(1,2))", "Ok(20.085536923187668)") }) + describe("toString", () => { + testEval("toString(normal(5,2))", "Ok('Normal(5,2)')") + }) describe("normalize", () => { testEval("normalize(normal(5,2))", "Ok(Normal(5,2))") }) diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res index 4b111c97..faf39e27 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res @@ -113,8 +113,8 @@ let rec run = (~env, functionCallInfo: functionCallInfo): outputType => { GenericDist.toFloatOperation(dist, ~toPointSetFn, ~distToFloatOperation) ->E.R2.fmap(r => Float(r)) ->OutputLocal.fromResult - | ToString => dist->GenericDist.toString->String - | ToSparkline(buckets) => + | ToString(ToString) => dist->GenericDist.toString->String + | ToString(ToSparkline(buckets)) => GenericDist.toSparkline(dist, ~sampleCount, ~buckets, ()) ->E.R2.fmap(r => String(r)) ->OutputLocal.fromResult 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 ec8374b2..cc9f94c9 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res @@ -52,12 +52,15 @@ module Operation = { type toFloatArray = Sample(int) + type toString = + | ToString + | ToSparkline(int) + type fromDist = | ToFloat(toFloat) | ToDist(toDist) | ToDistCombination(direction, arithmeticOperation, [#Dist(genericDist) | #Float(float)]) - | ToString - | ToSparkline(int) + | ToString(toString) type singleParamaterFunction = | FromDist(fromDist) @@ -80,8 +83,8 @@ module Operation = { | ToDist(ToSampleSet(r)) => `toSampleSet(${E.I.toString(r)})` | ToDist(Truncate(_, _)) => `truncate` | ToDist(Inspect) => `inspect` - | ToString => `toString` - | ToSparkline(n) => `toSparkline(${E.I.toString(n)})` + | ToString(ToString) => `toString` + | ToString(ToSparkline(n)) => `toSparkline(${E.I.toString(n)})` | ToDistCombination(Algebraic, _, _) => `algebraic` | ToDistCombination(Pointwise, _, _) => `pointwise` } diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index 84e0697f..89be2d46 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -45,6 +45,13 @@ module Helpers = { FromDist(GenericDist_Types.Operation.ToFloat(fnCall), dist)->runGenericOperation->Some } + let toStringFn = ( + fnCall: GenericDist_Types.Operation.toString, + dist: GenericDist_Types.genericDist, + ) => { + FromDist(GenericDist_Types.Operation.ToString(fnCall), dist)->runGenericOperation->Some + } + let toDistFn = (fnCall: GenericDist_Types.Operation.toDist, dist) => { FromDist(GenericDist_Types.Operation.ToDist(fnCall), dist)->runGenericOperation->Some } @@ -119,6 +126,9 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option< ->SymbolicConstructors.symbolicResultToOutput | ("sample", [EvDistribution(dist)]) => Helpers.toFloatFn(#Sample, dist) | ("mean", [EvDistribution(dist)]) => Helpers.toFloatFn(#Mean, dist) + | ("toString", [EvDistribution(dist)]) => Helpers.toStringFn(ToString, dist) + | ("toSparkline", [EvDistribution(dist)]) => Helpers.toStringFn(ToSparkline(20), dist) + | ("toSparkline", [EvDistribution(dist), EvNumber(n)]) => Helpers.toStringFn(ToSparkline(Belt.Float.toInt(n)), dist) | ("exp", [EvDistribution(a)]) => // https://mathjs.org/docs/reference/functions/exp.html Helpers.twoDiststoDistFn(Algebraic, "pow", GenericDist.fromFloat(Math.e), a)->Some