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..44b6abd2 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,30 @@ 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) + PointSetDist.T.mapYResult(~fn, pointSetDist)->toType + } +} + let library = [ Function.make( ~name="fromDist", @@ -53,6 +77,27 @@ 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(Reducer_ErrorValue.errorToString) + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), Function.make( ~name="makeContinuous", ~nameSpace, 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..c40fe349 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,9 @@ 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 +238,9 @@ 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 +263,9 @@ 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) }, (), diff --git a/packages/website/docs/Api/DistPointSet.md b/packages/website/docs/Api/DistPointSet.md index 9c9c3d0d..bfd8ae82 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 +```