Merge pull request #1042 from quantified-uncertainty/getByWithFn
Changed getByWithFn to not need two function calls
This commit is contained in:
commit
5e9bf521fa
21
packages/squiggle-lang/__tests__/E/A_test.res
Normal file
21
packages/squiggle-lang/__tests__/E/A_test.res
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
open Jest
|
||||||
|
open TestHelpers
|
||||||
|
|
||||||
|
describe("E.A.getByFmap", () => {
|
||||||
|
makeTest("Empty list returns None", E.A.getByFmap([], x => x + 1, x => mod(x, 2) == 0), None)
|
||||||
|
makeTest(
|
||||||
|
"Never predicate returns None",
|
||||||
|
E.A.getByFmap([1, 2, 3, 4, 5, 6], x => x + 1, _ => false),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
makeTest(
|
||||||
|
"function evaluates",
|
||||||
|
E.A.getByFmap([1, 1, 1, 1, 1, 1, 1, 2, 1, 1], x => 3 * x, x => x > 4),
|
||||||
|
Some(6),
|
||||||
|
)
|
||||||
|
makeTest(
|
||||||
|
"always predicate returns fn(fst(a))",
|
||||||
|
E.A.getByFmap([0, 1, 2, 3, 4, 5, 6], x => 10 + x, _ => true),
|
||||||
|
Some(10),
|
||||||
|
)
|
||||||
|
})
|
|
@ -572,12 +572,22 @@ module A = {
|
||||||
|> (x => Ok(x))
|
|> (x => Ok(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
let getByOpen = (a, op, bin) =>
|
let getByFmap = (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)
|
||||||
|
|
||||||
let zip = Belt.Array.zip
|
let zip = Belt.Array.zip
|
||||||
|
@ -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(getByFmap(r, l => l(), O.isSome))
|
||||||
|
|
||||||
let firstSomeFnWithDefault = (r, default) => firstSomeFn(r)->O2.default(default)
|
let firstSomeFnWithDefault = (r, default) => firstSomeFn(r)->O2.default(default)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user