From 2f77888365a675acb0f3292f9b9fbb0e4dd230c3 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 30 Aug 2022 17:58:51 -0700 Subject: [PATCH] Changed getByWithFn to not need two function calls --- .../squiggle-lang/src/rescript/Utility/E.res | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 22c8c525..4cad3472 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -572,12 +572,22 @@ module A = { |> (x => Ok(x)) } - let getByOpen = (a, op, bin) => - switch getBy(a, r => bin(op(r))) { - | Some(r) => Some(op(r)) - | None => None + let getByWithFn = (a, fn, boolCondition) => { + let i = ref(0); + let finalFunctionValue = ref(None); + let length = Belt.Array.length(a); + + while (i.contents < length) && (finalFunctionValue.contents == None) { + let itemWithFnApplied = Belt.Array.getUnsafe(a, i.contents) |> fn + if boolCondition(itemWithFnApplied) { + finalFunctionValue := Some(itemWithFnApplied) + } + i := i.contents + 1 } + finalFunctionValue.contents + } + let tail = Belt.Array.sliceToEnd(_, 1) let zip = Belt.Array.zip @@ -680,7 +690,7 @@ module A = { let firstSome = x => Belt.Array.getBy(x, O.isSome) let firstSomeFn = (r: array option<'a>>): option<'a> => - O.flatten(getByOpen(r, l => l(), O.isSome)) + O.flatten(getByWithFn(r, l => l(), O.isSome)) let firstSomeFnWithDefault = (r, default) => firstSomeFn(r)->O2.default(default)