From 43ef31e7723e41601f4c9c5c6996515a2f83143b Mon Sep 17 00:00:00 2001 From: Roman Galochkin Date: Tue, 25 Feb 2020 09:16:14 +0300 Subject: [PATCH] Adds a "range" function --- src/utility/lib/Functions.re | 16 +++++++++++++++- src/utility/lib/functions.js | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/utility/lib/Functions.re b/src/utility/lib/Functions.re index d6db7a03..a887be60 100644 --- a/src/utility/lib/Functions.re +++ b/src/utility/lib/Functions.re @@ -1,3 +1,5 @@ +exception RangeWrong(string); + let interpolate = (xMin: float, xMax: float, yMin: float, yMax: float, xIntended: float) : float => { @@ -7,10 +9,22 @@ let interpolate = }; // @todo: To test! -/* https://bucklescript.github.io/bucklescript/api/Belt.html */ let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j); let mean = a => sum(a) /. (Array.length(a) |> float_of_int); let min = Belt.Array.reduce(_, 0., (i, j) => i < j ? i : j); let max = Belt.Array.reduce(_, 0., (i, j) => i > j ? i : j); let up = (a, b) => Array.make(b - a, a) |> Array.mapi((i, c) => c + i); let down = (a, b) => Array.make(a - b, a) |> Array.mapi((i, c) => c - i); +let range = (min: float, max: float, n: int): array(float) => { + switch (n) { + | 0 => [||] + | 1 => [|min|] + | 2 => [|min, max|] + | _ when min == max => Belt.Array.make(n, min) + | _ when n < 0 => raise(RangeWrong("n is less then zero")) + | _ when min > max => raise(RangeWrong("Min values is less then max")) + | _ => + let diff = max -. min; + Belt.Array.makeBy(n, i => float_of_int(i) *. diff); + }; +}; diff --git a/src/utility/lib/functions.js b/src/utility/lib/functions.js index a234c799..a4353e52 100644 --- a/src/utility/lib/functions.js +++ b/src/utility/lib/functions.js @@ -14,6 +14,7 @@ function interpolate(xMin, xMax, yMin, yMax, xIntended) { } /** + * @Done * This should return an array of n evenly-spaced items * between min and max, including min and max. * range(1,5,3) = [1, 3, 5];