Simple cleanup

This commit is contained in:
Ozzie Gooen 2020-02-09 22:24:11 +00:00
parent 0462a88ad5
commit 4e59c73343
5 changed files with 154 additions and 103 deletions

View File

@ -2,7 +2,6 @@
var Block = require("bs-platform/lib/js/block.js"); var Block = require("bs-platform/lib/js/block.js");
var Curry = require("bs-platform/lib/js/curry.js"); var Curry = require("bs-platform/lib/js/curry.js");
var React = require("react");
var Math$ProbExample = require("../Math.bs.js"); var Math$ProbExample = require("../Math.bs.js");
var Prop$ProbExample = require("../Prop.bs.js"); var Prop$ProbExample = require("../Prop.bs.js");
@ -74,6 +73,76 @@ function go(group, year, output) {
])]); ])]);
} }
function convertChoice(s) {
switch (s) {
case "animal" :
return /* Fund */[/* ANIMAL_WELFARE */0];
case "globalHealth" :
return /* Fund */[/* GLOBAL_HEALTH */1];
case "longTerm" :
return /* Fund */[/* LONG_TERM_FUTURE */2];
case "meta" :
return /* Fund */[/* META */3];
default:
return /* All */0;
}
}
function convertOutput(s) {
if (s === "donations") {
return /* DONATIONS */0;
} else {
return /* PAYOUTS */1;
}
}
function run(p) {
var partial_arg = p[/* inputValues */1];
var partial_arg$1 = Prop$ProbExample.ValueMap.get;
var get = function (param) {
return partial_arg$1(partial_arg, param);
};
var match = Curry._1(get, "Fund");
var match$1 = Curry._1(get, "Year");
var match$2 = Curry._1(get, "Output");
if (match !== undefined) {
var match$3 = match;
switch (match$3.tag | 0) {
case /* SelectSingle */0 :
if (match$1 !== undefined) {
var match$4 = match$1;
switch (match$4.tag | 0) {
case /* FloatPoint */1 :
if (match$2 !== undefined) {
var match$5 = match$2;
switch (match$5.tag | 0) {
case /* SelectSingle */0 :
return go(convertChoice(match$3[0]), match$4[0], convertOutput(match$5[0]));
case /* FloatPoint */1 :
case /* FloatCdf */2 :
return ;
}
} else {
return ;
}
case /* SelectSingle */0 :
case /* FloatCdf */2 :
return ;
}
} else {
return ;
}
case /* FloatPoint */1 :
case /* FloatCdf */2 :
return ;
}
}
}
var model_002 = /* inputTypes : array */[ var model_002 = /* inputTypes : array */[
Prop$ProbExample.TypeWithMetadata.make("Fund", /* SelectSingle */Block.__(0, [/* record */[ Prop$ProbExample.TypeWithMetadata.make("Fund", /* SelectSingle */Block.__(0, [/* record */[
/* options : :: */[ /* options : :: */[
@ -114,7 +183,23 @@ var model_002 = /* inputTypes : array */[
/* min */2020.0, /* min */2020.0,
/* max */2050.0 /* max */2050.0
]]), undefined, undefined, undefined, /* () */0), ]]), undefined, undefined, undefined, /* () */0),
Prop$ProbExample.TypeWithMetadata.currentYear Prop$ProbExample.TypeWithMetadata.currentYear,
Prop$ProbExample.TypeWithMetadata.make("Output", /* SelectSingle */Block.__(0, [/* record */[
/* options : :: */[
/* record */[
/* id */"donations",
/* name */"Donations"
],
/* :: */[
/* record */[
/* id */"funding",
/* name */"Funding"
],
/* [] */0
]
],
/* default */"Output"
]]), undefined, undefined, undefined, /* () */0)
]; ];
var model_003 = /* outputTypes : array */[]; var model_003 = /* outputTypes : array */[];
@ -123,74 +208,15 @@ var model = /* record */[
/* name */"Calculate the payments and payouts of EA Funds based on existing data.", /* name */"Calculate the payments and payouts of EA Funds based on existing data.",
/* author */"George Harrison", /* author */"George Harrison",
model_002, model_002,
model_003 model_003,
/* run */run
]; ];
function convertChoice(s) {
switch (s) {
case "animal" :
return /* Fund */[/* ANIMAL_WELFARE */0];
case "globalHealth" :
return /* Fund */[/* GLOBAL_HEALTH */1];
case "longTerm" :
return /* Fund */[/* LONG_TERM_FUTURE */2];
case "meta" :
return /* Fund */[/* META */3];
default:
return /* All */0;
}
}
function run(p) {
var partial_arg = p[/* inputValues */1];
var partial_arg$1 = Prop$ProbExample.ValueMap.get;
var get = function (param) {
return partial_arg$1(partial_arg, param);
};
var match = Curry._1(get, "Fund");
var match$1 = Curry._1(get, "Year");
if (match !== undefined) {
var match$2 = match;
switch (match$2.tag | 0) {
case /* SelectSingle */0 :
if (match$1 !== undefined) {
var match$3 = match$1;
switch (match$3.tag | 0) {
case /* FloatPoint */1 :
return go(convertChoice(match$2[0]), match$3[0], /* DONATIONS */0);
case /* SelectSingle */0 :
case /* FloatCdf */2 :
return ;
}
} else {
return ;
}
case /* FloatPoint */1 :
case /* FloatCdf */2 :
return ;
}
}
}
function EAFunds_Model$Interface$Form(Props) {
return React.createElement(Prop$ProbExample.ModelForm.make, {
combo: Prop$ProbExample.Combo.fromModel(model),
runModel: run
});
}
var Form = {
make: EAFunds_Model$Interface$Form
};
var Interface = { var Interface = {
model: model,
convertChoice: convertChoice, convertChoice: convertChoice,
convertOutput: convertOutput,
run: run, run: run,
Form: Form model: model
}; };
exports.PayoutsIfAround = PayoutsIfAround; exports.PayoutsIfAround = PayoutsIfAround;

View File

@ -57,6 +57,34 @@ let go = (group: group, year: float, output: output) => {
module Interface = { module Interface = {
open Prop; open Prop;
let convertChoice = (s: string) =>
switch (s) {
| "animal" => Fund(ANIMAL_WELFARE)
| "globalHealth" => Fund(GLOBAL_HEALTH)
| "longTerm" => Fund(LONG_TERM_FUTURE)
| "meta" => Fund(META)
| _ => All
};
let convertOutput = (s: string) =>
switch (s) {
| "donations" => DONATIONS
| _ => PAYOUTS
};
let run = (p: Combo.t) => {
let get = Prop.ValueMap.get(p.inputValues);
switch (get("Fund"), get("Year"), get("Output")) {
| (
Some(SelectSingle(fund)),
Some(FloatPoint(intendedYear)),
Some(SelectSingle(output)),
) =>
Some(go(convertChoice(fund), intendedYear, convertOutput(output)))
| _ => None
};
};
let model: Model.t = { let model: Model.t = {
name: "Calculate the payments and payouts of EA Funds based on existing data.", name: "Calculate the payments and payouts of EA Funds based on existing data.",
author: "George Harrison", author: "George Harrison",
@ -87,31 +115,20 @@ module Interface = {
(), (),
), ),
TypeWithMetadata.currentYear, TypeWithMetadata.currentYear,
TypeWithMetadata.make(
~name="Output",
~type_=
SelectSingle({
default: Some("Output"),
options: [
{name: "Donations", id: "donations"},
{name: "Funding", id: "funding"},
],
}),
(),
),
|], |],
outputTypes: [||], outputTypes: [||],
}; run,
let convertChoice = (s: string) =>
switch (s) {
| "animal" => Fund(ANIMAL_WELFARE)
| "globalHealth" => Fund(GLOBAL_HEALTH)
| "longTerm" => Fund(LONG_TERM_FUTURE)
| "meta" => Fund(META)
| _ => All
};
let run = (p: Combo.t) => {
let get = Prop.ValueMap.get(p.inputValues);
switch (get("Fund"), get("Year")) {
| (Some(SelectSingle(fund)), Some(FloatPoint(intendedYear))) =>
Some(go(convertChoice(fund), intendedYear, DONATIONS))
| _ => None
};
};
module Form = {
[@react.component]
let make = () =>
<Prop.ModelForm combo={Prop.Combo.fromModel(model)} runModel=run />;
}; };
}; };

View File

@ -2,10 +2,13 @@
var React = require("react"); var React = require("react");
var ReactDOMRe = require("reason-react/src/ReactDOMRe.js"); var ReactDOMRe = require("reason-react/src/ReactDOMRe.js");
var Prop$ProbExample = require("./Prop.bs.js");
var EAFunds_Model$ProbExample = require("./EAFunds/EAFunds_Model.bs.js"); var EAFunds_Model$ProbExample = require("./EAFunds/EAFunds_Model.bs.js");
((import('./styles/index.css'))); ((import('./styles/index.css')));
ReactDOMRe.renderToElementWithId(React.createElement(EAFunds_Model$ProbExample.Interface.Form.make, { }), "app"); ReactDOMRe.renderToElementWithId(React.createElement(Prop$ProbExample.ModelForm.make, {
model: EAFunds_Model$ProbExample.Interface.model
}), "app");
/* Not a pure module */ /* Not a pure module */

View File

@ -1,2 +1,5 @@
[%bs.raw {|import('./styles/index.css')|}]; [%bs.raw {|import('./styles/index.css')|}];
ReactDOMRe.renderToElementWithId(<EAFunds_Model.Interface.Form />, "app"); ReactDOMRe.renderToElementWithId(
<Prop.ModelForm model=EAFunds_Model.Interface.model />,
"app",
);

View File

@ -115,6 +115,12 @@ module Model = {
author: string, author: string,
inputTypes: array(TypeWithMetadata.t), inputTypes: array(TypeWithMetadata.t),
outputTypes: array(TypeWithMetadata.t), outputTypes: array(TypeWithMetadata.t),
run: combo => option(Value.t),
}
and combo = {
model: t,
inputValues: ValueMap.t,
outputValues: ValueMap.t,
}; };
module InputTypes = { module InputTypes = {
@ -124,11 +130,7 @@ module Model = {
}; };
module Combo = { module Combo = {
type t = { type t = Model.combo;
model: Model.t,
inputValues: ValueMap.t,
outputValues: ValueMap.t,
};
module InputValues = { module InputValues = {
let defaults = (t: Model.t) => let defaults = (t: Model.t) =>
@ -136,17 +138,17 @@ module Combo = {
|> E.A.fmap((o: TypeWithMetadata.t) => (o.id, Type.default(o.type_))) |> E.A.fmap((o: TypeWithMetadata.t) => (o.id, Type.default(o.type_)))
|> ValueMap.fromOptionalArray; |> ValueMap.fromOptionalArray;
let isValid = t => let isValid = (t: t) =>
t.model t.model
|> Model.InputTypes.keys |> Model.InputTypes.keys
|> E.A.fmap(ValueMap.get(t.inputValues)) |> E.A.fmap(ValueMap.get(t.inputValues))
|> Belt.Array.some(_, E.O.isNone); |> Belt.Array.some(_, E.O.isNone);
let update = (t, key: string, onUpdate: option(Value.t)) => let update = (t: t, key: string, onUpdate: option(Value.t)) =>
ValueMap.update(t.inputValues, key, onUpdate); ValueMap.update(t.inputValues, key, onUpdate);
}; };
let updateInputValue = (t, k, u) => { let updateInputValue = (t: t, k, u) => {
...t, ...t,
inputValues: InputValues.update(t, k, u), inputValues: InputValues.update(t, k, u),
}; };
@ -157,7 +159,7 @@ module Combo = {
(i, ValueMap.get(t.inputValues, i.id)) (i, ValueMap.get(t.inputValues, i.id))
); );
let fromModel = (t: Model.t) => { let fromModel = (t: Model.t): t => {
model: t, model: t,
inputValues: InputValues.defaults(t), inputValues: InputValues.defaults(t),
outputValues: InputValues.defaults(t), outputValues: InputValues.defaults(t),
@ -236,8 +238,8 @@ module ModelForm = {
handleChange(ReactEvent.Form.target(event)##value); handleChange(ReactEvent.Form.target(event)##value);
[@react.component] [@react.component]
let make = (~combo: Combo.t, ~runModel: Combo.t => option(Value.t)) => { let make = (~model: Model.t) => {
let formState = makeHelpers(combo); let formState = makeHelpers(Combo.fromModel(model));
<div> <div>
{Combo.inputTypeValuePairs(formState.combo) {Combo.inputTypeValuePairs(formState.combo)
|> E.A.fmap(((type_, value)) => |> E.A.fmap(((type_, value)) =>
@ -251,7 +253,7 @@ module ModelForm = {
/> />
) )
|> ReasonReact.array} |> ReasonReact.array}
{runModel(formState.combo) {model.run(formState.combo)
|> E.O.fmap(Value.to_string) |> E.O.fmap(Value.to_string)
|> E.O.default("") |> E.O.default("")
|> ReasonReact.string} |> ReasonReact.string}