From 5db63fbe9ffe2e1326449ba72d6f7508f4d4c6d3 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 21 Sep 2022 01:42:54 +0400 Subject: [PATCH 1/3] fastSort in toPointSet conversion --- .../SampleSetDist/SampleSetDist_ToPointSet.res | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res index 81bfc760..7d5498f7 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res @@ -39,6 +39,13 @@ module Internals = { module T = { type t = array + let fastSort = (samples: t): t => { + let typedSamples = samples->Js.TypedArray2.Float64Array.make->Js.TypedArray2.Float64Array.sortInPlace + // why is there no standard function in Resctipt for this? + let typedToArray: Js.TypedArray2.Float64Array.t => t = %raw(`a => Array.from(a)`) + typedToArray(typedSamples) + } + let xWidthToUnitWidth = (samples, outputXYPoints, xWidth) => { let xyPointRange = E.A.Sorted.range(samples)->E.O2.default(0.0) let xyPointWidth = xyPointRange /. float_of_int(outputXYPoints) @@ -62,7 +69,7 @@ let toPointSetDist = ( ~samplingInputs: SamplingInputs.samplingInputs, (), ): Internals.Types.outputs => { - let samples = samples->Js.Array2.copy->Js.Array2.sortInPlaceWith(compare) + let samples = samples->Internals.T.fastSort let minDiscreteToKeep = MagicNumbers.ToPointSet.minDiscreteToKeep(samples) let (continuousPart, discretePart) = E.A.Floats.Sorted.splitContinuousAndDiscreteForMinWeight( From 59d38f7885240d97a41526401ca4803a39f1ccb7 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 21 Sep 2022 03:17:25 +0400 Subject: [PATCH 2/3] E.A.Floats.sort uses typed arrays --- .../SampleSetDist/SampleSetDist_ToPointSet.res | 9 +-------- packages/squiggle-lang/src/rescript/Utility/E/E_A.res | 7 ++++--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res index 7d5498f7..4cf2b868 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist_ToPointSet.res @@ -39,13 +39,6 @@ module Internals = { module T = { type t = array - let fastSort = (samples: t): t => { - let typedSamples = samples->Js.TypedArray2.Float64Array.make->Js.TypedArray2.Float64Array.sortInPlace - // why is there no standard function in Resctipt for this? - let typedToArray: Js.TypedArray2.Float64Array.t => t = %raw(`a => Array.from(a)`) - typedToArray(typedSamples) - } - let xWidthToUnitWidth = (samples, outputXYPoints, xWidth) => { let xyPointRange = E.A.Sorted.range(samples)->E.O2.default(0.0) let xyPointWidth = xyPointRange /. float_of_int(outputXYPoints) @@ -69,7 +62,7 @@ let toPointSetDist = ( ~samplingInputs: SamplingInputs.samplingInputs, (), ): Internals.Types.outputs => { - let samples = samples->Internals.T.fastSort + let samples = samples->E.A.Floats.sort let minDiscreteToKeep = MagicNumbers.ToPointSet.minDiscreteToKeep(samples) let (continuousPart, discretePart) = E.A.Floats.Sorted.splitContinuousAndDiscreteForMinWeight( diff --git a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res index 46ada15f..dd7b7c90 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res +++ b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res @@ -229,9 +229,10 @@ module Floats = { let floatCompare: (float, float) => int = compare let sort = t => { - let r = t - r |> Array.fast_sort(floatCompare) - r + let typed = t->Js.TypedArray2.Float64Array.make->Js.TypedArray2.Float64Array.sortInPlace + // why is there no standard function in Resctipt for this? + let typedToArray: Js.TypedArray2.Float64Array.t => t = %raw(`a => Array.from(a)`) + typedToArray(typed) } let getNonFinite = (t: t) => Belt.Array.getBy(t, r => !Js.Float.isFinite(r)) From 824749976e98af72ac2faa243a0f5e9f97ee6bc3 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 21 Sep 2022 03:19:39 +0400 Subject: [PATCH 3/3] tweaking names --- packages/squiggle-lang/src/rescript/Utility/E/E_A.res | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res index dd7b7c90..14ba187c 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res +++ b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res @@ -229,10 +229,11 @@ module Floats = { let floatCompare: (float, float) => int = compare let sort = t => { - let typed = t->Js.TypedArray2.Float64Array.make->Js.TypedArray2.Float64Array.sortInPlace + let typedArray = t->Js.TypedArray2.Float64Array.make + typedArray->Js.TypedArray2.Float64Array.sortInPlace->ignore // why is there no standard function in Resctipt for this? - let typedToArray: Js.TypedArray2.Float64Array.t => t = %raw(`a => Array.from(a)`) - typedToArray(typed) + let typedArrayToArray: Js.TypedArray2.Float64Array.t => t = %raw(`a => Array.from(a)`) + typedArrayToArray(typedArray) } let getNonFinite = (t: t) => Belt.Array.getBy(t, r => !Js.Float.isFinite(r))