diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res index d21a7383..92398099 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/PointSetDist.res @@ -231,9 +231,8 @@ let doN = (n, fn) => { } let sample = (t: t): float => { - let randomItem = Random.float(1.) - let bar = t |> T.Integral.yToX(randomItem) - bar + let randomItem = Random.float(1.0) + t |> T.Integral.yToX(randomItem) } let isFloat = (t: t) => diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index 1b0fa809..5ecaf9dd 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -9,6 +9,7 @@ module Wrappers = { } module Prepare = { + type t = frValue type ts = array type err = string @@ -26,6 +27,14 @@ module Prepare = { | _ => Error(impossibleError) } } + + module Array = { + let openA = (inputs: t): result => + switch inputs { + | FRValueArray(n) => Ok(n) + | _ => Error(impossibleError) + } + } } module ToValueTuple = { diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 532f534d..79195dc2 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -32,7 +32,36 @@ module Declaration = { let registry = [ Function.make( - ~name="FnMake", + ~name="toContinuousPointSet", + ~definitions=[ + FnDefinition.make( + ~name="toContinuousPointSet", + ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], + ~run=(inputs, _) => { + let array = inputs->E.A.unsafe_get(0)->Prepare.ToValueArray.Array.openA + let xyCoords = + array->E.R.bind(xyCoords => + xyCoords + ->E.A2.fmap(xyCoord => + [xyCoord] + ->Prepare.ToValueArray.Record.twoArgs + ->E.R.bind(Prepare.ToValueTuple.twoNumbers) + ) + ->E.A.R.firstErrorOrOpen + ) + let expressionValue = + xyCoords + ->E.R.bind(r => r->XYShape.T.makeFromZipped->E.R2.errMap(XYShape.Error.toString)) + ->E.R2.fmap(r => ReducerInterface_ExpressionValue.EvDistribution( + PointSet(Continuous(Continuous.make(r))), + )) + expressionValue + }, + ), + ], + ), + Function.make( + ~name="Declaration", ~definitions=[ FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) => { inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 40852eb3..372ad8ab 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -571,6 +571,7 @@ module A = { let tail = Belt.Array.sliceToEnd(_, 1) let zip = Belt.Array.zip + let unzip = Belt.Array.unzip let zip3 = (a, b, c) => Belt.Array.zip(a, b)->Belt.Array.zip(c)->Belt.Array.map((((v1, v2), v3)) => (v1, v2, v3)) // This zips while taking the longest elements of each array. diff --git a/packages/squiggle-lang/src/rescript/Utility/XYShape.res b/packages/squiggle-lang/src/rescript/Utility/XYShape.res index b4758dfd..3fbe9f46 100644 --- a/packages/squiggle-lang/src/rescript/Utility/XYShape.res +++ b/packages/squiggle-lang/src/rescript/Utility/XYShape.res @@ -148,6 +148,11 @@ module T = { | None => Ok(attempt) } } + + let makeFromZipped = (values: array<(float, float)>) => { + let (xs, ys) = E.A.unzip(values) + make(~xs, ~ys) + } } module Ts = {