diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/int.gleam | 202 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 19 |
2 files changed, 141 insertions, 80 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 5c1a588..0ce53c4 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -1,95 +1,137 @@ import gleam/order.{Order} -if erlang { - pub type Int = - Int - - /// Returns the absolute value of the input. - /// - /// ## Examples - /// - /// > absolute_value(-12) - /// 12 - /// - /// > absolute_value(10) - /// 10 - /// - pub fn absolute_value(num: Int) -> Int { - case num >= 0 { - True -> num - False -> num * -1 - } +pub type Int = + Int + +/// Returns the absolute value of the input. +/// +/// ## Examples +/// +/// > absolute_value(-12) +/// 12 +/// +/// > absolute_value(10) +/// 10 +/// +pub fn absolute_value(num: Int) -> Int { + case num >= 0 { + True -> num + False -> num * -1 } +} + +/// Parses a given string as an int if possible. +/// +/// ## Examples +/// +/// > parse("2") +/// Ok(2) +/// +/// > parse("ABC") +/// Error(Nil) +/// +pub fn parse(string) { + do_parse(string) +} - /// Parses a given string as an int if possible. - /// - /// ## Examples - /// - /// > parse("2") - /// Ok(2) - /// - /// > parse("ABC") - /// Error(Nil) - /// - pub external fn parse(String) -> Result(Int, Nil) = +if erlang { + external fn do_parse(String) -> Result(Int, Nil) = "gleam_stdlib" "parse_int" +} + +if javascript { + external fn do_parse(String) -> Result(Int, Nil) = + "../gleam_stdlib.js" "parse_int" +} + +/// Prints a given int to a string. +/// +/// ## Examples +/// +/// > to_string(2) +/// "2" +/// +pub fn to_string(int) { + do_to_string(int) +} - /// Prints a given int to a string. - /// - /// ## Examples - /// - /// > to_string(2) - /// "2" - /// - pub external fn to_string(Int) -> String = +if erlang { + external fn do_to_string(Int) -> String = "erlang" "integer_to_binary" +} + +if javascript { + external fn do_to_string(Int) -> String = + "../gleam_stdlib.js" "int_to_string" +} - /// Prints a given int to a string using the base number provided. - /// - /// ## Examples - /// - /// > to_base_string(2, 2) - /// "10" - /// - /// > to_base_string(48, 16) - /// "30" - /// - /// > to_base_string(48, 36) - /// "1C" - /// - pub external fn to_base_string(Int, Int) -> String = +/// Prints a given int to a string using the base number provided. +/// +/// ## Examples +/// +/// > to_base_string(2, 2) +/// "10" +/// +/// > to_base_string(48, 16) +/// "30" +/// +/// > to_base_string(48, 36) +/// "1C" +/// +pub fn to_base_string(int, base) { + do_to_base_string(int, base) +} + +if erlang { + external fn do_to_base_string(Int, Int) -> String = "erlang" "integer_to_binary" +} + +if javascript { + external fn do_to_base_string(Int, Int) -> String = + "../gleam_stdlib.js" "int_to_base_string" +} - /// Takes an int and returns its value as a float - /// - /// ## Examples - /// - /// > to_float(5) - /// 5. - /// - /// > to_float(0) - /// 0. - /// - /// > to_float(-3) - /// -3. - /// - pub external fn to_float(a: Int) -> Float = +/// Takes an int and returns its value as a float +/// +/// ## Examples +/// +/// > to_float(5) +/// 5. +/// +/// > to_float(0) +/// 0. +/// +/// > to_float(-3) +/// -3. +/// +pub fn to_float(int) { + do_to_float(int) +} + +if erlang { + external fn do_to_float(a: Int) -> Float = "erlang" "float" +} - /// Restricts an Int between a lower and upper bound - /// - /// ## Examples - /// - /// ``` - /// > clamp(40, min: 50, max: 60) - /// 50 - /// ``` - /// - pub fn clamp(n: Int, min min_bound: Int, max max_bound: Int) -> Int { - n - |> min(max_bound) - |> max(min_bound) - } +if javascript { + external fn do_to_float(a: Int) -> Float = + "../gleam_stdlib.js" "identity" +} + +/// Restricts an Int between a lower and upper bound +/// +/// ## Examples +/// +/// ``` +/// > clamp(40, min: 50, max: 60) +/// 50 +/// ``` +/// +pub fn clamp(n: Int, min min_bound: Int, max max_bound: Int) -> Int { + n + |> min(max_bound) + |> max(min_bound) } /// Compares two ints, returning an order. diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index e69de29..960792b 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -0,0 +1,19 @@ +export function identity(x) { + return x +} + +export function parse_int(value) { + if (/^[-+]?(\d+)$/.test(value)) { + return { "type": "Ok", "0": Number(value) } + } else { + return { "type": "Error", "0": null } + } +} + +export function int_to_string(int) { + return int.toString() +} + +export function int_to_base_string(int, base) { + return int.toString(base) +}
\ No newline at end of file |