Draft of Validates for XYShape

This commit is contained in:
Ozzie Gooen 2022-04-28 11:39:29 -04:00
parent dde28e54f0
commit d1ffac492c
2 changed files with 19 additions and 5 deletions

View File

@ -476,6 +476,7 @@ module A = {
} }
module Floats = { module Floats = {
type t = array<float>
let mean = Jstat.mean let mean = Jstat.mean
let geomean = Jstat.geomean let geomean = Jstat.geomean
let mode = Jstat.mode let mode = Jstat.mode
@ -491,8 +492,11 @@ module A = {
r r
} }
let isSorted = (ar: array<float>): bool => let getNonFinite = (t: t) => Belt.Array.getBy(t, r => !Js.Float.isFinite(r))
reduce(zip(ar, tail(ar)), true, (acc, (first, second)) => acc && first < second) 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. //Passing true for the exclusive parameter excludes both endpoints of the range.
//https://jstat.github.io/all.html //https://jstat.github.io/all.html
@ -500,8 +504,8 @@ module A = {
// Gives an array with all the differences between values // Gives an array with all the differences between values
// diff([1,5,3,7]) = [4,-2,4] // diff([1,5,3,7]) = [4,-2,4]
let diff = (arr: array<float>): array<float> => let diff = (t: t): array<float> =>
Belt.Array.zipBy(arr, Belt.Array.sliceToEnd(arr, 1), (left, right) => right -. left) Belt.Array.zipBy(t, Belt.Array.sliceToEnd(t, 1), (left, right) => right -. left)
exception RangeError(string) exception RangeError(string)
let range = (min: float, max: float, n: int): array<float> => let range = (min: float, max: float, n: int): array<float> =>
@ -574,7 +578,7 @@ module A = {
} }
} }
} }
module Sorted = Floats.Sorted; module Sorted = Floats.Sorted
} }
module A2 = { module A2 = {

View File

@ -62,6 +62,16 @@ module T = {
let toJs = (t: t) => {"xs": t.xs, "ys": t.ys} 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 = { module Ts = {
type t = T.ts type t = T.ts
let minX = (t: t) => t |> E.A.fmap(T.minX) |> E.A.Floats.min let minX = (t: t) => t |> E.A.fmap(T.minX) |> E.A.Floats.min