Starting to add conditionals

This commit is contained in:
Ozzie Gooen 2020-02-10 23:10:03 +00:00
parent fc3108c289
commit b44073140f
3 changed files with 78 additions and 10 deletions

View File

@ -1,14 +1,25 @@
module Value = {
type binaryConditional =
| Selected(bool)
| Unselected;
type t =
| BinaryConditional(binaryConditional)
| SelectSingle(string)
| DateTime(MomentRe.Moment.t)
| FloatPoint(float)
| Probability(float)
| FloatCdf(string);
let to_string = (t: t) => {
switch (t) {
| BinaryConditional(binaryConditional) =>
switch (binaryConditional) {
| Selected(r) => r ? "True" : "False"
| Unselected => ""
}
| SelectSingle(r) => r
| FloatCdf(r) => r
| Probability(r) => (r *. 100. |> Js.Float.toFixed) ++ "%"
| DateTime(r) => r |> MomentRe.Moment.defaultFormat
| FloatPoint(r) => r |> Js.Float.toFixed
};
@ -34,17 +45,23 @@ module Type = {
max: option('a),
};
type withDefault('a) = {default: option('a)};
type t =
| BinaryConditional
| SelectSingle(selectSingle)
| FloatPoint(withDefaultMinMax(float))
| Probability(withDefault(float))
| DateTime(withDefaultMinMax(MomentRe.Moment.t))
| Year(withDefaultMinMax(float))
| FloatCdf;
let default = (t: t) =>
switch (t) {
| BinaryConditional => Some(Value.BinaryConditional(Unselected))
| Year(r) => r.default->Belt.Option.map(p => Value.FloatPoint(p))
| FloatPoint(r) => r.default->Belt.Option.map(p => Value.FloatPoint(p))
| Probability(r) => r.default->Belt.Option.map(p => Value.Probability(p))
| DateTime(r) => r.default->Belt.Option.map(p => Value.DateTime(p))
| SelectSingle(r) =>
r.default->Belt.Option.map(p => Value.SelectSingle(p))

View File

@ -33,6 +33,36 @@ let make =
}
)}
/>
| (BinaryConditional, Some(BinaryConditional(r))) =>
switch (r) {
| Unselected =>
<div
onClick={_ => onChange(Some(BinaryConditional(Selected(true))))}>
{"Select" |> ReasonReact.string}
</div>
| Selected(true) =>
<div>
{"YES!" |> ReasonReact.string}
<div
onClick={_ => onChange(Some(BinaryConditional(Selected(false))))}>
{"No" |> ReasonReact.string}
</div>
<div onClick={_ => onChange(Some(BinaryConditional(Unselected)))}>
{"Deselect" |> ReasonReact.string}
</div>
</div>
| Selected(false) =>
<div>
{"NO!" |> ReasonReact.string}
<div
onClick={_ => onChange(Some(BinaryConditional(Selected(true))))}>
{"Yes" |> ReasonReact.string}
</div>
<div onClick={_ => onChange(Some(BinaryConditional(Unselected)))}>
{"Deselect" |> ReasonReact.string}
</div>
</div>
}
| (Year(_), _)
| (FloatPoint(_), _) => <input type_="number" value="" />
| (SelectSingle(t), Some(SelectSingle(r))) =>

View File

@ -11,8 +11,12 @@ module Data = {
type output =
| DONATIONS
| CHANCE_OF_EXISTENCE
| PAYOUTS;
type conditionals =
| WORLD_CATASTROPHE;
type fundWithInfo = {
group,
name: string,
@ -93,6 +97,7 @@ module Model = {
| (Fund(META), DONATIONS) => 9300000.0
| (Fund(META), PAYOUTS) => 830000.0
| (All, _) => sum()
| (_, CHANCE_OF_EXISTENCE) => 0.0
};
};
let make =
@ -102,6 +107,9 @@ module Model = {
currentDateTime: MomentRe.Moment.t,
output: output,
) => {
switch (output) {
| DONATIONS
| PAYOUTS =>
Prop.Value.FloatCdf(
calculateDifference(
currentValue(group, output),
@ -109,7 +117,11 @@ module Model = {
currentDateTime,
yearlyMeanGrowthRateIfNotClosed(group),
),
);
)
| CHANCE_OF_EXISTENCE =>
let yearDiff = MomentRe.diff(dateTime, currentDateTime, `days) /. 365.;
Prop.Value.Probability((100. -. yearDiff) /. 100.);
};
};
};
@ -126,6 +138,7 @@ module Interface = {
let outputFromString = (s: string) =>
switch (s) {
| "donations" => DONATIONS
| "exists" => CHANCE_OF_EXISTENCE
| _ => PAYOUTS
};
@ -136,6 +149,7 @@ module Interface = {
Some(DateTime(intendedYear)),
Some(DateTime(currentYear)),
Some(SelectSingle(output)),
Some(BinaryConditional(r)),
|] =>
choiceFromString(fund)
|> E.O.fmap(fund =>
@ -206,12 +220,19 @@ module Interface = {
SelectSingle({
default: Some("Output"),
options: [
{name: "Donations", id: "donations"},
{name: "Funding", id: "funding"},
{name: "Donations | Exists", id: "donations"},
{name: "Funding | Exists", id: "funding"},
{name: "Exists", id: "exists"},
],
}),
(),
),
TypeWithMetadata.make(
~name="Conditional on World Ending",
~id="worldEnd",
~type_=BinaryConditional,
(),
),
|],
outputTypes: [||],
run,