From 2aaa43008d0497cc65df19d4fe75865e16c77fcb Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Mon, 8 Aug 2022 10:31:05 -0700 Subject: [PATCH 1/4] Very simple implementation of pointset mapY --- .../FunctionRegistry/Library/FR_Pointset.res | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res index 4f4e1731..c0270e48 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res @@ -23,6 +23,31 @@ let inputsTodist = (inputs: array, makeDist) => { expressionValue } +module Internal = { + type t = PointSetDist.t + + let toType = (r): result< + ReducerInterface_InternalExpressionValue.t, + Reducer_ErrorValue.errorValue, + > => + switch r { + | Ok(r) => Ok(Wrappers.evDistribution(PointSet(r))) + | Error(err) => Error(REOperationError(err)) + } + + let doLambdaCall = (aLambdaValue, list, environment, reducer) => + switch Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) { + | Ok(IEvNumber(f)) => Ok(f) + | _ => Error(Operation.SampleMapNeedsNtoNFunction) + } + + let mapY = (pointSetDist: t, aLambdaValue, env, reducer) => { + let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) + let foo = PointSetDist.T.mapYResult(~fn, pointSetDist) + foo->toType + } +} + let library = [ Function.make( ~name="fromDist", @@ -53,6 +78,26 @@ let library = [ ], (), ), + Function.make( + ~name="mapY", + ~nameSpace, + ~requiresNamespace=true, + ~examples=[`PointSet.mapY(mx(normal(5,2)), {|x| x + 1})`], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="mapY", + ~inputs=[FRTypeDist, FRTypeLambda], + ~run=(inputs, _, env, reducer) => + switch inputs { + | [IEvDistribution(PointSet(dist)), IEvLambda(lambda)] => Internal.mapY(dist, lambda, env, reducer)->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), Function.make( ~name="makeContinuous", ~nameSpace, From a04761226d28be90129f15b8b84d99ddf80cde5e Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 20 Aug 2022 15:50:30 -0700 Subject: [PATCH 2/4] Improved error messages of function library --- .../FunctionRegistry/Library/FR_Pointset.res | 6 +++--- .../FunctionRegistry/Library/FR_Sampleset.res | 14 ++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res index c0270e48..44b6abd2 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res @@ -43,8 +43,7 @@ module Internal = { let mapY = (pointSetDist: t, aLambdaValue, env, reducer) => { let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) - let foo = PointSetDist.T.mapYResult(~fn, pointSetDist) - foo->toType + PointSetDist.T.mapYResult(~fn, pointSetDist)->toType } } @@ -90,7 +89,8 @@ let library = [ ~inputs=[FRTypeDist, FRTypeLambda], ~run=(inputs, _, env, reducer) => switch inputs { - | [IEvDistribution(PointSet(dist)), IEvLambda(lambda)] => Internal.mapY(dist, lambda, env, reducer)->E.R2.errMap(_ => "") + | [IEvDistribution(PointSet(dist)), IEvLambda(lambda)] => + Internal.mapY(dist, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) | _ => Error(impossibleError) }, (), diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index 27b870ee..ec6df20c 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -92,7 +92,7 @@ let library = [ GenericDist.toSampleSetDist(dist, env.sampleCount) ->E.R2.fmap(Wrappers.sampleSet) ->E.R2.fmap(Wrappers.evDistribution) - ->E.R2.errMap(_ => "") + ->E.R2.errMap(DistributionTypes.Error.toString) | _ => Error(impossibleError) }, (), @@ -158,7 +158,7 @@ let library = [ | [IEvLambda(lambda)] => switch Internal.fromFn(lambda, env, reducer) { | Ok(r) => Ok(r->Wrappers.sampleSet->Wrappers.evDistribution) - | Error(_) => Error("issue") + | Error(e) => Error(Operation.Error.toString(e)) } | _ => Error(impossibleError) }, @@ -180,7 +180,7 @@ let library = [ ~run=(inputs, _, env, reducer) => switch inputs { | [IEvDistribution(SampleSet(dist)), IEvLambda(lambda)] => - Internal.map1(dist, lambda, env, reducer)->E.R2.errMap(_ => "") + Internal.map1(dist, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) | _ => Error(impossibleError) }, (), @@ -207,7 +207,7 @@ let library = [ IEvDistribution(SampleSet(dist2)), IEvLambda(lambda), ] => - Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(_ => "") + Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) | _ => Error(impossibleError) } }, @@ -236,7 +236,7 @@ let library = [ IEvDistribution(SampleSet(dist3)), IEvLambda(lambda), ] => - Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(_ => "") + Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) | _ => Error(impossibleError) }, (), @@ -259,9 +259,7 @@ let library = [ ~run=(inputs, _, env, reducer) => switch inputs { | [IEvArray(dists), IEvLambda(lambda)] => - Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(_e => { - "AHHH doesn't work" - }) + Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) | _ => Error(impossibleError) }, (), From 15debbddea9ead8481d85ee624db9fb2c891ae68 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 20 Aug 2022 15:56:59 -0700 Subject: [PATCH 3/4] Added very minimal documentation for PointSet mapY --- packages/website/docs/Api/DistPointSet.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/website/docs/Api/DistPointSet.md b/packages/website/docs/Api/DistPointSet.md index 9c9c3d0d..a966f296 100644 --- a/packages/website/docs/Api/DistPointSet.md +++ b/packages/website/docs/Api/DistPointSet.md @@ -46,3 +46,13 @@ PointSet.makeDiscrete([ { x: 3, y: 0.1 }, ]); ``` + +### mapY + +``` +PointSet.mapY: (pointSetDist, (number => number)) => pointSetDist +``` + +```javascript +normal(5,3) |> PointSet.fromDist |> PointSet.mapY({|x| x ^ 2}) |> normalize +``` \ No newline at end of file From 72b1d6af3e7504af935f32f40cc036c7e0e8dfaf Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 20 Aug 2022 15:59:25 -0700 Subject: [PATCH 4/4] Formatted --- .../FunctionRegistry/Library/FR_Sampleset.res | 12 +++++++++--- packages/website/docs/Api/DistPointSet.md | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index ec6df20c..c40fe349 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -207,7 +207,9 @@ let library = [ IEvDistribution(SampleSet(dist2)), IEvLambda(lambda), ] => - Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) + Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap( + Reducer_ErrorValue.errorToString, + ) | _ => Error(impossibleError) } }, @@ -236,7 +238,9 @@ let library = [ IEvDistribution(SampleSet(dist3)), IEvLambda(lambda), ] => - Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) + Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap( + Reducer_ErrorValue.errorToString, + ) | _ => Error(impossibleError) }, (), @@ -259,7 +263,9 @@ let library = [ ~run=(inputs, _, env, reducer) => switch inputs { | [IEvArray(dists), IEvLambda(lambda)] => - Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString) + Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap( + Reducer_ErrorValue.errorToString, + ) | _ => Error(impossibleError) }, (), diff --git a/packages/website/docs/Api/DistPointSet.md b/packages/website/docs/Api/DistPointSet.md index a966f296..bfd8ae82 100644 --- a/packages/website/docs/Api/DistPointSet.md +++ b/packages/website/docs/Api/DistPointSet.md @@ -55,4 +55,4 @@ PointSet.mapY: (pointSetDist, (number => number)) => pointSetDist ```javascript normal(5,3) |> PointSet.fromDist |> PointSet.mapY({|x| x ^ 2}) |> normalize -``` \ No newline at end of file +```