tweak: Use firstErrorOrOpen instead of hack

This commit is contained in:
NunoSempere 2022-09-06 14:54:59 +02:00
parent fc2d647b62
commit 162f158dbc

View File

@ -96,7 +96,6 @@ module Integration = {
) )
// Gotcha: makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby> // Gotcha: makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby>
let ysOptions = Belt.Array.map(innerXs, x => applyFunctionAtFloatToFloatOption(x)) let ysOptions = Belt.Array.map(innerXs, x => applyFunctionAtFloatToFloatOption(x))
let okYs = E.A.R.filterOk(ysOptions)
/* Logging, with a worked example. */ /* Logging, with a worked example. */
// Useful for understanding what is happening. // Useful for understanding what is happening.
@ -122,29 +121,32 @@ module Integration = {
Js.Console.log2("innerPointIncrement", innerPointIncrement) // (10-0)/4 = 2.5 Js.Console.log2("innerPointIncrement", innerPointIncrement) // (10-0)/4 = 2.5
Js.Console.log2("innerXs", innerXs) // 2.5, 5, 7.5 Js.Console.log2("innerXs", innerXs) // 2.5, 5, 7.5
Js.Console.log2("ysOptions", ysOptions) Js.Console.log2("ysOptions", ysOptions)
Js.Console.log2("okYs", okYs)
} }
//This is pretty hacky. It should use a result type instead of checking that length matches. //This is pretty hacky. It should use a result type instead of checking that length matches.
let result = if E.A.length(ysOptions) == E.A.length(okYs) { let result = switch E.A.R.firstErrorOrOpen(ysOptions) {
let innerPointsSum = okYs->E.A.reduce(0.0, (a, b) => a +. b) | Ok(ys) => {
let resultWithOuterPoints = switch ( let innerPointsSum = ys->E.A.reduce(0.0, (a, b) => a +. b)
applyFunctionAtFloatToFloatOption(min), let resultWithOuterPoints = switch (
applyFunctionAtFloatToFloatOption(max), applyFunctionAtFloatToFloatOption(min),
) { applyFunctionAtFloatToFloatOption(max),
| (Ok(yMin), Ok(yMax)) => { ) {
let result = | (Ok(yMin), Ok(yMax)) => {
(yMin +. yMax) *. weightForAnOuterPoint +. innerPointsSum *. weightForAnInnerPoint let result =
let wrappedResult = result->ReducerInterface_InternalExpressionValue.IEvNumber->Ok (yMin +. yMax) *. weightForAnOuterPoint +. innerPointsSum *. weightForAnInnerPoint
wrappedResult let wrappedResult = result->ReducerInterface_InternalExpressionValue.IEvNumber->Ok
wrappedResult
}
| (Error(b), _) => Error(b)
| (_, Error(b)) => Error(b)
} }
| (Error(b), _) => Error(b) resultWithOuterPoints
| (_, Error(b)) => Error(b)
} }
resultWithOuterPoints | Error(b) =>
} else {
Error( Error(
"Integration error 3 in Danger.integrate. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead", "Integration error 3 in Danger.integrate. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead." ++
"Original error: " ++
b,
) )
} }
result result