diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 27 | ||||
-rw-r--r-- | src/gleam/list.gleam | 64 | ||||
-rw-r--r-- | src/gleam/should.gleam | 8 | ||||
-rw-r--r-- | src/gleam/string_builder.gleam | 2 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 13 |
5 files changed, 61 insertions, 53 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index b3f0df2..8fdb79e 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -1,8 +1,5 @@ import gleam/order.{Order} - -if erlang { - import gleam/string_builder -} +import gleam/string_builder /// Attempts to parse a string as a float, returning `Error(Nil)` if it was not /// possible. @@ -28,18 +25,16 @@ if javascript { "../gleam_stdlib.js" "parse_float" } -if erlang { - /// Returns the string representation of the provided float. - /// - /// ## Examples - /// > to_string(2.3) - /// "2.3" - /// - pub fn to_string(f: Float) -> String { - f - |> string_builder.from_float - |> string_builder.to_string - } +/// Returns the string representation of the provided float. +/// +/// ## Examples +/// > to_string(2.3) +/// "2.3" +/// +pub fn to_string(f: Float) -> String { + f + |> string_builder.from_float + |> string_builder.to_string } /// Restricts a Float between a lower and upper bound diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 341b4a1..414f64e 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -480,10 +480,10 @@ if javascript { do_append_acc(reverse(first), second) } - fn do_append_acc(remaining: List(a), second: List(a)) -> List(a) { - case remaining { + fn do_append_acc(first: List(a), second: List(a)) -> List(a) { + case first { [] -> second - [item, ..rest] -> do_append(rest, [item, ..second]) + [item, ..rest] -> do_append_acc(rest, [item, ..second]) } } } @@ -1377,37 +1377,41 @@ pub fn take_while( do_take_while(list, predicate, []) } -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) +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) + } } } } -} -/// 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], []) + } } } diff --git a/src/gleam/should.gleam b/src/gleam/should.gleam index c6c8992..3566dc2 100644 --- a/src/gleam/should.gleam +++ b/src/gleam/should.gleam @@ -35,9 +35,9 @@ if javascript { _ -> crash(string.concat([ "\n", - stringify(b), - "\nshould equal \n", stringify(a), + "\nshould equal \n", + stringify(b), "\n", ])) } @@ -49,9 +49,9 @@ if javascript { _ -> crash(string.concat([ "\n", - stringify(b), - "\nshould not equal \n", stringify(a), + "\nshould not equal \n", + stringify(b), ])) } } diff --git a/src/gleam/string_builder.gleam b/src/gleam/string_builder.gleam index ac6817f..0d464ca 100644 --- a/src/gleam/string_builder.gleam +++ b/src/gleam/string_builder.gleam @@ -164,7 +164,7 @@ if erlang { if javascript { external fn do_from_float(Float) -> StringBuilder = - "../gleam_stdlib.js" "to_string" + "../gleam_stdlib.js" "float_to_string" } /// Converts a builder to a new builder where the contents have been diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index a60b361..95fb526 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -36,8 +36,17 @@ export function parse_float(value) { } } -export function to_string(int) { - return int.toString(); +export function to_string(term) { + return term.toString(); +} + +export function float_to_string(float) { + let string = float.toString(); + if (string.indexOf(".") >= 0) { + return string; + } else { + return string + ".0"; + } } export function int_to_base_string(int, base) { |