Simple testing and manipulations

This commit is contained in:
Ozzie Gooen 2020-02-18 15:50:36 +00:00
parent 9e5561a7b8
commit db023c0bef
6 changed files with 2417 additions and 68 deletions

View File

@ -0,0 +1,46 @@
open Jest;
open Expect;
let shape: DistributionTypes.xyShape = {
xs: [|1., 4., 8.|],
ys: [|8., 9., 2.|],
};
open Shape;
describe("Shape", () =>
describe("XYShape", () => {
test("#ySum", () =>
expect(XYShape.ySum(shape)) |> toEqual(19.0)
);
test("#volume", () => {
let shape: DistributionTypes.xyShape = {
xs: [|1., 5., 10.|],
ys: [|1., 2., 2.|],
};
expect(XYShape.volume(shape)) |> toEqual(Some(7.0));
});
test("#integral", () => {
let expected: DistributionTypes.xyShape = {
xs: [|1., 4., 8.|],
ys: [|8., 17., 19.|],
};
expect(XYShape.volum2(shape)) |> toEqual(Some(expected));
});
test("#derivative", () => {
let expected: DistributionTypes.xyShape = {
xs: [|1., 4., 8.|],
ys: [|8., 1., 1.|],
};
expect(XYShape.derivative(shape)) |> toEqual(expected);
});
// test("#both", () => {
// let expected: DistributionTypes.xyShape = {
// xs: [|1., 4., 8.|],
// ys: [|8., 1., 1.|],
// };
// expect(shape |> XYShape.derivative |> XYShape.integral)
// |> toEqual(shape);
// });
})
);

View File

@ -11,6 +11,11 @@
"dir": "showcase",
"type": "dev",
"subdirs": true
},
{
"dir": "__tests__",
"type": "dev",
"subdirs": true
}
],
"bsc-flags": ["-bs-super-errors", "-bs-no-version-header"],
@ -21,6 +26,7 @@
"suffix": ".bs.js",
"namespace": true,
"bs-dependencies": [
"@glennsl/bs-jest",
"@foretold/components",
"bs-ant-design-alt",
"reason-react",

View File

@ -13,7 +13,10 @@
"server": "moduleserve ./ --port 8000",
"predeploy": "parcel build ./src/index.html --no-source-maps --no-autoinstall",
"deploy": "gh-pages -d dist",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest",
"test:ci": "yarn jest",
"watch:test": "jest --watchAll",
"watch:s": "yarn jest -- Converter_test --watch"
},
"keywords": [
"BuckleScript",
@ -26,13 +29,16 @@
"@foretold/cdf": "^1.0.15",
"@foretold/components": "^0.0.3",
"@foretold/guesstimator": "^1.0.10",
"@glennsl/bs-jest": "^0.4.9",
"antd": "3.17.0",
"autoprefixer": "^9.7.4",
"babel-jest": "^25.1.0",
"bs-ant-design-alt": "2.0.0-alpha.31",
"bs-css": "^11.0.0",
"bs-moment": "0.4.4",
"bs-reform": "9.7.1",
"d3": "^5.15.0",
"jest": "^25.1.0",
"lenses-ppx": "4.0.0",
"less": "^3.10.3",
"lodash": "^4.17.15",
@ -57,4 +63,4 @@
"react": "./node_modules/react",
"react-dom": "./node_modules/react-dom"
}
}
}

View File

@ -25,6 +25,7 @@ module XYShape = {
let ySum = yFold((a, b) => a +. b);
let fromArray = ((xs, ys)): t => {xs, ys};
let fromArrays = (xs, ys): t => {xs, ys};
let transverse = (fn, p: t) => {
@ -33,7 +34,8 @@ module XYShape = {
->Belt.Array.reduce([||], (items, (x, y)) =>
switch (_lastElement(items)) {
| Some((_, yLast)) =>
Belt.Array.concat(items, [|(x, fn(y, yLast))|])
Js.log3(y, yLast, fn(y, yLast));
Belt.Array.concat(items, [|(x, fn(y, yLast))|]);
| None => [|(x, y)|]
}
)
@ -41,19 +43,68 @@ module XYShape = {
fromArrays(xs, ys);
};
type zippedRange = ((float, float), (float, float));
let inRanges = (fn, t: t) => {
let ranges: Belt.Result.t(array(zippedRange), string) =
Belt.Array.zip(t.xs, t.ys) |> E.A.toRanges;
ranges |> E.R.toOption |> E.O.fmap(fn);
};
let sum = Belt.Array.reduce(_, 0., (a, b) => a +. b);
let volume = {
let assumeLastY = (((lastX, lastY), (nextX, _)): zippedRange) =>
(nextX -. lastX) *. lastY;
inRanges((inRanges: array(zippedRange)) =>
Belt.Array.map(inRanges, assumeLastY) |> sum
);
};
let volumeTriangle = {
let assumeLastY = (((lastX, lastY), (nextX, nextY)): zippedRange) =>
(nextX -. lastX) *. (lastY -. nextY) /. 2.;
inRanges((inRanges: array(zippedRange)) =>
Belt.Array.map(inRanges, assumeLastY) |> sum
);
};
let volum2 = {
let assumeLastY = (((lastX, lastY), (nextX, _)): zippedRange) => (
nextX,
(nextX -. lastX) *. lastY,
);
inRanges((inRanges: array(zippedRange)) =>
Belt.Array.map(inRanges, assumeLastY) |> Belt.Array.unzip |> fromArray
);
};
let diff = {
let assumeLastY = (((lastX, lastY), (nextX, _)): zippedRange) => (
nextX,
(lastY -. lastY) /. (nextX -. lastX),
);
inRanges((inRanges: array(zippedRange)) =>
Belt.Array.map(inRanges, assumeLastY) |> Belt.Array.unzip |> fromArray
);
};
let getY = (t: t, x: float) => x;
let findY = (t: t, x: float) => x;
let integral = transverse((aCurrent, aLast) => aCurrent +. aLast);
let derivative = transverse((aCurrent, aLast) => aCurrent -. aLast);
let massWithin = (t: t, left: pointInRange, right: pointInRange) => {
switch (left, right) {
| (Unbounded, Unbounded) => t |> ySum
| (Unbounded, X(f)) => t |> integral |> getY(_, f)
| (X(f), Unbounded) => ySum(t) -. getY(integral(t), f)
| (X(l), X(r)) => getY(integral(t), r) -. getY(integral(t), l)
};
};
// let massWithin = (t: t, left: pointInRange, right: pointInRange) => {
// switch (left, right) {
// | (Unbounded, Unbounded) => t |> ySum
// | (Unbounded, X(f)) => t |> integral |> getY(t, 3.0)
// | (X(f), Unbounded) => ySum(t) -. getY(integral(t), f)
// | (X(l), X(r)) => getY(integral(t), r) -. getY(integral(t), l)
// };
// };
};
module Continuous = {

View File

@ -114,6 +114,11 @@ module R = {
let id = e => e |> result(U.id, U.id);
let fmap = Rationale.Result.fmap;
let bind = Rationale.Result.bind;
let toOption = (e: Belt.Result.t('a, 'b)) =>
switch (e) {
| Ok(r) => Some(r)
| Error(_) => None
};
};
let safe_fn_of_string = (fn, s: string): option('a) =>
@ -217,6 +222,20 @@ module A = {
let concatMany = Belt.Array.concatMany;
let keepMap = Belt.Array.keepMap;
let stableSortBy = Belt.SortArray.stableSortBy;
let toRanges = (a: array('a)) =>
switch (a |> Belt.Array.length) {
| 0
| 1 => Belt.Result.Error("Must be at least 2 elements")
| n =>
Belt.Array.makeBy(n - 1, r => r)
|> Belt.Array.map(_, index =>
(
Belt.Array.getUnsafe(a, index),
Belt.Array.getUnsafe(a, index + 1),
)
)
|> Rationale.Result.return
};
let asList = (f: list('a) => list('a), r: array('a)) =>
r |> to_list |> f |> of_list;

2333
yarn.lock

File diff suppressed because it is too large Load Diff