Step 4 - first step

This commit is contained in:
Roman Galochkin 2020-02-21 16:17:32 +03:00
parent 8448b388fa
commit f929ccd942
3 changed files with 26 additions and 16 deletions

View 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);
});
});

View File

@ -2,6 +2,17 @@ module type Config = {let shape: DistributionTypes.xyShape;};
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) => {
let validateHasLength = (): bool => Array.length(Config.shape.xs) > 0;
let validateSize = (): bool =>
@ -12,12 +23,4 @@ module Make = (Config: Config) => {
if (!validateSize()) {
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)};
};
};

View File

@ -1,8 +0,0 @@
module CDFConfig = {
let shape: DistributionTypes.xyShape = {
xs: [|1., 4., 8.|],
ys: [|8., 9., 2.|],
};
};
module CDF = CDFunctor.Make(CDFConfig);