diff options
-rw-r--r-- | src/gleam/iterator.gleam | 20 | ||||
-rw-r--r-- | test/gleam/iterator_test.gleam | 10 |
2 files changed, 19 insertions, 11 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 8ce7355..db57fea 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_flatten(flattened: fn() -> Action(Iterator(a))) -> Action(a) { +fn do_concat(flattened: fn() -> Action(Iterator(a))) -> Action(a) { case flattened() { Stop -> Stop Continue(it, next_iterator) -> - do_append(it.continuation, fn() { do_flatten(next_iterator) }) + do_append(it.continuation, fn() { do_concat(next_iterator) }) } } @@ -431,13 +431,21 @@ fn do_flatten(flattened: fn() -> Action(Iterator(a))) -> Action(a) { /// ```gleam /// > from_list([[1, 2], [3, 4]]) /// > |> map(from_list) -/// > |> flatten +/// > |> from_list +/// > |> concat /// > |> to_list /// [1, 2, 3, 4] /// ``` /// +pub fn concat(iterator: Iterator(Iterator(a))) -> Iterator(a) { + fn() { do_concat(iterator.continuation) } + |> Iterator +} + +// TODO: Add deprecation attribute and then remove later. +/// This function is deprecated, see `concat` instead. pub fn flatten(iterator: Iterator(Iterator(a))) -> Iterator(a) { - fn() { do_flatten(iterator.continuation) } + fn() { do_concat(iterator.continuation) } |> Iterator } @@ -466,7 +474,7 @@ pub fn flat_map( ) -> Iterator(b) { iterator |> map(f) - |> flatten + |> concat } fn do_filter( @@ -525,7 +533,7 @@ pub fn filter( /// pub fn cycle(iterator: Iterator(a)) -> Iterator(a) { repeat(iterator) - |> flatten + |> concat } /// 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 85f8722..8b7e01d 100644 --- a/test/gleam/iterator_test.gleam +++ b/test/gleam/iterator_test.gleam @@ -156,7 +156,7 @@ pub fn map_test() { } // a |> from_list |> flat_map(f) |> to_list == -// a |> list.map(f) |> list.map(to_list) |> list.flatten +// a |> list.map(f) |> list.map(to_list) |> list.concat pub fn flat_map_test() { let test = fn(subject, f) { subject @@ -178,7 +178,7 @@ pub fn flat_map_test() { test([1, 2], f) } -// a |> from_list |> append(from_list(b)) |> to_list == list.flatten([a, b]) +// a |> from_list |> append(from_list(b)) |> to_list == list.concat([a, b]) pub fn append_test() { let test = fn(left, right) { left @@ -193,13 +193,13 @@ pub fn append_test() { test([1, 2], [3, 4]) } -// a |> list.map(from_list) |> flatten |> to_list == list.flatten(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.from_list - |> iterator.flatten + |> iterator.concat |> iterator.to_list |> should.equal(list.concat(lists)) } |