Skip to content

Commit

Permalink
Remove remaining |> usage except some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mlochbaum committed Oct 24, 2022
1 parent a89599f commit 25e7837
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ let toDiscretePointMassesFromTriangulars = (
s: XYShape.T.t,
): pointMassesWithMoments => {
// TODO: what if there is only one point in the distribution?
let n = s |> XYShape.T.length
let n = s->XYShape.T.length
// first, double up the leftmost and rightmost points:
let {xs, ys}: XYShape.T.t = s
Js.Array.unshift(xs[0], xs) |> ignore
Js.Array.unshift(ys[0], ys) |> ignore
Js.Array.push(xs[n - 1], xs) |> ignore
Js.Array.push(ys[n - 1], ys) |> ignore
Js.Array.unshift(xs[0], xs)->ignore
Js.Array.unshift(ys[0], ys)->ignore
Js.Array.push(xs[n - 1], xs)->ignore
Js.Array.push(ys[n - 1], ys)->ignore
let n = E.A.length(xs)
// squares and neighbourly products of the xs
let xsSq: array<float> = Belt.Array.makeUninitializedUnsafe(n)
let xsProdN1: array<float> = Belt.Array.makeUninitializedUnsafe(n - 1)
let xsProdN2: array<float> = Belt.Array.makeUninitializedUnsafe(n - 2)
for i in 0 to n - 1 {
Belt.Array.set(xsSq, i, xs[i] *. xs[i]) |> ignore
Belt.Array.set(xsSq, i, xs[i] *. xs[i])->ignore
()
}
for i in 0 to n - 2 {
Belt.Array.set(xsProdN1, i, xs[i] *. xs[i + 1]) |> ignore
Belt.Array.set(xsProdN1, i, xs[i] *. xs[i + 1])->ignore
()
}
for i in 0 to n - 3 {
Belt.Array.set(xsProdN2, i, xs[i] *. xs[i + 2]) |> ignore
Belt.Array.set(xsProdN2, i, xs[i] *. xs[i + 2])->ignore
()
}
// means and variances
Expand All @@ -47,7 +47,7 @@ let toDiscretePointMassesFromTriangulars = (

if inverse {
for i in 1 to n - 2 {
Belt.Array.set(masses, i - 1, (xs[i + 1] -. xs[i - 1]) *. ys[i] /. 2.) |> ignore
Belt.Array.set(masses, i - 1, (xs[i + 1] -. xs[i - 1]) *. ys[i] /. 2.)->ignore

// this only works when the whole triange is either on the left or on the right of zero
let a = xs[i - 1]
Expand All @@ -63,20 +63,20 @@ let toDiscretePointMassesFromTriangulars = (
2. *. (log(c /. a) /. (a -. c) +. b *. log(b /. c) /. (b -. c)) /. (a -. b) -.
inverseMean ** 2.

Belt.Array.set(means, i - 1, inverseMean) |> ignore
Belt.Array.set(means, i - 1, inverseMean)->ignore

Belt.Array.set(variances, i - 1, inverseVar) |> ignore
Belt.Array.set(variances, i - 1, inverseVar)->ignore
()
}

{n: n - 2, masses, means, variances}
} else {
for i in 1 to n - 2 {
// area of triangle = width * height / 2
Belt.Array.set(masses, i - 1, (xs[i + 1] -. xs[i - 1]) *. ys[i] /. 2.) |> ignore
Belt.Array.set(masses, i - 1, (xs[i + 1] -. xs[i - 1]) *. ys[i] /. 2.)->ignore

// means of triangle = (a + b + c) / 3
Belt.Array.set(means, i - 1, (xs[i - 1] +. xs[i] +. xs[i + 1]) /. 3.) |> ignore
Belt.Array.set(means, i - 1, (xs[i - 1] +. xs[i] +. xs[i + 1]) /. 3.)->ignore

// variance of triangle = (a^2 + b^2 + c^2 - ab - ac - bc) / 18
Belt.Array.set(
Expand All @@ -88,7 +88,7 @@ let toDiscretePointMassesFromTriangulars = (
xsProdN1[i - 1] -.
xsProdN1[i] -.
xsProdN2[i - 1]) /. 18.,
) |> ignore
)->ignore
()
}
{n: n - 2, masses, means, variances}
Expand Down Expand Up @@ -127,7 +127,7 @@ let combineShapesContinuousContinuous = (
for i in 0 to t1m.n - 1 {
for j in 0 to t2m.n - 1 {
let k = i * t2m.n + j
Belt.Array.set(masses, k, t1m.masses[i] *. t2m.masses[j]) |> ignore
Belt.Array.set(masses, k, t1m.masses[i] *. t2m.masses[j])->ignore

let mean = combineMeansFn(t1m.means[i], t2m.means[j])
let variance = combineVariancesFn(
Expand All @@ -136,8 +136,8 @@ let combineShapesContinuousContinuous = (
t1m.means[i],
t2m.means[j],
)
Belt.Array.set(means, k, mean) |> ignore
Belt.Array.set(variances, k, variance) |> ignore
Belt.Array.set(means, k, mean)->ignore
Belt.Array.set(variances, k, variance)->ignore
// update bounds
let minX = mean -. 2. *. sqrt(variance) *. 1.644854
let maxX = mean +. 2. *. sqrt(variance) *. 1.644854
Expand Down Expand Up @@ -168,7 +168,7 @@ let combineShapesContinuousContinuous = (
masses[j] *.
exp(-.(dx ** 2.) /. (2. *. variances[j])) /.
sqrt(2. *. 3.14159276 *. variances[j])
Belt.Array.set(outputYs, i, outputYs[i] +. contribution) |> ignore
Belt.Array.set(outputYs, i, outputYs[i] +. contribution)->ignore
}
}
}
Expand All @@ -195,8 +195,8 @@ let combineShapesContinuousDiscrete = (
discreteShape: PointSetTypes.xyShape,
~discretePosition: argumentPosition,
): PointSetTypes.xyShape => {
let t1n = continuousShape |> XYShape.T.length
let t2n = discreteShape |> XYShape.T.length
let t1n = continuousShape->XYShape.T.length
let t2n = discreteShape->XYShape.T.length

// each x pair is added/subtracted
let opFunc = Operation.Convolution.toFn(op)
Expand All @@ -222,9 +222,9 @@ let combineShapesContinuousDiscrete = (
fn(continuousShape.xs[i], discreteShape.xs[j]),
continuousShape.ys[i] *. discreteShape.ys[j],
),
) |> ignore
)->ignore
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore
Belt.Array.set(outXYShapes, j, dxyShape)->ignore
()
}
| #Multiply =>
Expand All @@ -243,10 +243,10 @@ let combineShapesContinuousDiscrete = (
fn(continuousShape.xs[i], discreteShape.xs[j]),
continuousShape.ys[i] *. discreteShape.ys[j] /. Js.Math.abs_float(discreteShape.xs[j]),
),
) |> ignore
)->ignore
()
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore
Belt.Array.set(outXYShapes, j, dxyShape)->ignore
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let buildSimple = (
continuous->E.O.defaultFn(() => Continuous.make(~integralSumCache=Some(0.0), {xs: [], ys: []}))
let discrete =
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
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) {
| (0 | 1, 0) => None
| (0 | 1, _) => Some(Discrete(discrete))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
let {iqr_percentile, nrd0_lo_denominator, one, nrd0_coef, nrd_coef, nrd_fractionalPower} = module(
MagicNumbers.SampleSetBandwidth
)
let len = x => E.A.length(x) |> float_of_int
let len = x => E.A.length(x)->float_of_int

let iqr = x =>
Jstat.percentile(x, iqr_percentile, true) -. Jstat.percentile(x, 1.0 -. iqr_percentile, true)
Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/rescript/FR/FR_GenericDist.res
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Old = {
}

let parseNumberArray = (ags: array<Reducer_T.value>): Belt.Result.t<array<float>, string> =>
E.A.fmap(ags, parseNumber) |> E.A.R.firstErrorOrOpen
E.A.fmap(ags, parseNumber)->E.A.R.firstErrorOrOpen

let parseDist = (args: Reducer_T.value): Belt.Result.t<DistributionTypes.genericDist, string> =>
switch args {
Expand All @@ -87,7 +87,7 @@ module Old = {
let parseDistributionArray = (ags: array<Reducer_T.value>): Belt.Result.t<
array<DistributionTypes.genericDist>,
string,
> => E.A.fmap(ags, parseDist) |> E.A.R.firstErrorOrOpen
> => E.A.fmap(ags, parseDist)->E.A.R.firstErrorOrOpen

let mixtureWithGivenWeights = (
distributions: array<DistributionTypes.genericDist>,
Expand Down
6 changes: 0 additions & 6 deletions packages/squiggle-lang/src/rescript/Utility/E/E_JsArray.res

This file was deleted.

2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/rescript/Utility/E/E_O.res
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let apply = (o, a) =>
| Some(f) => bind(a, b => some(f(b)))
| _ => None
}
let flatApply = (fn, b) => apply(fn, Some(b)) |> flatten
let flatApply = (fn, b) => apply(fn, Some(b))->flatten

let toBool = opt =>
switch opt {
Expand Down
1 change: 0 additions & 1 deletion packages/squiggle-lang/src/rescript/Utility/E/E_R.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let result = (okF, errF, r) =>
| Ok(a) => okF(a)
| Error(err) => errF(err)
}
let id = e => e |> result(e => e, e => e)
let isOk = Belt.Result.isOk
let getError = (r: result<'a, 'b>) =>
switch r {
Expand Down

0 comments on commit 25e7837

Please sign in to comment.