From 8fdcc99231116c07cb97d9f74c63116cf98df8a4 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Thu, 6 Feb 2020 21:05:13 +0000 Subject: [PATCH] Added math functions --- index.html | 5 +- src/EAFunds.bs.js | 65 +++++++++++++++---- src/EAFunds.re | 46 ++++++++++--- .../FetchedDogPictures.bs.js | 2 +- src/FetchedDogPictures/FetchedDogPictures.re | 4 +- src/Funds.bs.js | 16 +++++ src/Funds.re | 6 ++ src/Index.bs.js | 15 +---- src/Index.re | 23 +------ src/Math.bs.js | 16 +++++ src/Math.re | 10 +++ 11 files changed, 146 insertions(+), 62 deletions(-) create mode 100644 src/Funds.bs.js create mode 100644 src/Funds.re create mode 100644 src/Math.bs.js create mode 100644 src/Math.re diff --git a/index.html b/index.html index 94689a3b..d9f99078 100644 --- a/index.html +++ b/index.html @@ -1,9 +1,11 @@ + ReasonReact Examples + - + + \ No newline at end of file diff --git a/src/EAFunds.bs.js b/src/EAFunds.bs.js index d34b90ba..e4d05532 100644 --- a/src/EAFunds.bs.js +++ b/src/EAFunds.bs.js @@ -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 */ diff --git a/src/EAFunds.re b/src/EAFunds.re index d72ae583..be1c668d 100644 --- a/src/EAFunds.re +++ b/src/EAFunds.re @@ -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), + ) + ); }; \ No newline at end of file diff --git a/src/FetchedDogPictures/FetchedDogPictures.bs.js b/src/FetchedDogPictures/FetchedDogPictures.bs.js index 909b02e5..afc11e14 100644 --- a/src/FetchedDogPictures/FetchedDogPictures.bs.js +++ b/src/FetchedDogPictures/FetchedDogPictures.bs.js @@ -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) { diff --git a/src/FetchedDogPictures/FetchedDogPictures.re b/src/FetchedDogPictures/FetchedDogPictures.re index 2a32618b..0ea0f181 100644 --- a/src/FetchedDogPictures/FetchedDogPictures.re +++ b/src/FetchedDogPictures/FetchedDogPictures.re @@ -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 }} ; -}; +}; \ No newline at end of file diff --git a/src/Funds.bs.js b/src/Funds.bs.js new file mode 100644 index 00000000..6a3683cc --- /dev/null +++ b/src/Funds.bs.js @@ -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 */ diff --git a/src/Funds.re b/src/Funds.re new file mode 100644 index 00000000..8c74c4df --- /dev/null +++ b/src/Funds.re @@ -0,0 +1,6 @@ +let response = EAFunds.calculate(Fund(GLOBAL_HEALTH), 2029., DONATIONS); + +[@react.component] +let make = () => { +
{React.string(response)}
; +}; \ No newline at end of file diff --git a/src/Index.bs.js b/src/Index.bs.js index 236a5a4b..88be24e1 100644 --- a/src/Index.bs.js +++ b/src/Index.bs.js @@ -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; diff --git a/src/Index.re b/src/Index.re index c45ca054..8b240446 100644 --- a/src/Index.re +++ b/src/Index.re @@ -27,25 +27,4 @@ let makeContainer = text => { content; }; -// All 4 examples. -ReactDOMRe.render( - - {React.string("Hello!")} - , - makeContainer("Blinking Greeting"), -); - -ReactDOMRe.render( - , - makeContainer("Reducer From ReactJS Docs"), -); - -ReactDOMRe.render( - , - makeContainer("Fetched Dog Pictures"), -); - -ReactDOMRe.render( - , - makeContainer("Reason Using JS Using Reason"), -); +ReactDOMRe.render(, makeContainer("Reason Using JS Using Reason")); \ No newline at end of file diff --git a/src/Math.bs.js b/src/Math.bs.js new file mode 100644 index 00000000..6e7a703e --- /dev/null +++ b/src/Math.bs.js @@ -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 */ diff --git a/src/Math.re b/src/Math.re new file mode 100644 index 00000000..d94c209b --- /dev/null +++ b/src/Math.re @@ -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}; \ No newline at end of file