From ecf5249b5c6309a8fad02fe7642374437dc9bea7 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 4 Sep 2022 22:45:55 +0200 Subject: [PATCH] feat: And integration function to Danger namespace Note that I haven't tested this yet. --- .../FunctionRegistry/Library/FR_Danger.res | 89 +++++++++++++++++-- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res index 56bb33e3..3dd42efb 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res @@ -74,13 +74,51 @@ module Internals = { let internalZero = ReducerInterface_InternalExpressionValue.IEvNumber(0.0) let applyFunctionAtZero = (aLambda, environment, reducer) => applyFunctionAtPoint(aLambda, internalZero, environment, reducer) + @dead let applyFunctionAtFloat = (aLambda, point, environment, reducer) => applyFunctionAtPoint(aLambda, ReducerInterface_InternalExpressionValue.IEvNumber(point)) - let integrateFunction = (aLambda, min: float, max: float, increment, environment, reducer) => { + + let integrateFunctionBetweenWithIncrement = ( + aLambda, + min: float, + max: float, + increment: float, + environment, + reducer, + ) => { // Should be easy, but tired today. - 0.0 + let applyFunctionAtFloatToFloatOption = (point: float) => { + let pointAsInternalExpression = ReducerInterface_InternalExpressionValue.IEvNumber(point) + let resultAsInternalExpression = Reducer_Expression_Lambda.doLambdaCall( + aLambda, + list{pointAsInternalExpression}, + environment, + reducer, + ) + let result = switch resultAsInternalExpression { + | Ok(IEvNumber(x)) => Ok(x) + | Error(_) => Error("Integration error in Danger.integrate") + | _ => Error("Integration error in Danger.integrate") + } + result + } + let xsLength = Js.Math.ceil((max -. min) /. increment) + let xs = Belt.Array.makeBy(xsLength, i => min +. Belt_Float.fromInt(i) *. increment) + let ysOptions = Belt.Array.map(xs, x => applyFunctionAtFloatToFloatOption(x)) + let okYs = E.A.R.filterOk(ysOptions) + let result = switch E.A.length(ysOptions) == E.A.length(okYs) { + | true => { + let numericIntermediate = okYs->E.A.reduce(0.0, (a, b) => a +. b) + let numericIntermediate2 = numericIntermediate *. increment + let resultWrapped = + numericIntermediate2->ReducerInterface_InternalExpressionValue.IEvNumber->Ok + resultWrapped + } + | false => Error("Integration error in Danger.integrate") + } + result } - let getDiminishingMarginalReturnsEquilibrium = "To do" + @dead let getDiminishingMarginalReturnsEquilibrium = "To do" } let library = [ @@ -143,12 +181,14 @@ let library = [ FnDefinition.make( ~name="applyFunctionAtZero", ~inputs=[FRTypeLambda], - ~run=(inputs, _, env, reducer) => - switch inputs { + ~run=(inputs, _, env, reducer) => { + let result = switch inputs { | [IEvLambda(aLambda)] => Internals.applyFunctionAtZero(aLambda, env, reducer)->E.R2.errMap(_ => "Error!") | _ => Error(impossibleError) - }, + } + result + }, (), ), ], @@ -175,4 +215,41 @@ let library = [ ], (), ), + Function.make( + ~name="integrateFunctionBetweenWithIncrement", + ~nameSpace, + ~output=EvtArray, + ~requiresNamespace=false, + ~examples=[`Danger.integrateFunctionBetweenWithIncrement({|x| x+1}, 1, 10, 1)`], // should be [x^2 + x]1_10 = (10^2 + 10) - (1 + 1) = 110 - 2 = 118 + ~definitions=[ + FnDefinition.make( + ~name="integrateFunctionBetweenWithIncrement", + ~inputs=[FRTypeLambda, FRTypeNumber, FRTypeNumber, FRTypeNumber], + ~run=(inputs, _, env, reducer) => { + let result = switch inputs { + | [_, _, _, IEvNumber(0.0)] => + Error("Integration error in Danger.integrate: Increment can't be 0.") + | [IEvLambda(aLambda), IEvNumber(min), IEvNumber(max), IEvNumber(increment)] => + Internals.integrateFunctionBetweenWithIncrement( + aLambda, + min, + max, + increment, + env, + reducer, + )->E.R2.errMap(_ => + "Integration error in Danger.integrate. Something went wrong along the way" + ) + | _ => + Error( + "Integration error in Danger.integrate. Remember that inputs are (function, number (min), number (max), number(increment))", + ) + } + result + }, + (), + ), + ], + (), + ), ]