aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiacomo Cavalieri <giacomo.cavalieri@icloud.com>2023-08-26 19:19:34 +0200
committerLouis Pilfold <louis@lpil.uk>2023-09-04 12:50:50 +0100
commit60d62bd3a551f34335274cb33b4611a038b643ff (patch)
treebac786d13e5708e55de0d8e33884b7e039e62f7b
parentbb7798efd8b75c8770d3b17d0bd4683c2abd0ea0 (diff)
downloadgleam_stdlib-60d62bd3a551f34335274cb33b4611a038b643ff.tar.gz
gleam_stdlib-60d62bd3a551f34335274cb33b4611a038b643ff.zip
swap `iterator.flatten` and `iterator.concat`
-rw-r--r--src/gleam/iterator.gleam20
-rw-r--r--test/gleam/iterator_test.gleam12
2 files changed, 16 insertions, 16 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam
index f4421a3..1718600 100644
--- a/src/gleam/iterator.gleam
+++ b/src/gleam/iterator.gleam
@@ -413,11 +413,11 @@ pub fn append(to first: Iterator(a), suffix second: Iterator(a)) -> Iterator(a)
|> Iterator
}
-fn do_concat(flattened: fn() -> Action(Iterator(a))) -> Action(a) {
+fn do_flatten(flattened: fn() -> Action(Iterator(a))) -> Action(a) {
case flattened() {
Stop -> Stop
Continue(it, next_iterator) ->
- do_append(it.continuation, fn() { do_concat(next_iterator) })
+ do_append(it.continuation, fn() { do_flatten(next_iterator) })
}
}
@@ -432,13 +432,13 @@ fn do_concat(flattened: fn() -> Action(Iterator(a))) -> Action(a) {
/// > from_list([[1, 2], [3, 4]])
/// > |> map(from_list)
/// > |> from_list
-/// > |> concat
+/// > |> flatten
/// > |> to_list
/// [1, 2, 3, 4]
/// ```
///
-pub fn concat(iterator: Iterator(Iterator(a))) -> Iterator(a) {
- fn() { do_concat(iterator.continuation) }
+pub fn flatten(iterator: Iterator(Iterator(a))) -> Iterator(a) {
+ fn() { do_flatten(iterator.continuation) }
|> Iterator
}
@@ -452,13 +452,13 @@ pub fn concat(iterator: Iterator(Iterator(a))) -> Iterator(a) {
/// ```gleam
/// > [[1, 2], [3, 4]]
/// > |> map(from_list)
-/// > |> flatten
+/// > |> concat
/// > |> to_list
/// [1, 2, 3, 4]
/// ```
///
-pub fn flatten(iterators: List(Iterator(a))) -> Iterator(a) {
- concat(from_list(iterators))
+pub fn concat(iterators: List(Iterator(a))) -> Iterator(a) {
+ flatten(from_list(iterators))
}
/// Creates an iterator from an existing iterator and a transformation function.
@@ -486,7 +486,7 @@ pub fn flat_map(
) -> Iterator(b) {
iterator
|> map(f)
- |> concat
+ |> flatten
}
fn do_filter(
@@ -545,7 +545,7 @@ pub fn filter(
///
pub fn cycle(iterator: Iterator(a)) -> Iterator(a) {
repeat(iterator)
- |> concat
+ |> flatten
}
/// Creates an iterator of ints, starting at a given start int and stepping by
diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam
index 5d10cb2..66e8bd4 100644
--- a/test/gleam/iterator_test.gleam
+++ b/test/gleam/iterator_test.gleam
@@ -193,13 +193,13 @@ pub fn append_test() {
test([1, 2], [3, 4])
}
-// a |> list.map(from_list) |> from_list |> concat |> to_list == list.concat(a)
-pub fn concat_test() {
+// a |> list.map(from_list) |> from_list |> flatten |> to_list == list.concat(a)
+pub fn flatten_test() {
let test = fn(lists) {
lists
|> list.map(iterator.from_list)
|> iterator.from_list
- |> iterator.concat
+ |> iterator.flatten
|> iterator.to_list
|> should.equal(list.concat(lists))
}
@@ -209,12 +209,12 @@ pub fn concat_test() {
test([[1, 2], [3, 4]])
}
-// a |> list.map(from_list) |> flatten |> to_list == list.concat(a)
-pub fn flatten_test() {
+// a |> list.map(from_list) |> concat |> to_list == list.concat(a)
+pub fn concat_test() {
let test = fn(lists) {
lists
|> list.map(iterator.from_list)
- |> iterator.flatten
+ |> iterator.concat
|> iterator.to_list
|> should.equal(list.concat(lists))
}