From 2f77888365a675acb0f3292f9b9fbb0e4dd230c3 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 30 Aug 2022 17:58:51 -0700 Subject: [PATCH 1/4] 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) From 22458aa9e51e06e2d70bbdbf187310aeb68b4214 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 30 Aug 2022 18:17:00 -0700 Subject: [PATCH 2/4] Proper formatting --- packages/squiggle-lang/src/rescript/Utility/E.res | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 4cad3472..a2012efd 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -573,11 +573,11 @@ module A = { } let getByWithFn = (a, fn, boolCondition) => { - let i = ref(0); - let finalFunctionValue = ref(None); - let length = Belt.Array.length(a); + let i = ref(0) + let finalFunctionValue = ref(None) + let length = Belt.Array.length(a) - while (i.contents < length) && (finalFunctionValue.contents == None) { + while i.contents < length && finalFunctionValue.contents == None { let itemWithFnApplied = Belt.Array.getUnsafe(a, i.contents) |> fn if boolCondition(itemWithFnApplied) { finalFunctionValue := Some(itemWithFnApplied) From 39a9cd4eb9c5fe3829160d39536877c61d5b267a Mon Sep 17 00:00:00 2001 From: Quinn Dougherty Date: Wed, 31 Aug 2022 10:31:32 +0800 Subject: [PATCH 3/4] a few unit tests --- packages/squiggle-lang/__tests__/E/A_test.res | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 packages/squiggle-lang/__tests__/E/A_test.res diff --git a/packages/squiggle-lang/__tests__/E/A_test.res b/packages/squiggle-lang/__tests__/E/A_test.res new file mode 100644 index 00000000..c40dba7f --- /dev/null +++ b/packages/squiggle-lang/__tests__/E/A_test.res @@ -0,0 +1,21 @@ +open Jest +open TestHelpers + +describe("E.A.getByWithFn", () => { + makeTest("Empty list returns None", E.A.getByWithFn([], x => x + 1, x => mod(x, 2) == 0), None) + makeTest( + "Never predicate returns None", + E.A.getByWithFn([1, 2, 3, 4, 5, 6], x => x + 1, _ => false), + None, + ) + makeTest( + "function evaluates", + E.A.getByWithFn([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.getByWithFn([0, 1, 2, 3, 4, 5, 6], x => 10 + x, _ => true), + Some(10), + ) +}) From 6631c9bad78a4b0f34d0c5f3c4e8b05aeddacade Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 31 Aug 2022 19:56:12 -0700 Subject: [PATCH 4/4] Renamed getByWithFn --- packages/squiggle-lang/__tests__/E/A_test.res | 10 +++++----- packages/squiggle-lang/src/rescript/Utility/E.res | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/squiggle-lang/__tests__/E/A_test.res b/packages/squiggle-lang/__tests__/E/A_test.res index c40dba7f..81a38d36 100644 --- a/packages/squiggle-lang/__tests__/E/A_test.res +++ b/packages/squiggle-lang/__tests__/E/A_test.res @@ -1,21 +1,21 @@ open Jest open TestHelpers -describe("E.A.getByWithFn", () => { - makeTest("Empty list returns None", E.A.getByWithFn([], x => x + 1, x => mod(x, 2) == 0), None) +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.getByWithFn([1, 2, 3, 4, 5, 6], x => x + 1, _ => false), + E.A.getByFmap([1, 2, 3, 4, 5, 6], x => x + 1, _ => false), None, ) makeTest( "function evaluates", - E.A.getByWithFn([1, 1, 1, 1, 1, 1, 1, 2, 1, 1], x => 3 * x, x => x > 4), + 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.getByWithFn([0, 1, 2, 3, 4, 5, 6], x => 10 + x, _ => true), + E.A.getByFmap([0, 1, 2, 3, 4, 5, 6], x => 10 + x, _ => true), Some(10), ) }) diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index a2012efd..bceb12c7 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -572,7 +572,7 @@ module A = { |> (x => Ok(x)) } - let getByWithFn = (a, fn, boolCondition) => { + let getByFmap = (a, fn, boolCondition) => { let i = ref(0) let finalFunctionValue = ref(None) let length = Belt.Array.length(a) @@ -690,7 +690,7 @@ module A = { let firstSome = x => Belt.Array.getBy(x, O.isSome) let firstSomeFn = (r: array option<'a>>): option<'a> => - O.flatten(getByWithFn(r, l => l(), O.isSome)) + O.flatten(getByFmap(r, l => l(), O.isSome)) let firstSomeFnWithDefault = (r, default) => firstSomeFn(r)->O2.default(default)