diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-06 19:56:45 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-06 19:56:45 +0100 |
commit | dd8a7bacc5db670bb084e8c77b02c0c1c44f605d (patch) | |
tree | 0812798f5b97ff1e1cd8a017b92e4bf478a98092 /src | |
parent | 91c0357c6ca3092525b798ad7dfee79fc308b2b9 (diff) | |
download | gleam_stdlib-dd8a7bacc5db670bb084e8c77b02c0c1c44f605d.tar.gz gleam_stdlib-dd8a7bacc5db670bb084e8c77b02c0c1c44f605d.zip |
Work around JS compiler bug
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/list.gleam | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 414f64e..fdf2caf 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1377,41 +1377,40 @@ pub fn take_while( do_take_while(list, predicate, []) } -if erlang { - // TODO: JavaScript fix - fn do_chunk( - list: List(a), - f: fn(a) -> key, - previous_key: key, - current_chunk: List(a), - acc: List(List(a)), - ) -> List(List(a)) { - case list { - [] -> reverse([reverse(current_chunk), ..acc]) - [head, ..tail] -> { - let key = f(head) - case key == previous_key { - False -> - do_chunk(tail, f, key, [head], [reverse(current_chunk), ..acc]) - True -> do_chunk(tail, f, key, [head, ..current_chunk], acc) +fn do_chunk( + list: List(a), + f: fn(a) -> key, + previous_key: key, + current_chunk: List(a), + acc: List(List(a)), +) -> List(List(a)) { + case list { + [head, ..tail] -> { + let key = f(head) + case key == previous_key { + False -> { + let new_acc = [reverse(current_chunk), ..acc] + do_chunk(tail, f, key, [head], new_acc) } + _true -> do_chunk(tail, f, key, [head, ..current_chunk], acc) } } + _empty -> reverse([reverse(current_chunk), ..acc]) } +} - /// Returns a list of chunks in which - /// the result of calling `f` on each element is the same. - /// - /// ## 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]] - /// - pub fn chunk(in list: List(a), by f: fn(a) -> key) -> List(List(a)) { - case list { - [] -> [] - [head, ..tail] -> do_chunk(tail, f, f(head), [head], []) - } +/// Returns a list of chunks in which +/// the result of calling `f` on each element is the same. +/// +/// ## 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]] +/// +pub fn chunk(in list: List(a), by f: fn(a) -> key) -> List(List(a)) { + case list { + [] -> [] + [head, ..tail] -> do_chunk(tail, f, f(head), [head], []) } } |