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 = {
type t = array<float>
let mean = Jstat.mean
let geomean = Jstat.geomean
let mode = Jstat.mode
@ -491,8 +492,11 @@ module A = {
r
}
let isSorted = (ar: array<float>): 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<float>): array<float> =>
Belt.Array.zipBy(arr, Belt.Array.sliceToEnd(arr, 1), (left, right) => right -. left)
let diff = (t: t): array<float> =>
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<float> =>
@ -574,7 +578,7 @@ module A = {
}
}
}
module Sorted = Floats.Sorted;
module Sorted = Floats.Sorted
}
module A2 = {

View File

@ -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