Changed getByWithFn to not need two function calls

This commit is contained in:
Ozzie Gooen 2022-08-30 17:58:51 -07:00
parent 9a8ec06ca7
commit 2f77888365

View File

@ -572,10 +572,20 @@ module A = {
|> (x => Ok(x)) |> (x => Ok(x))
} }
let getByOpen = (a, op, bin) => let getByWithFn = (a, fn, boolCondition) => {
switch getBy(a, r => bin(op(r))) { let i = ref(0);
| Some(r) => Some(op(r)) let finalFunctionValue = ref(None);
| None => 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 tail = Belt.Array.sliceToEnd(_, 1)
@ -680,7 +690,7 @@ module A = {
let firstSome = x => Belt.Array.getBy(x, O.isSome) let firstSome = x => Belt.Array.getBy(x, O.isSome)
let firstSomeFn = (r: array<unit => option<'a>>): option<'a> => let firstSomeFn = (r: array<unit => 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) let firstSomeFnWithDefault = (r, default) => firstSomeFn(r)->O2.default(default)