diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res index f0fbff99..2b76b1fb 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res @@ -140,3 +140,9 @@ let truncate = (t, ~leftCutoff: option, ~rightCutoff: option) => { let withTruncatedRight = t => rightCutoff |> E.O.dimap(left => truncateRight(t, left), _ => Ok(t)) t->withTruncatedLeft |> E.R2.bind(withTruncatedRight) } + +let minOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.min_float(a, b)), ~t1, ~t2) +let maxOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.max_float(a, b)), ~t1, ~t2) + +let minOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.min_float(a, f)), t) +let maxOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.max_float(a, f)), t) 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 c40fe349..1d5487ad 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -75,7 +75,7 @@ module Internal = { } } -let library = [ +let library1 = [ Function.make( ~name="fromDist", ~nameSpace, @@ -274,3 +274,63 @@ let library = [ (), ), ] + +module Comparison = { + let template = (name, inputs, run) => { + FnDefinition.make( + ~name, + ~inputs, + ~run=(inputs, _, _, _) => { + run(inputs) + }, + (), + ) + } + + let wrapper = r => + r + ->E.R2.fmap(r => r->Wrappers.sampleSet->Wrappers.evDistribution) + ->E.R2.errMap(SampleSetDist.Error.toString) + + let mkBig = (name, withDist, withFloat) => + Function.make( + ~name, + ~nameSpace, + ~requiresNamespace=false, + ~examples=[ + `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(6,2)))`, + `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), 3.0)`, + `SampleSet.${name}(4.0, SampleSet.fromDist(normal(6,2)))`, + ], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + template(name, [FRTypeDist, FRTypeDist], inputs => { + switch inputs { + | [IEvDistribution(SampleSet(dist1)), IEvDistribution(SampleSet(dist2))] => + withDist(dist1, dist2)->wrapper + | _ => Error(impossibleError) + } + }), + template(name, [FRTypeDist, FRTypeNumber], inputs => { + switch inputs { + | [IEvDistribution(SampleSet(dist)), IEvNumber(f)] => withFloat(dist, f)->wrapper + | _ => Error(impossibleError) + } + }), + template(name, [FRTypeNumber, FRTypeDist], inputs => { + switch inputs { + | [IEvNumber(f), IEvDistribution(SampleSet(dist))] => withFloat(dist, f)->wrapper + | _ => Error(impossibleError) + } + }), + ], + (), + ) + + let library = [ + mkBig("min", SampleSetDist.minOfTwo, SampleSetDist.minOfFloat), + mkBig("max", SampleSetDist.maxOfTwo, SampleSetDist.maxOfFloat), + ] +} + +let library = E.A.append(library1, Comparison.library)