diff options
author | Marcin Puc <marcin.e.puc@gmail.com> | 2021-03-17 19:27:49 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-03-20 13:35:54 +0000 |
commit | bfc3589325a94c68d5dd08ebbecb8d1767dfdef2 (patch) | |
tree | ee48a6fc9776865452c86549a5006fd00ef39bc1 | |
parent | aaa3bcac4757f46edae58c8d4fa30411ede2c7a2 (diff) | |
download | gleam_stdlib-bfc3589325a94c68d5dd08ebbecb8d1767dfdef2.tar.gz gleam_stdlib-bfc3589325a94c68d5dd08ebbecb8d1767dfdef2.zip |
Rename chunk_by to chunk
-rw-r--r-- | src/gleam/iterator.gleam | 22 | ||||
-rw-r--r-- | src/gleam/list.gleam | 13 | ||||
-rw-r--r-- | test/gleam/iterator_test.gleam | 4 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 4 |
4 files changed, 21 insertions, 22 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 7e16121..3a939d0 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -661,39 +661,39 @@ pub fn zip(left: Iterator(a), right: Iterator(b)) -> Iterator(tuple(a, b)) { |> Iterator } -type ChunkBy(element, key) { +type Chunk(element, key) { AnotherBy(List(element), key, element, fn() -> Action(element)) LastBy(List(element)) } -fn next_chunk_by( +fn next_chunk( continuation: fn() -> Action(element), f: fn(element) -> key, previous_key: key, current_chunk: List(element), -) -> ChunkBy(element, key) { +) -> Chunk(element, key) { case continuation() { Stop -> LastBy(list.reverse(current_chunk)) Continue(e, next) -> { let key = f(e) case key == previous_key { - True -> next_chunk_by(next, f, key, [e, ..current_chunk]) + True -> next_chunk(next, f, key, [e, ..current_chunk]) False -> AnotherBy(list.reverse(current_chunk), key, e, next) } } } } -fn do_chunk_by( +fn do_chunk( continuation: fn() -> Action(element), f: fn(element) -> key, previous_key: key, previous_element: element, ) -> Action(List(element)) { - case next_chunk_by(continuation, f, previous_key, [previous_element]) { + case next_chunk(continuation, f, previous_key, [previous_element]) { LastBy(chunk) -> Continue(chunk, stop) AnotherBy(chunk, key, el, next) -> - Continue(chunk, fn() { do_chunk_by(next, f, key, el) }) + Continue(chunk, fn() { do_chunk(next, f, key, el) }) } } @@ -702,17 +702,17 @@ fn do_chunk_by( /// /// ## Examples /// -/// > from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) |> chunk_by(fn(n) { n % 2 }) |> to_list +/// > from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) |> chunk(by: fn(n) { n % 2 }) |> to_list /// [[1], [2, 2], [3], [4, 4, 6], [7, 7]] /// -pub fn chunk_by( +pub fn chunk( over iterator: Iterator(element), - with f: fn(element) -> key, + by f: fn(element) -> key, ) -> Iterator(List(element)) { fn() { case iterator.continuation() { Stop -> Stop - Continue(e, next) -> do_chunk_by(next, f, f(e), e) + Continue(e, next) -> do_chunk(next, f, f(e), e) } } |> Iterator diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index df62886..066c584 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1270,7 +1270,7 @@ pub fn take_while( do_take_while(list, predicate, []) } -fn do_chunk_by( +fn do_chunk( list: List(a), f: fn(a) -> key, previous_key: key, @@ -1282,9 +1282,8 @@ fn do_chunk_by( [head, ..tail] -> { let key = f(head) case key == previous_key { - False -> - do_chunk_by(tail, f, key, [head], [reverse(current_chunk), ..acc]) - True -> do_chunk_by(tail, f, key, [head, ..current_chunk], acc) + False -> do_chunk(tail, f, key, [head], [reverse(current_chunk), ..acc]) + True -> do_chunk(tail, f, key, [head, ..current_chunk], acc) } } } @@ -1295,12 +1294,12 @@ fn do_chunk_by( /// /// ## Examples /// -/// > [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk_by(fn(n) { n % 2 }) +/// > [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk(by: fn(n) { n % 2 }) /// [[1], [2, 2], [3], [4, 4, 6], [7, 7]] /// -pub fn chunk_by(in list: List(a), with f: fn(a) -> key) -> List(List(a)) { +pub fn chunk(in list: List(a), by f: fn(a) -> key) -> List(List(a)) { case list { [] -> [] - [head, ..tail] -> do_chunk_by(tail, f, f(head), [head], []) + [head, ..tail] -> do_chunk(tail, f, f(head), [head], []) } } diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam index 2c62ff4..49c65fe 100644 --- a/test/gleam/iterator_test.gleam +++ b/test/gleam/iterator_test.gleam @@ -299,9 +299,9 @@ pub fn zip_test() { |> should.equal([tuple("a", 20), tuple("b", 21), tuple("c", 22)]) } -pub fn chunk_by_test() { +pub fn chunk_test() { iterator.from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) - |> iterator.chunk_by(fn(n) { n % 2 }) + |> iterator.chunk(by: fn(n) { n % 2 }) |> iterator.to_list |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) } diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 091bd96..8c414a8 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -591,8 +591,8 @@ pub fn take_while_test() { |> should.equal([1, 2]) } -pub fn chunk_by_test() { +pub fn chunk_test() { [1, 2, 2, 3, 4, 4, 6, 7, 7] - |> list.chunk_by(fn(n) { n % 2 }) + |> list.chunk(by: fn(n) { n % 2 }) |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) } |