diff options
author | Kayla Washburn <mckayla@hey.com> | 2023-07-13 07:34:25 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-13 14:34:25 +0100 |
commit | 55e7b16fefdcc477719dda3e606828e154c35bdf (patch) | |
tree | 54ac4edb4a34a2126f964d55ce1e2bea9923d180 | |
parent | 682d9e03ba2847c63b9f3882bb3d743b67552def (diff) | |
download | gleam_stdlib-55e7b16fefdcc477719dda3e606828e154c35bdf.tar.gz gleam_stdlib-55e7b16fefdcc477719dda3e606828e154c35bdf.zip |
rename `list.flatten` to `list.concat` (#477)
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/dynamic.gleam | 16 | ||||
-rw-r--r-- | src/gleam/list.gleam | 24 | ||||
-rw-r--r-- | test/gleam/iterator_test.gleam | 6 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 12 |
5 files changed, 30 insertions, 29 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b2e66b..18f9c04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - The `list` module gains the `list.map2` function. +- `flatten` has been renamed to `concat` in the `list` module. ## v0.29.2 - 2023-06-21 diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 86055a0..f6b1717 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -1190,7 +1190,7 @@ pub fn decode2( fn(value) { case t1(value), t2(value) { Ok(a), Ok(b) -> Ok(constructor(a, b)) - a, b -> Error(list.flatten([all_errors(a), all_errors(b)])) + a, b -> Error(list.concat([all_errors(a), all_errors(b)])) } } } @@ -1224,7 +1224,7 @@ pub fn decode3( case t1(value), t2(value), t3(value) { Ok(a), Ok(b), Ok(c) -> Ok(constructor(a, b, c)) a, b, c -> - Error(list.flatten([all_errors(a), all_errors(b), all_errors(c)])) + Error(list.concat([all_errors(a), all_errors(b), all_errors(c)])) } } } @@ -1271,7 +1271,7 @@ pub fn decode4( case t1(x), t2(x), t3(x), t4(x) { Ok(a), Ok(b), Ok(c), Ok(d) -> Ok(constructor(a, b, c, d)) a, b, c, d -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), @@ -1326,7 +1326,7 @@ pub fn decode5( case t1(x), t2(x), t3(x), t4(x), t5(x) { Ok(a), Ok(b), Ok(c), Ok(d), Ok(e) -> Ok(constructor(a, b, c, d, e)) a, b, c, d, e -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), @@ -1386,7 +1386,7 @@ pub fn decode6( Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f) -> Ok(constructor(a, b, c, d, e, f)) a, b, c, d, e, f -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), @@ -1450,7 +1450,7 @@ pub fn decode7( Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g) -> Ok(constructor(a, b, c, d, e, f, g)) a, b, c, d, e, f, g -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), @@ -1518,7 +1518,7 @@ pub fn decode8( Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h) -> Ok(constructor(a, b, c, d, e, f, g, h)) a, b, c, d, e, f, g, h -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), @@ -1590,7 +1590,7 @@ pub fn decode9( Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h), Ok(i) -> Ok(constructor(a, b, c, d, e, f, g, h, i)) a, b, c, d, e, f, g, h, i -> - Error(list.flatten([ + Error(list.concat([ all_errors(a), all_errors(b), all_errors(c), diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index b1acb74..ed1d718 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -660,30 +660,30 @@ fn reverse_and_prepend(list prefix: List(a), to suffix: List(a)) -> List(a) { } } -fn do_flatten(lists: List(List(a)), acc: List(a)) -> List(a) { +fn do_concat(lists: List(List(a)), acc: List(a)) -> List(a) { case lists { [] -> reverse(acc) [list, ..further_lists] -> - do_flatten(further_lists, reverse_and_prepend(list: list, to: acc)) + do_concat(further_lists, reverse_and_prepend(list: list, to: acc)) } } -/// Flattens a list of lists into a single list. +/// Joins a list of lists into a single list. /// /// This function traverses all elements twice. /// /// ## Examples /// /// ```gleam -/// > flatten([[1], [2, 3], []]) +/// > concat([[1], [2, 3], []]) /// [1, 2, 3] /// ``` /// -pub fn flatten(lists: List(List(a))) -> List(a) { - do_flatten(lists, []) +pub fn concat(lists: List(List(a))) -> List(a) { + do_concat(lists, []) } -/// Maps the list with the given function and then flattens it. +/// Maps the list with the given function into a list of lists, and then flattens it. /// /// ## Examples /// @@ -694,7 +694,7 @@ pub fn flatten(lists: List(List(a))) -> List(a) { /// pub fn flat_map(over list: List(a), with fun: fn(a) -> List(b)) -> List(b) { map(list, fun) - |> flatten + |> concat } /// Reduces a list of elements into a single value by calling a given function @@ -1684,7 +1684,7 @@ pub fn permutations(l: List(a)) -> List(List(a)) { |> permutations |> map(fn(permutation) { [i, ..permutation] }) }) - |> flatten + |> concat } } @@ -2012,7 +2012,7 @@ fn do_combination_pairs(items: List(a)) -> List(List(#(a, a))) { /// pub fn combination_pairs(items: List(a)) -> List(#(a, a)) { do_combination_pairs(items) - |> flatten + |> concat } /// Make a list alternating the elements from the given lists @@ -2026,7 +2026,7 @@ pub fn combination_pairs(items: List(a)) -> List(#(a, a)) { /// pub fn interleave(list: List(List(a))) -> List(a) { transpose(list) - |> flatten + |> concat } /// Transpose rows and columns of the list of lists. @@ -2058,7 +2058,7 @@ pub fn transpose(list_of_list: List(List(a))) -> List(List(a)) { let firsts = rows |> map(take_first) - |> flatten + |> concat let rest = transpose(map(rows, drop(_, 1))) [firsts, ..rest] } diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam index 5348183..bb9a825 100644 --- a/test/gleam/iterator_test.gleam +++ b/test/gleam/iterator_test.gleam @@ -167,7 +167,7 @@ pub fn flat_map_test() { subject |> list.map(f) |> list.map(iterator.to_list) - |> list.flatten, + |> list.concat, ) } @@ -185,7 +185,7 @@ pub fn append_test() { |> iterator.from_list |> iterator.append(iterator.from_list(right)) |> iterator.to_list - |> should.equal(list.flatten([left, right])) + |> should.equal(list.concat([left, right])) } test([], []) @@ -201,7 +201,7 @@ pub fn flatten_test() { |> iterator.from_list |> iterator.flatten |> iterator.to_list - |> should.equal(list.flatten(lists)) + |> should.equal(list.concat(lists)) } test([[], []]) diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index bf8bb2a..2883535 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -287,23 +287,23 @@ pub fn append_test() { |> list.append([1]) } -pub fn flatten_test() { - list.flatten([]) +pub fn concat_test() { + list.concat([]) |> should.equal([]) - list.flatten([[]]) + list.concat([[]]) |> should.equal([]) - list.flatten([[], [], []]) + list.concat([[], [], []]) |> should.equal([]) - list.flatten([[1, 2], [], [3, 4]]) + list.concat([[1, 2], [], [3, 4]]) |> should.equal([1, 2, 3, 4]) // // TCO test // case recursion_test_cycles > 2 { // True -> // list.repeat([[1]], recursion_test_cycles / 50) - // |> list.flatten() + // |> list.concat() // False -> [] // } } |