From d1ffac492cb0c4147ace407c3f36418081459887 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Thu, 28 Apr 2022 11:39:29 -0400 Subject: [PATCH] Draft of Validates for XYShape --- packages/squiggle-lang/src/rescript/Utility/E.res | 14 +++++++++----- .../squiggle-lang/src/rescript/Utility/XYShape.res | 10 ++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 9c11426a..45051578 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -476,6 +476,7 @@ module A = { } module Floats = { + type t = array let mean = Jstat.mean let geomean = Jstat.geomean let mode = Jstat.mode @@ -491,8 +492,11 @@ module A = { r } - let isSorted = (ar: array): bool => - reduce(zip(ar, tail(ar)), true, (acc, (first, second)) => acc && first < second) + let getNonFinite = (t: t) => Belt.Array.getBy(t, r => !Js.Float.isFinite(r)) + let getBelowZero = (t: t) => Belt.Array.getBy(t, r => r < 0.0) + + let isSorted = (t: t): bool => + reduce(zip(t, tail(t)), true, (acc, (first, second)) => acc && first < second) //Passing true for the exclusive parameter excludes both endpoints of the range. //https://jstat.github.io/all.html @@ -500,8 +504,8 @@ module A = { // Gives an array with all the differences between values // diff([1,5,3,7]) = [4,-2,4] - let diff = (arr: array): array => - Belt.Array.zipBy(arr, Belt.Array.sliceToEnd(arr, 1), (left, right) => right -. left) + let diff = (t: t): array => + Belt.Array.zipBy(t, Belt.Array.sliceToEnd(t, 1), (left, right) => right -. left) exception RangeError(string) let range = (min: float, max: float, n: int): array => @@ -574,7 +578,7 @@ module A = { } } } - module Sorted = Floats.Sorted; + module Sorted = Floats.Sorted } module A2 = { diff --git a/packages/squiggle-lang/src/rescript/Utility/XYShape.res b/packages/squiggle-lang/src/rescript/Utility/XYShape.res index 97974884..e22dc5fd 100644 --- a/packages/squiggle-lang/src/rescript/Utility/XYShape.res +++ b/packages/squiggle-lang/src/rescript/Utility/XYShape.res @@ -62,6 +62,16 @@ module T = { let toJs = (t: t) => {"xs": t.xs, "ys": t.ys} } +module Validates = { + type t = T.t + let areXsSorted = (t:t) => E.A.Floats.isSorted(T.xs(t)) + let validate = (t:t) => { + let xsNotSorted = E.A.Floats.isSorted(T.xs(t)) ? None : Some("Xs are not sorted") + let xsNotFinite = E.A.Floats.getNonFinite(T.xs(t)) |> E.O.fmap(r => `Xs contain non-finite values: ${E.Float.toString(r)}`) + let ysNotFinite = E.A.Floats.getNonFinite(T.ys(t)) |> E.O.fmap(r => `Ys contain non-finite values: ${E.Float.toString(r)}`) + } +} + module Ts = { type t = T.ts let minX = (t: t) => t |> E.A.fmap(T.minX) |> E.A.Floats.min