Refactor mapY and mapYResult in mixed to remove duplication

This commit is contained in:
Sam Nolan 2022-04-23 10:17:07 -04:00
parent ad593e659b
commit 9fb7148290

View File

@ -161,24 +161,20 @@ module T = Dist({
let integralYtoX = (f, t) => t |> integral |> Continuous.getShape |> XYShape.YtoX.linear(f) let integralYtoX = (f, t) => t |> integral |> Continuous.getShape |> XYShape.YtoX.linear(f)
// This pipes all ys (continuous and discrete) through fn. let createMixedFromContinuousDiscrete = (
// If mapY is a linear operation, we might be able to update the integralSumCaches as well;
// if not, they'll be set to None.
let mapY = (
~integralSumCacheFn=_ => None, ~integralSumCacheFn=_ => None,
~integralCacheFn=_ => None, ~integralCacheFn=_ => None,
~fn: float => float,
t: t, t: t,
discrete: PointSetTypes.discreteShape,
continuous: PointSetTypes.continuousShape,
): t => { ): t => {
let yMappedDiscrete: PointSetTypes.discreteShape = let yMappedDiscrete: PointSetTypes.discreteShape =
t.discrete discrete
|> Discrete.T.mapY(~fn)
|> Discrete.updateIntegralSumCache(E.O.bind(t.discrete.integralSumCache, integralSumCacheFn)) |> Discrete.updateIntegralSumCache(E.O.bind(t.discrete.integralSumCache, integralSumCacheFn))
|> Discrete.updateIntegralCache(E.O.bind(t.discrete.integralCache, integralCacheFn)) |> Discrete.updateIntegralCache(E.O.bind(t.discrete.integralCache, integralCacheFn))
let yMappedContinuous: PointSetTypes.continuousShape = let yMappedContinuous: PointSetTypes.continuousShape =
t.continuous continuous
|> Continuous.T.mapY(~fn)
|> Continuous.updateIntegralSumCache( |> Continuous.updateIntegralSumCache(
E.O.bind(t.continuous.integralSumCache, integralSumCacheFn), E.O.bind(t.continuous.integralSumCache, integralSumCacheFn),
) )
@ -192,6 +188,26 @@ module T = Dist({
} }
} }
// This pipes all ys (continuous and discrete) through fn.
// If mapY is a linear operation, we might be able to update the integralSumCaches as well;
// if not, they'll be set to None.
let mapY = (
~integralSumCacheFn=_ => None,
~integralCacheFn=_ => None,
~fn: float => float,
t: t,
): t => {
let discrete = t.discrete |> Discrete.T.mapY(~fn)
let continuous = t.continuous |> Continuous.T.mapY(~fn)
createMixedFromContinuousDiscrete(
~integralCacheFn,
~integralSumCacheFn,
t,
discrete,
continuous,
)
}
let mapYResult = ( let mapYResult = (
~integralSumCacheFn=_ => None, ~integralSumCacheFn=_ => None,
~integralCacheFn=_ => None, ~integralCacheFn=_ => None,
@ -202,27 +218,12 @@ module T = Dist({
Discrete.T.mapYResult(~fn, t.discrete), Discrete.T.mapYResult(~fn, t.discrete),
Continuous.T.mapYResult(~fn, t.continuous), Continuous.T.mapYResult(~fn, t.continuous),
)->E.R2.fmap(((discreteMapped, continuousMapped)) => { )->E.R2.fmap(((discreteMapped, continuousMapped)) => {
let yMappedDiscrete: PointSetTypes.discreteShape = createMixedFromContinuousDiscrete(
discreteMapped ~integralCacheFn,
|> Discrete.updateIntegralSumCache( ~integralSumCacheFn,
E.O.bind(t.discrete.integralSumCache, integralSumCacheFn), t,
) discreteMapped,
|> Discrete.updateIntegralCache(E.O.bind(t.discrete.integralCache, integralCacheFn)) continuousMapped,
let yMappedContinuous: PointSetTypes.continuousShape =
continuousMapped
|> Continuous.updateIntegralSumCache(
E.O.bind(t.continuous.integralSumCache, integralSumCacheFn),
)
|> Continuous.updateIntegralCache(E.O.bind(t.continuous.integralCache, integralCacheFn))
(
{
discrete: yMappedDiscrete,
continuous: yMappedContinuous,
integralSumCache: E.O.bind(t.integralSumCache, integralSumCacheFn),
integralCache: E.O.bind(t.integralCache, integralCacheFn),
}: t
) )
}) })
} }