Rename List.find and findIndex to include 'First'

This commit is contained in:
Richard Feldman 2022-07-22 09:30:46 -04:00 committed by Folkert
parent 661376958c
commit f3b8025d8d
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
4 changed files with 17 additions and 17 deletions

View file

@ -81,7 +81,7 @@ withCapacity = \n -> @Dict (List.withCapacity n)
get : Dict k v, k -> Result v [KeyNotFound]*
get = \@Dict list, needle ->
when List.find list (\Pair key _ -> key == needle) is
when List.findFirst list (\Pair key _ -> key == needle) is
Ok (Pair _ v) ->
Ok v
@ -94,7 +94,7 @@ walk = \@Dict list, initialState, transform ->
insert : Dict k v, k, v -> Dict k v
insert = \@Dict list, k, v ->
when List.findIndex list (\Pair key _ -> key == k) is
when List.findFirstIndex list (\Pair key _ -> key == k) is
Err NotFound ->
insertFresh (@Dict list) k v
@ -109,7 +109,7 @@ len = \@Dict list ->
remove : Dict k v, k -> Dict k v
remove = \@Dict list, key ->
when List.findIndex list (\Pair k _ -> k == key) is
when List.findFirstIndex list (\Pair k _ -> k == key) is
Err NotFound ->
@Dict list

View file

@ -44,8 +44,8 @@ interface List
any,
takeFirst,
takeLast,
find,
findIndex,
findFirst,
findFirstIndex,
sublist,
intersperse,
split,
@ -774,8 +774,8 @@ joinMap = \list, mapper ->
## Returns the first element of the list satisfying a predicate function.
## If no satisfying element is found, an `Err NotFound` is returned.
find : List elem, (elem -> Bool) -> Result elem [NotFound]*
find = \array, pred ->
findFirst : List elem, (elem -> Bool) -> Result elem [NotFound]*
findFirst = \array, pred ->
callback = \_, elem ->
if pred elem then
Break elem
@ -792,8 +792,8 @@ find = \array, pred ->
## Returns the index at which the first element in the list
## satisfying a predicate function can be found.
## If no satisfying element is found, an `Err NotFound` is returned.
findIndex : List elem, (elem -> Bool) -> Result Nat [NotFound]*
findIndex = \list, matcher ->
findFirstIndex : List elem, (elem -> Bool) -> Result Nat [NotFound]*
findFirstIndex = \list, matcher ->
foundIndex = List.iterate list 0 \index, elem ->
if matcher elem then
Break index

View file

@ -1266,7 +1266,7 @@ define_builtins! {
42 LIST_TAKE_FIRST: "takeFirst"
43 LIST_TAKE_LAST: "takeLast"
44 LIST_FIND: "find"
45 LIST_FIND_RESULT: "#find_result" // symbol used in the definition of List.find
45 LIST_FIND_RESULT: "#find_result" // symbol used in the definition of List.findFirst
46 LIST_SUBLIST: "sublist"
47 LIST_INTERSPERSE: "intersperse"
48 LIST_INTERSPERSE_CLOS: "#intersperseClos"

View file

@ -2846,7 +2846,7 @@ fn list_find() {
assert_evals_to!(
indoc!(
r#"
when List.find ["a", "bc", "def"] (\s -> Str.countGraphemes s > 1) is
when List.findFirst ["a", "bc", "def"] (\s -> Str.countGraphemes s > 1) is
Ok v -> v
Err _ -> "not found"
"#
@ -2862,7 +2862,7 @@ fn list_find_not_found() {
assert_evals_to!(
indoc!(
r#"
when List.find ["a", "bc", "def"] (\s -> Str.countGraphemes s > 5) is
when List.findFirst ["a", "bc", "def"] (\s -> Str.countGraphemes s > 5) is
Ok v -> v
Err _ -> "not found"
"#
@ -2878,7 +2878,7 @@ fn list_find_empty_typed_list() {
assert_evals_to!(
indoc!(
r#"
when List.find [] (\s -> Str.countGraphemes s > 5) is
when List.findFirst [] (\s -> Str.countGraphemes s > 5) is
Ok v -> v
Err _ -> "not found"
"#
@ -2895,7 +2895,7 @@ fn list_find_empty_layout() {
assert_evals_to!(
indoc!(
r#"
List.find [] (\_ -> True)
List.findFirst [] (\_ -> True)
"#
),
0,
@ -2909,7 +2909,7 @@ fn list_find_index() {
assert_evals_to!(
indoc!(
r#"
when List.findIndex ["a", "bc", "def"] (\s -> Str.countGraphemes s > 1) is
when List.findFirstIndex ["a", "bc", "def"] (\s -> Str.countGraphemes s > 1) is
Ok v -> v
Err _ -> 999
"#
@ -2925,7 +2925,7 @@ fn list_find_index_not_found() {
assert_evals_to!(
indoc!(
r#"
when List.findIndex ["a", "bc", "def"] (\s -> Str.countGraphemes s > 5) is
when List.findFirstIndex ["a", "bc", "def"] (\s -> Str.countGraphemes s > 5) is
Ok v -> v
Err _ -> 999
"#
@ -2941,7 +2941,7 @@ fn list_find_index_empty_typed_list() {
assert_evals_to!(
indoc!(
r#"
when List.findIndex [] (\s -> Str.countGraphemes s > 5) is
when List.findFirstIndex [] (\s -> Str.countGraphemes s > 5) is
Ok v -> v
Err _ -> 999
"#