Added math functions

This commit is contained in:
Ozzie Gooen 2020-02-06 21:05:13 +00:00
parent eecfd765f3
commit 8fdcc99231
11 changed files with 146 additions and 62 deletions

View File

@ -1,9 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ReasonReact Examples</title>
</head>
<body>
<script>
// stub a variable ReactJS checks. ReactJS assumes you're using a bundler, NodeJS or similar system that provides it the `process.env.NODE_ENV` variable.
@ -19,4 +21,5 @@
<!-- Our little watcher. Super clean. Check it out! -->
<script src="/watcher.js"></script>
</body>
</html>
</html>

View File

@ -1,5 +1,10 @@
'use strict';
var Math$ReasonReactExamples = require("./Math.bs.js");
function yearDiff(year) {
return year - 2020.0;
}
function yearlyMeanGrowthRateIfNotClosed(group) {
return {
@ -8,29 +13,63 @@ function yearlyMeanGrowthRateIfNotClosed(group) {
};
}
function yearlyChanceOfClosing(group) {
return 0.1;
function calculateDifference(currentValue, yearInQuestion, y) {
var yearDiff = yearInQuestion - 2020.0;
var meanDiff = Math.pow(y.meanDiff, yearDiff);
var stdDevDiff = Math.pow(y.meanDiff, yearDiff);
return Math$ReasonReactExamples.normal(currentValue * meanDiff, 0.2 * stdDevDiff);
}
function yearlyStdevGrowthRate(group, year, parameter) {
function currentValue(group, parameter) {
if (group) {
return /* tuple */[
30,
30
];
switch (group[0]) {
case /* ANIMAL_WELFARE */0 :
if (parameter) {
return 2300000.0;
} else {
return 300000.0;
}
case /* GLOBAL_HEALTH */1 :
if (parameter) {
return 500000.0;
} else {
return 1000000.0;
}
case /* LONG_TERM_FUTURE */2 :
if (parameter) {
return 120000.0;
} else {
return 600000.0;
}
case /* META */3 :
if (parameter) {
return 830000.0;
} else {
return 9300000.0;
}
}
} else {
return /* tuple */[
50,
10
];
return currentValue(/* Fund */[/* ANIMAL_WELFARE */0], parameter) + currentValue(/* Fund */[/* GLOBAL_HEALTH */1], parameter) + currentValue(/* Fund */[/* LONG_TERM_FUTURE */2], parameter) + currentValue(/* Fund */[/* META */3], parameter);
}
}
var PayoutsIfAround = {
currentYear: 2020,
firstYearStdDev: 0.2,
yearDiff: yearDiff,
yearlyMeanGrowthRateIfNotClosed: yearlyMeanGrowthRateIfNotClosed,
yearlyChanceOfClosing: yearlyChanceOfClosing,
yearlyStdevGrowthRate: yearlyStdevGrowthRate
calculateDifference: calculateDifference,
currentValue: currentValue
};
function calculate(group, year, parameter) {
return calculateDifference(currentValue(group, parameter), year, {
meanDiff: 1.1,
stdDiff: 1.1
});
}
exports.PayoutsIfAround = PayoutsIfAround;
exports.calculate = calculate;
/* No side effect */

View File

@ -9,7 +9,6 @@ type group =
| All;
type parameter =
| CHANCE_CLOSED
| DONATIONS
| PAYOUTS;
@ -19,21 +18,48 @@ type yearlyNumericDiff = {
};
module PayoutsIfAround = {
let currentYear = 2020.;
let firstYearStdDev = 0.2;
let yearDiff = year => year -. 2020.0;
let yearlyMeanGrowthRateIfNotClosed = (group: group): yearlyNumericDiff => {
{meanDiff: 1.1, stdDiff: 1.1};
};
let yearlyChanceOfClosing = (group: group) => {
0.1;
let calculateDifference =
(currentValue, yearInQuestion, y: yearlyNumericDiff) => {
let yearDiff = yearDiff(yearInQuestion);
let meanDiff = Js.Math.pow_float(~base=y.meanDiff, ~exp=yearDiff);
let stdDevDiff = Js.Math.pow_float(~base=y.meanDiff, ~exp=yearDiff);
Math.normal(currentValue *. meanDiff, firstYearStdDev *. stdDevDiff);
};
let yearlyStdevGrowthRate = (group: group, year: int, parameter: parameter) => {
switch (group) {
| Fund(ANIMAL_WELFARE) => (30, 30)
| Fund(GLOBAL_HEALTH) => (30, 30)
| Fund(LONG_TERM_FUTURE) => (30, 30)
| Fund(META) => (30, 30)
| All => (50, 10)
let rec currentValue = (group: group, parameter) => {
let sum = (): float =>
currentValue(Fund(ANIMAL_WELFARE), parameter)
+. currentValue(Fund(GLOBAL_HEALTH), parameter)
+. currentValue(Fund(LONG_TERM_FUTURE), parameter)
+. currentValue(Fund(META), parameter);
switch (group, parameter) {
| (Fund(ANIMAL_WELFARE), DONATIONS) => 300000.0
| (Fund(ANIMAL_WELFARE), PAYOUTS) => 2300000.0
| (Fund(GLOBAL_HEALTH), DONATIONS) => 1000000.0
| (Fund(GLOBAL_HEALTH), PAYOUTS) => 500000.0
| (Fund(LONG_TERM_FUTURE), DONATIONS) => 600000.0
| (Fund(LONG_TERM_FUTURE), PAYOUTS) => 120000.0
| (Fund(META), DONATIONS) => 9300000.0
| (Fund(META), PAYOUTS) => 830000.0
| (All, _) => sum()
};
};
};
let calculate = (group: group, year: float, parameter: parameter) => {
PayoutsIfAround.(
calculateDifference(
currentValue(group, parameter),
year,
yearlyMeanGrowthRateIfNotClosed(group),
)
);
};

View File

@ -11,7 +11,7 @@ function FetchedDogPictures(Props) {
var setState = match[1];
var state = match[0];
React.useEffect((function () {
fetch("https://dog.ceo/api/breeds/image/random/3").then((function (response) {
fetch("https://dog.ceo/api/breeds/image/random/2").then((function (response) {
return response.json();
})).then((function (jsonResponse) {
Curry._1(setState, (function (_previousState) {

View File

@ -13,7 +13,7 @@ let make = () => {
// reasonml.github.io/reason-react/docs/en/components#hooks for more info
React.useEffect0(() => {
Js.Promise.(
fetch("https://dog.ceo/api/breeds/image/random/3")
fetch("https://dog.ceo/api/breeds/image/random/2")
|> then_(response => response##json())
|> then_(jsonResponse => {
setState(_previousState => LoadedDogs(jsonResponse##message));
@ -67,4 +67,4 @@ let make = () => {
->React.array
}}
</div>;
};
};

16
src/Funds.bs.js Normal file
View File

@ -0,0 +1,16 @@
'use strict';
var React = require("react");
var EAFunds$ReasonReactExamples = require("./EAFunds.bs.js");
var response = EAFunds$ReasonReactExamples.calculate(/* Fund */[/* GLOBAL_HEALTH */1], 2029, /* DONATIONS */0);
function Funds(Props) {
return React.createElement("div", undefined, response);
}
var make = Funds;
exports.response = response;
exports.make = make;
/* response Not a pure module */

6
src/Funds.re Normal file
View File

@ -0,0 +1,6 @@
let response = EAFunds.calculate(Fund(GLOBAL_HEALTH), 2029., DONATIONS);
[@react.component]
let make = () => {
<div> {React.string(response)} </div>;
};

View File

@ -2,11 +2,8 @@
var React = require("react");
var ReactDom = require("react-dom");
var Funds$ReasonReactExamples = require("./Funds.bs.js");
var ExampleStyles$ReasonReactExamples = require("./ExampleStyles.bs.js");
var BlinkingGreeting$ReasonReactExamples = require("./BlinkingGreeting/BlinkingGreeting.bs.js");
var FetchedDogPictures$ReasonReactExamples = require("./FetchedDogPictures/FetchedDogPictures.bs.js");
var ReducerFromReactJSDocs$ReasonReactExamples = require("./ReducerFromReactJSDocs/ReducerFromReactJSDocs.bs.js");
var ReasonUsingJSUsingReason$ReasonReactExamples = require("./ReasonUsingJSUsingReason/ReasonUsingJSUsingReason.bs.js");
var style = document.createElement("style");
@ -28,15 +25,7 @@ function makeContainer(text) {
return content;
}
ReactDom.render(React.createElement(BlinkingGreeting$ReasonReactExamples.make, {
children: "Hello!"
}), makeContainer("Blinking Greeting"));
ReactDom.render(React.createElement(ReducerFromReactJSDocs$ReasonReactExamples.make, { }), makeContainer("Reducer From ReactJS Docs"));
ReactDom.render(React.createElement(FetchedDogPictures$ReasonReactExamples.make, { }), makeContainer("Fetched Dog Pictures"));
ReactDom.render(React.createElement(ReasonUsingJSUsingReason$ReasonReactExamples.make, { }), makeContainer("Reason Using JS Using Reason"));
ReactDom.render(React.createElement(Funds$ReasonReactExamples.make, { }), makeContainer("Reason Using JS Using Reason"));
exports.style = style;
exports.makeContainer = makeContainer;

View File

@ -27,25 +27,4 @@ let makeContainer = text => {
content;
};
// All 4 examples.
ReactDOMRe.render(
<BlinkingGreeting>
{React.string("Hello!")}
</BlinkingGreeting>,
makeContainer("Blinking Greeting"),
);
ReactDOMRe.render(
<ReducerFromReactJSDocs />,
makeContainer("Reducer From ReactJS Docs"),
);
ReactDOMRe.render(
<FetchedDogPictures />,
makeContainer("Fetched Dog Pictures"),
);
ReactDOMRe.render(
<ReasonUsingJSUsingReason />,
makeContainer("Reason Using JS Using Reason"),
);
ReactDOMRe.render(<Funds />, makeContainer("Reason Using JS Using Reason"));

16
src/Math.bs.js Normal file
View File

@ -0,0 +1,16 @@
'use strict';
function normal(mean, std) {
var nMean = mean.toPrecision(4);
var nStd = std.toPrecision(2);
return "normal(" + (String(nMean) + (", " + (String(nStd) + ")")));
}
function divide(str1, str2) {
return "" + (String(str1) + ("/" + (String(str2) + "")));
}
exports.normal = normal;
exports.divide = divide;
/* No side effect */

10
src/Math.re Normal file
View File

@ -0,0 +1,10 @@
let normal = (mean: float, std: float) =>
Js.Float.(
{
let nMean = toPrecisionWithPrecision(mean, ~digits=4);
let nStd = toPrecisionWithPrecision(std, ~digits=2);
{j|normal($(nMean), $(nStd))|j};
}
);
let divide = (str1: string, str2: string) => {j|$(str1)/$(str2)|j};