Skip to content

Commit 55e7b16

Browse files
authored
rename list.flatten to list.concat (#477)
1 parent 682d9e0 commit 55e7b16

File tree

5 files changed

+30
-29
lines changed

5 files changed

+30
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- The `list` module gains the `list.map2` function.
6+
- `flatten` has been renamed to `concat` in the `list` module.
67

78
## v0.29.2 - 2023-06-21
89

src/gleam/dynamic.gleam

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ pub fn decode2(
11901190
fn(value) {
11911191
case t1(value), t2(value) {
11921192
Ok(a), Ok(b) -> Ok(constructor(a, b))
1193-
a, b -> Error(list.flatten([all_errors(a), all_errors(b)]))
1193+
a, b -> Error(list.concat([all_errors(a), all_errors(b)]))
11941194
}
11951195
}
11961196
}
@@ -1224,7 +1224,7 @@ pub fn decode3(
12241224
case t1(value), t2(value), t3(value) {
12251225
Ok(a), Ok(b), Ok(c) -> Ok(constructor(a, b, c))
12261226
a, b, c ->
1227-
Error(list.flatten([all_errors(a), all_errors(b), all_errors(c)]))
1227+
Error(list.concat([all_errors(a), all_errors(b), all_errors(c)]))
12281228
}
12291229
}
12301230
}
@@ -1271,7 +1271,7 @@ pub fn decode4(
12711271
case t1(x), t2(x), t3(x), t4(x) {
12721272
Ok(a), Ok(b), Ok(c), Ok(d) -> Ok(constructor(a, b, c, d))
12731273
a, b, c, d ->
1274-
Error(list.flatten([
1274+
Error(list.concat([
12751275
all_errors(a),
12761276
all_errors(b),
12771277
all_errors(c),
@@ -1326,7 +1326,7 @@ pub fn decode5(
13261326
case t1(x), t2(x), t3(x), t4(x), t5(x) {
13271327
Ok(a), Ok(b), Ok(c), Ok(d), Ok(e) -> Ok(constructor(a, b, c, d, e))
13281328
a, b, c, d, e ->
1329-
Error(list.flatten([
1329+
Error(list.concat([
13301330
all_errors(a),
13311331
all_errors(b),
13321332
all_errors(c),
@@ -1386,7 +1386,7 @@ pub fn decode6(
13861386
Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f) ->
13871387
Ok(constructor(a, b, c, d, e, f))
13881388
a, b, c, d, e, f ->
1389-
Error(list.flatten([
1389+
Error(list.concat([
13901390
all_errors(a),
13911391
all_errors(b),
13921392
all_errors(c),
@@ -1450,7 +1450,7 @@ pub fn decode7(
14501450
Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g) ->
14511451
Ok(constructor(a, b, c, d, e, f, g))
14521452
a, b, c, d, e, f, g ->
1453-
Error(list.flatten([
1453+
Error(list.concat([
14541454
all_errors(a),
14551455
all_errors(b),
14561456
all_errors(c),
@@ -1518,7 +1518,7 @@ pub fn decode8(
15181518
Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h) ->
15191519
Ok(constructor(a, b, c, d, e, f, g, h))
15201520
a, b, c, d, e, f, g, h ->
1521-
Error(list.flatten([
1521+
Error(list.concat([
15221522
all_errors(a),
15231523
all_errors(b),
15241524
all_errors(c),
@@ -1590,7 +1590,7 @@ pub fn decode9(
15901590
Ok(a), Ok(b), Ok(c), Ok(d), Ok(e), Ok(f), Ok(g), Ok(h), Ok(i) ->
15911591
Ok(constructor(a, b, c, d, e, f, g, h, i))
15921592
a, b, c, d, e, f, g, h, i ->
1593-
Error(list.flatten([
1593+
Error(list.concat([
15941594
all_errors(a),
15951595
all_errors(b),
15961596
all_errors(c),

src/gleam/list.gleam

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -660,30 +660,30 @@ fn reverse_and_prepend(list prefix: List(a), to suffix: List(a)) -> List(a) {
660660
}
661661
}
662662

663-
fn do_flatten(lists: List(List(a)), acc: List(a)) -> List(a) {
663+
fn do_concat(lists: List(List(a)), acc: List(a)) -> List(a) {
664664
case lists {
665665
[] -> reverse(acc)
666666
[list, ..further_lists] ->
667-
do_flatten(further_lists, reverse_and_prepend(list: list, to: acc))
667+
do_concat(further_lists, reverse_and_prepend(list: list, to: acc))
668668
}
669669
}
670670

671-
/// Flattens a list of lists into a single list.
671+
/// Joins a list of lists into a single list.
672672
///
673673
/// This function traverses all elements twice.
674674
///
675675
/// ## Examples
676676
///
677677
/// ```gleam
678-
/// > flatten([[1], [2, 3], []])
678+
/// > concat([[1], [2, 3], []])
679679
/// [1, 2, 3]
680680
/// ```
681681
///
682-
pub fn flatten(lists: List(List(a))) -> List(a) {
683-
do_flatten(lists, [])
682+
pub fn concat(lists: List(List(a))) -> List(a) {
683+
do_concat(lists, [])
684684
}
685685

686-
/// Maps the list with the given function and then flattens it.
686+
/// Maps the list with the given function into a list of lists, and then flattens it.
687687
///
688688
/// ## Examples
689689
///
@@ -694,7 +694,7 @@ pub fn flatten(lists: List(List(a))) -> List(a) {
694694
///
695695
pub fn flat_map(over list: List(a), with fun: fn(a) -> List(b)) -> List(b) {
696696
map(list, fun)
697-
|> flatten
697+
|> concat
698698
}
699699

700700
/// 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)) {
16841684
|> permutations
16851685
|> map(fn(permutation) { [i, ..permutation] })
16861686
})
1687-
|> flatten
1687+
|> concat
16881688
}
16891689
}
16901690

@@ -2012,7 +2012,7 @@ fn do_combination_pairs(items: List(a)) -> List(List(#(a, a))) {
20122012
///
20132013
pub fn combination_pairs(items: List(a)) -> List(#(a, a)) {
20142014
do_combination_pairs(items)
2015-
|> flatten
2015+
|> concat
20162016
}
20172017

20182018
/// Make a list alternating the elements from the given lists
@@ -2026,7 +2026,7 @@ pub fn combination_pairs(items: List(a)) -> List(#(a, a)) {
20262026
///
20272027
pub fn interleave(list: List(List(a))) -> List(a) {
20282028
transpose(list)
2029-
|> flatten
2029+
|> concat
20302030
}
20312031

20322032
/// 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)) {
20582058
let firsts =
20592059
rows
20602060
|> map(take_first)
2061-
|> flatten
2061+
|> concat
20622062
let rest = transpose(map(rows, drop(_, 1)))
20632063
[firsts, ..rest]
20642064
}

test/gleam/iterator_test.gleam

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn flat_map_test() {
167167
subject
168168
|> list.map(f)
169169
|> list.map(iterator.to_list)
170-
|> list.flatten,
170+
|> list.concat,
171171
)
172172
}
173173

@@ -185,7 +185,7 @@ pub fn append_test() {
185185
|> iterator.from_list
186186
|> iterator.append(iterator.from_list(right))
187187
|> iterator.to_list
188-
|> should.equal(list.flatten([left, right]))
188+
|> should.equal(list.concat([left, right]))
189189
}
190190

191191
test([], [])
@@ -201,7 +201,7 @@ pub fn flatten_test() {
201201
|> iterator.from_list
202202
|> iterator.flatten
203203
|> iterator.to_list
204-
|> should.equal(list.flatten(lists))
204+
|> should.equal(list.concat(lists))
205205
}
206206

207207
test([[], []])

test/gleam/list_test.gleam

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,23 @@ pub fn append_test() {
287287
|> list.append([1])
288288
}
289289

290-
pub fn flatten_test() {
291-
list.flatten([])
290+
pub fn concat_test() {
291+
list.concat([])
292292
|> should.equal([])
293293

294-
list.flatten([[]])
294+
list.concat([[]])
295295
|> should.equal([])
296296

297-
list.flatten([[], [], []])
297+
list.concat([[], [], []])
298298
|> should.equal([])
299299

300-
list.flatten([[1, 2], [], [3, 4]])
300+
list.concat([[1, 2], [], [3, 4]])
301301
|> should.equal([1, 2, 3, 4])
302302
// // TCO test
303303
// case recursion_test_cycles > 2 {
304304
// True ->
305305
// list.repeat([[1]], recursion_test_cycles / 50)
306-
// |> list.flatten()
306+
// |> list.concat()
307307
// False -> []
308308
// }
309309
}

0 commit comments

Comments
 (0)