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> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>ReasonReact Examples</title> <title>ReasonReact Examples</title>
</head> </head>
<body> <body>
<script> <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. // 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! --> <!-- Our little watcher. Super clean. Check it out! -->
<script src="/watcher.js"></script> <script src="/watcher.js"></script>
</body> </body>
</html> </html>

View File

@ -1,5 +1,10 @@
'use strict'; 'use strict';
var Math$ReasonReactExamples = require("./Math.bs.js");
function yearDiff(year) {
return year - 2020.0;
}
function yearlyMeanGrowthRateIfNotClosed(group) { function yearlyMeanGrowthRateIfNotClosed(group) {
return { return {
@ -8,29 +13,63 @@ function yearlyMeanGrowthRateIfNotClosed(group) {
}; };
} }
function yearlyChanceOfClosing(group) { function calculateDifference(currentValue, yearInQuestion, y) {
return 0.1; 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) { if (group) {
return /* tuple */[ switch (group[0]) {
30, case /* ANIMAL_WELFARE */0 :
30 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 { } else {
return /* tuple */[ 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);
50,
10
];
} }
} }
var PayoutsIfAround = { var PayoutsIfAround = {
currentYear: 2020,
firstYearStdDev: 0.2,
yearDiff: yearDiff,
yearlyMeanGrowthRateIfNotClosed: yearlyMeanGrowthRateIfNotClosed, yearlyMeanGrowthRateIfNotClosed: yearlyMeanGrowthRateIfNotClosed,
yearlyChanceOfClosing: yearlyChanceOfClosing, calculateDifference: calculateDifference,
yearlyStdevGrowthRate: yearlyStdevGrowthRate currentValue: currentValue
}; };
function calculate(group, year, parameter) {
return calculateDifference(currentValue(group, parameter), year, {
meanDiff: 1.1,
stdDiff: 1.1
});
}
exports.PayoutsIfAround = PayoutsIfAround; exports.PayoutsIfAround = PayoutsIfAround;
exports.calculate = calculate;
/* No side effect */ /* No side effect */

View File

@ -9,7 +9,6 @@ type group =
| All; | All;
type parameter = type parameter =
| CHANCE_CLOSED
| DONATIONS | DONATIONS
| PAYOUTS; | PAYOUTS;
@ -19,21 +18,48 @@ type yearlyNumericDiff = {
}; };
module PayoutsIfAround = { module PayoutsIfAround = {
let currentYear = 2020.;
let firstYearStdDev = 0.2;
let yearDiff = year => year -. 2020.0;
let yearlyMeanGrowthRateIfNotClosed = (group: group): yearlyNumericDiff => { let yearlyMeanGrowthRateIfNotClosed = (group: group): yearlyNumericDiff => {
{meanDiff: 1.1, stdDiff: 1.1}; {meanDiff: 1.1, stdDiff: 1.1};
}; };
let yearlyChanceOfClosing = (group: group) => { let calculateDifference =
0.1; (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) => { let rec currentValue = (group: group, parameter) => {
switch (group) { let sum = (): float =>
| Fund(ANIMAL_WELFARE) => (30, 30) currentValue(Fund(ANIMAL_WELFARE), parameter)
| Fund(GLOBAL_HEALTH) => (30, 30) +. currentValue(Fund(GLOBAL_HEALTH), parameter)
| Fund(LONG_TERM_FUTURE) => (30, 30) +. currentValue(Fund(LONG_TERM_FUTURE), parameter)
| Fund(META) => (30, 30) +. currentValue(Fund(META), parameter);
| All => (50, 10) 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 setState = match[1];
var state = match[0]; var state = match[0];
React.useEffect((function () { 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(); return response.json();
})).then((function (jsonResponse) { })).then((function (jsonResponse) {
Curry._1(setState, (function (_previousState) { 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 // reasonml.github.io/reason-react/docs/en/components#hooks for more info
React.useEffect0(() => { React.useEffect0(() => {
Js.Promise.( 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_(response => response##json())
|> then_(jsonResponse => { |> then_(jsonResponse => {
setState(_previousState => LoadedDogs(jsonResponse##message)); setState(_previousState => LoadedDogs(jsonResponse##message));

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 React = require("react");
var ReactDom = require("react-dom"); var ReactDom = require("react-dom");
var Funds$ReasonReactExamples = require("./Funds.bs.js");
var ExampleStyles$ReasonReactExamples = require("./ExampleStyles.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"); var style = document.createElement("style");
@ -28,15 +25,7 @@ function makeContainer(text) {
return content; return content;
} }
ReactDom.render(React.createElement(BlinkingGreeting$ReasonReactExamples.make, { ReactDom.render(React.createElement(Funds$ReasonReactExamples.make, { }), makeContainer("Reason Using JS Using Reason"));
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"));
exports.style = style; exports.style = style;
exports.makeContainer = makeContainer; exports.makeContainer = makeContainer;

View File

@ -27,25 +27,4 @@ let makeContainer = text => {
content; content;
}; };
// All 4 examples. ReactDOMRe.render(<Funds />, makeContainer("Reason Using JS Using Reason"));
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"),
);

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