Step 4 - first step
This commit is contained in:
parent
8448b388fa
commit
f929ccd942
15
__tests__/CDFunctor__Test.re
Normal file
15
__tests__/CDFunctor__Test.re
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
open Jest;
|
||||||
|
open Expect;
|
||||||
|
|
||||||
|
describe("CDF", () => {
|
||||||
|
module CDF =
|
||||||
|
CDFunctor.Make({
|
||||||
|
let shape: DistributionTypes.xyShape =
|
||||||
|
CDFunctor.order({xs: [|1., 4., 8.|], ys: [|8., 9., 2.|]});
|
||||||
|
});
|
||||||
|
test("order", () => {
|
||||||
|
Js.log(CDFunctor.order({xs: [|1., 4., 8.|], ys: [|8., 9., 2.|]}));
|
||||||
|
Js.log(CDFunctor.order({xs: [|10., 5., 12.|], ys: [|8., 9., 2.|]}));
|
||||||
|
expect(19.0) |> toEqual(19.0);
|
||||||
|
});
|
||||||
|
});
|
|
@ -2,6 +2,17 @@ module type Config = {let shape: DistributionTypes.xyShape;};
|
||||||
|
|
||||||
exception ShapeWrong(string);
|
exception ShapeWrong(string);
|
||||||
|
|
||||||
|
let order = (shape: DistributionTypes.xyShape): DistributionTypes.xyShape => {
|
||||||
|
let xy =
|
||||||
|
shape.xs
|
||||||
|
|> Array.mapi((i, x) => [x, shape.ys[i]])
|
||||||
|
|> Belt.SortArray.stableSortBy(_, ([a, _], [b, _]) => a > b ? 1 : (-1));
|
||||||
|
{
|
||||||
|
xs: xy |> Array.map(([x, _]) => x),
|
||||||
|
ys: xy |> Array.map(([_, y]) => y),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
module Make = (Config: Config) => {
|
module Make = (Config: Config) => {
|
||||||
let validateHasLength = (): bool => Array.length(Config.shape.xs) > 0;
|
let validateHasLength = (): bool => Array.length(Config.shape.xs) > 0;
|
||||||
let validateSize = (): bool =>
|
let validateSize = (): bool =>
|
||||||
|
@ -12,12 +23,4 @@ module Make = (Config: Config) => {
|
||||||
if (!validateSize()) {
|
if (!validateSize()) {
|
||||||
raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes."));
|
raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes."));
|
||||||
};
|
};
|
||||||
|
|
||||||
let order = (): DistributionTypes.xyShape => {
|
|
||||||
let xy =
|
|
||||||
Config.shape.xs
|
|
||||||
|> Array.mapi((i, x) => [x, Config.shape.ys[i]])
|
|
||||||
|> Belt.SortArray.stableSortBy(_, ([a], [b]) => a > b ? (-1) : 1);
|
|
||||||
{xs: xy |> Array.map(([x]) => x), ys: xy |> Array.map(([_, y]) => y)};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
module CDFConfig = {
|
|
||||||
let shape: DistributionTypes.xyShape = {
|
|
||||||
xs: [|1., 4., 8.|],
|
|
||||||
ys: [|8., 9., 2.|],
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
module CDF = CDFunctor.Make(CDFConfig);
|
|
Loading…
Reference in New Issue
Block a user