From 5576bf2ef361c39607776d076fa86f590f314013 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Fri, 29 Jul 2022 02:12:07 +0200 Subject: [PATCH] fixes #904. E.O.default performance --- .../src/rescript/Distributions/PointSetDist/Continuous.res | 2 +- .../src/rescript/Distributions/PointSetDist/Discrete.res | 3 ++- .../Distributions/PointSetDist/MixedShapeBuilder.res | 6 ++++-- .../ReducerInterface/ReducerInterface_ExternalLibrary.res | 2 +- packages/squiggle-lang/src/rescript/Utility/E.res | 6 ++++++ 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res index 05d94ca9..2297a3e3 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Continuous.res @@ -250,7 +250,7 @@ module T = Dist({ let downsample = (length, t): t => t |> shapeMap(XYShape.XsConversion.proportionByProbabilityMass(length, integral(t).xyShape)) - let integralEndY = (t: t) => t.integralSumCache |> E.O.default(t |> integral |> lastY) + let integralEndY = (t: t) => t.integralSumCache |> E.O.defaultFn(() => t |> integral |> lastY) let integralXtoY = (f, t: t) => t |> integral |> shapeFn(XYShape.XtoY.linear(f)) let integralYtoX = (f, t: t) => t |> integral |> shapeFn(XYShape.YtoX.linear(f)) let toContinuous = t => Some(t) diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res index 7e7c4981..b7d5ffd4 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/Discrete.res @@ -158,7 +158,8 @@ module T = Dist({ Continuous.make(~interpolation=#Stepwise, integralShape) } - let integralEndY = (t: t) => t.integralSumCache |> E.O.default(t |> integral |> Continuous.lastY) + let integralEndY = (t: t) => + t.integralSumCache |> E.O.defaultFn(() => t |> integral |> Continuous.lastY) let minX = shapeFn(XYShape.T.minX) let maxX = shapeFn(XYShape.T.maxX) let toDiscreteProbabilityMassFraction = _ => 1.0 diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/MixedShapeBuilder.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/MixedShapeBuilder.res index 89273283..f4407ba8 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/MixedShapeBuilder.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/MixedShapeBuilder.res @@ -13,9 +13,11 @@ let buildSimple = ( ~discrete: option, ): option => { let continuous = - continuous |> E.O.default(Continuous.make(~integralSumCache=Some(0.0), {xs: [], ys: []})) + continuous |> E.O.defaultFn(() => + Continuous.make(~integralSumCache=Some(0.0), {xs: [], ys: []}) + ) let discrete = - discrete |> E.O.default(Discrete.make(~integralSumCache=Some(0.0), {xs: [], ys: []})) + discrete |> E.O.defaultFn(() => Discrete.make(~integralSumCache=Some(0.0), {xs: [], ys: []})) let cLength = continuous |> Continuous.getShape |> XYShape.T.xs |> E.A.length let dLength = discrete |> Discrete.getShape |> XYShape.T.xs |> E.A.length switch (cLength, dLength) { diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res index fc0a2821..7ae6ace9 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res @@ -16,7 +16,7 @@ let dispatch = ( () => ReducerInterface_Duration.dispatch(call, environment), () => ReducerInterface_Number.dispatch(call, environment), () => FunctionRegistry_Library.dispatch(call, environment, reducer), - ])->E.O2.default(chain(call, environment, reducer)) + ])->E.O2.defaultFn(() => chain(call, environment, reducer)) } /* diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 50a28382..22c8c525 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -82,6 +82,11 @@ module O = { | None => d | Some(a) => a } + let defaultFn = (d, o) => + switch o { + | None => d() + | Some(a) => a + } let isSome = o => switch o { | Some(_) => true @@ -158,6 +163,7 @@ module O = { module O2 = { let default = (a, b) => O.default(b, a) + let defaultFn = (a, b) => O.defaultFn(b, a) let toExn = (a, b) => O.toExn(b, a) let fmap = (a, b) => O.fmap(b, a) let toResult = (a, b) => O.toResult(b, a)