Adds a "range" function

This commit is contained in:
Roman Galochkin 2020-02-25 09:16:14 +03:00
parent e55993527e
commit 43ef31e772
2 changed files with 16 additions and 1 deletions

View File

@ -1,3 +1,5 @@
exception RangeWrong(string);
let interpolate = let interpolate =
(xMin: float, xMax: float, yMin: float, yMax: float, xIntended: float) (xMin: float, xMax: float, yMin: float, yMax: float, xIntended: float)
: float => { : float => {
@ -7,10 +9,22 @@ let interpolate =
}; };
// @todo: To test! // @todo: To test!
/* https://bucklescript.github.io/bucklescript/api/Belt.html */
let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j); let sum = Belt.Array.reduce(_, 0., (i, j) => i +. j);
let mean = a => sum(a) /. (Array.length(a) |> float_of_int); let mean = a => sum(a) /. (Array.length(a) |> float_of_int);
let min = Belt.Array.reduce(_, 0., (i, j) => i < j ? i : j); 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 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 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 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);
};
};

View File

@ -14,6 +14,7 @@ function interpolate(xMin, xMax, yMin, yMax, xIntended) {
} }
/** /**
* @Done
* This should return an array of n evenly-spaced items * This should return an array of n evenly-spaced items
* between min and max, including min and max. * between min and max, including min and max.
* range(1,5,3) = [1, 3, 5]; * range(1,5,3) = [1, 3, 5];