From 6887da59b3d9cdac821ec3792b805c014f18e7b2 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Fri, 7 Aug 2020 14:24:15 +0100 Subject: [PATCH] Simple optional functions --- src/components/DistBuilder.re | 24 +++++--- src/components/charts/DistPlusPlotReducer.re | 2 +- src/distPlus/expressionTree/ExpressionTree.re | 28 ++++----- src/distPlus/expressionTree/PTypes.re | 2 +- src/distPlus/renderers/DistPlusRenderer.re | 60 ++++++++++++++----- 5 files changed, 73 insertions(+), 43 deletions(-) diff --git a/src/components/DistBuilder.re b/src/components/DistBuilder.re index 5b5f6246..5f3fd6da 100644 --- a/src/components/DistBuilder.re +++ b/src/components/DistBuilder.re @@ -149,7 +149,8 @@ module DemoDist = { sampleCount: Some(options.sampleCount), outputXYPoints: Some(options.outputXYPoints), kernelWidth: options.kernelWidth, - shapeLength: Some(options.downsampleTo |> E.O.default(1000)) + shapeLength: + Some(options.downsampleTo |> E.O.default(1000)), }, ~distPlusIngredients, ~environment= @@ -160,12 +161,22 @@ module DemoDist = { let response1 = DistPlusRenderer.run2(inputs1); switch (response1) { - | (Ok(`DistPlus(distPlus1))) => + | Ok(`DistPlus(distPlus1)) => + + | Ok(`Function(f, a)) => + let result1 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(5.0))|],); + let result2 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(20.0))|],); + let result3 = DistPlusRenderer.runFunction(inputs1, (f, a), [|`SymbolicDist(`Float(40.0))|],); + switch (result1, result2, result3) { + | (Ok(`DistPlus(distPlus1)),Ok(`DistPlus(distPlus2)),Ok(`DistPlus(distPlus3))) => <> + + - | (Ok(`Function(f,a))) => "Function!!!" |> R.ste - | (Error(r)) => r |> R.ste + | _ => "Failure " |> R.ste + }; + | Error(r) => r |> R.ste }; | _ => "Nothing to show. Try to change the distribution description." @@ -329,10 +340,7 @@ let make = () => { - + diff --git a/src/components/charts/DistPlusPlotReducer.re b/src/components/charts/DistPlusPlotReducer.re index 0c4ec0ad..27277189 100644 --- a/src/components/charts/DistPlusPlotReducer.re +++ b/src/components/charts/DistPlusPlotReducer.re @@ -104,7 +104,7 @@ let reducer = (state: state, action: action) => let init = { showStats: false, showParams: false, - showPercentiles: true, + showPercentiles: false, distributions: [ {yLog: false, xLog: false, isCumulative: false, height: 1}, ], diff --git a/src/distPlus/expressionTree/ExpressionTree.re b/src/distPlus/expressionTree/ExpressionTree.re index 866d22a3..b61d04ce 100644 --- a/src/distPlus/expressionTree/ExpressionTree.re +++ b/src/distPlus/expressionTree/ExpressionTree.re @@ -1,13 +1,10 @@ open ExpressionTypes.ExpressionTree; -let toLeaf = (samplingInputs, environment, node: node) => { - node - |> ExpressionTreeEvaluator.toLeaf({ - samplingInputs, - environment, - evaluateNode: ExpressionTreeEvaluator.toLeaf, - }); +let envs = (samplingInputs, environment) => { + {samplingInputs, environment, evaluateNode: ExpressionTreeEvaluator.toLeaf}; }; +let toLeaf = (samplingInputs, environment, node: node) => + ExpressionTreeEvaluator.toLeaf(envs(samplingInputs, environment), node); let rec toString: node => string = fun @@ -27,7 +24,7 @@ let rec toString: node => string = | `Render(t) => toString(t) | `Symbol(t) => "Symbol: " ++ t | `FunctionCall(name, args) => - "[Fuction call: (" + "[Function call: (" ++ name ++ (args |> E.A.fmap(toString) |> Js.String.concatMany(_, ",")) ++ ")]" @@ -37,16 +34,6 @@ let rec toString: node => string = ++ toString(internal) ++ ")]"; -let toLeaf = (samplingInputs, environment, node: node) => { - switch(toLeaf(samplingInputs, environment, node)){ - | Ok(`SymbolicDist(n)) => `Render(`SymbolicDist(n)) |> toLeaf(samplingInputs, environment); - | Ok(`Function(n)) => Ok(`Function(n)) - | Ok(`RenderedDist(n)) => `Normalize(`RenderedDist(n))|> toLeaf(samplingInputs, environment) - | Error(e) => Error(e) - | _ => Error("Wrong type returned") - } -} - let toShape = (samplingInputs, environment, node: node) => { switch (toLeaf(samplingInputs, environment, node)) { | Ok(`RenderedDist(shape)) => Ok(shape) @@ -54,3 +41,8 @@ let toShape = (samplingInputs, environment, node: node) => { | Error(e) => Error(e) }; }; + +let runFunction = (samplingInputs, environment, inputs, fn: PTypes.Function.t) => { + let params = envs(samplingInputs, environment); + PTypes.Function.run(params, inputs, fn) +} diff --git a/src/distPlus/expressionTree/PTypes.re b/src/distPlus/expressionTree/PTypes.re index 19e85cca..950bd583 100644 --- a/src/distPlus/expressionTree/PTypes.re +++ b/src/distPlus/expressionTree/PTypes.re @@ -31,7 +31,7 @@ module Function = { }; evaluationParams.evaluateNode(newEvaluationParams, internals(t)); } else { - Error("Failure"); + Error("Wrong number of variables"); }; }; diff --git a/src/distPlus/renderers/DistPlusRenderer.re b/src/distPlus/renderers/DistPlusRenderer.re index 323d4b0f..4765722d 100644 --- a/src/distPlus/renderers/DistPlusRenderer.re +++ b/src/distPlus/renderers/DistPlusRenderer.re @@ -123,10 +123,7 @@ module Internals = { let inputsToLeaf = (inputs: inputs) => { MathJsParser.fromString(inputs.guesstimatorString) |> E.R.bind(_, g => runProgram(inputs, g)) - |> E.R.bind(_, r => - E.A.last(r) - |> E.O.toResult("No rendered lines") - ); + |> E.R.bind(_, r => E.A.last(r) |> E.O.toResult("No rendered lines")); }; let outputToDistPlus = (inputs: Inputs.inputs, shape: DistTypes.shape) => { @@ -144,21 +141,54 @@ let run = (inputs: Inputs.inputs) => { inputs |> Internals.distPlusRenderInputsToInputs |> Internals.inputsToLeaf - |> E.R.bind(_,r => switch(r){ - | `RenderedDist(n) => Ok(n) - | _ => Error("Didn't output renderedDist") - }) + |> E.R.bind(_, r => + switch (r) { + | `RenderedDist(n) => Ok(n) + | _ => Error("Didn't output renderedDist") + } + ) |> E.R.fmap(Internals.outputToDistPlus(inputs)); }; - let run2 = (inputs: Inputs.inputs) => { inputs |> Internals.distPlusRenderInputsToInputs |> Internals.inputsToLeaf - |> E.R.bind(_,r => switch(r){ - | `RenderedDist(n) => Ok(`DistPlus(Internals.outputToDistPlus(inputs,n))) - | `Function(n) => Ok(`Function(n)) - | _ => Error("Didn't output renderedDist") - }) -}; \ No newline at end of file + |> E.R.bind(_, r => + switch (r) { + | `RenderedDist(n) => + Ok(`DistPlus(Internals.outputToDistPlus(inputs, n))) + | `Function(n) => Ok(`Function(n)) + | _ => Error("Didn't output renderedDist") + } + ); +}; + +let runFunction = + ( + ins: Inputs.inputs, + fn: (array(string), ExpressionTypes.ExpressionTree.node), + fnInputs, + ) => { + let (_, fns) = fn; + let inputs = ins |> Internals.distPlusRenderInputsToInputs; + let output = + ExpressionTree.runFunction( + { + sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000), + outputXYPoints: + inputs.samplingInputs.outputXYPoints |> E.O.default(10000), + kernelWidth: inputs.samplingInputs.kernelWidth, + shapeLength: inputs.samplingInputs.shapeLength |> E.O.default(10000), + }, + inputs.environment, + fnInputs, + fn, + ); + Js.log2("GOt output", output); + switch (output) { + | Ok(`RenderedDist(n)) => Ok(`DistPlus(Internals.outputToDistPlus(ins, n))) + | Ok(`Function(n)) => Ok(`Function(n)) + | _ => Error("Didn't output renderedDist") + }; +};