Skip to content

Commit

Permalink
Replace do_* with direct externals
Browse files Browse the repository at this point in the history
  • Loading branch information
GearsDatapacks authored and lpil committed Nov 28, 2024
1 parent b8785ed commit 4411f58
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 266 deletions.
12 changes: 4 additions & 8 deletions src/gleam/bit_array.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,17 @@ fn is_utf8_loop(bits: BitArray) -> Bool {
///
/// Returns an error if the bit array is invalid UTF-8 data.
///
pub fn to_string(bits: BitArray) -> Result(String, Nil) {
do_to_string(bits)
}

@external(erlang, "gleam_stdlib", "identity")
fn unsafe_to_string(a: BitArray) -> String

@external(javascript, "../gleam_stdlib.mjs", "bit_array_to_string")
fn do_to_string(bits: BitArray) -> Result(String, Nil) {
pub fn to_string(bits: BitArray) -> Result(String, Nil) {
case is_utf8(bits) {
True -> Ok(unsafe_to_string(bits))
False -> Error(Nil)
}
}

@external(erlang, "gleam_stdlib", "identity")
fn unsafe_to_string(a: BitArray) -> String

/// Creates a new bit array by joining multiple binaries.
///
/// ## Examples
Expand Down
30 changes: 5 additions & 25 deletions src/gleam/dict.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,9 @@ fn do_has_key(key: k, dict: Dict(k, v)) -> Bool {

/// Creates a fresh dict that contains no values.
///
pub fn new() -> Dict(k, v) {
do_new()
}

@external(erlang, "maps", "new")
@external(javascript, "../gleam_stdlib.mjs", "new_map")
fn do_new() -> Dict(k, v)
pub fn new() -> Dict(k, v)

/// Fetches a value from a dict for a given key.
///
Expand All @@ -149,13 +145,9 @@ fn do_new() -> Dict(k, v)
/// // -> Error(Nil)
/// ```
///
pub fn get(from: Dict(k, v), get: k) -> Result(v, Nil) {
do_get(from, get)
}

@external(erlang, "gleam_stdlib", "map_get")
@external(javascript, "../gleam_stdlib.mjs", "map_get")
fn do_get(dict: Dict(k, v), key: k) -> Result(v, Nil)
pub fn get(from: Dict(k, v), get: k) -> Result(v, Nil)

/// Inserts a value into the dict with the given key.
///
Expand Down Expand Up @@ -216,12 +208,8 @@ fn do_map_values(f: fn(k, v) -> a, dict: Dict(k, v)) -> Dict(k, a) {
/// // -> ["a", "b"]
/// ```
///
pub fn keys(dict: Dict(k, v)) -> List(k) {
do_keys(dict)
}

@external(erlang, "maps", "keys")
fn do_keys(dict: Dict(k, v)) -> List(k) {
pub fn keys(dict: Dict(k, v)) -> List(k) {
let list_of_pairs = to_list(dict)
do_keys_loop(list_of_pairs, [])
}
Expand Down Expand Up @@ -253,12 +241,8 @@ fn do_keys_loop(list: List(#(k, v)), acc: List(k)) -> List(k) {
/// // -> [0, 1]
/// ```
///
pub fn values(dict: Dict(k, v)) -> List(v) {
do_values(dict)
}

@external(erlang, "maps", "values")
fn do_values(dict: Dict(k, v)) -> List(v) {
pub fn values(dict: Dict(k, v)) -> List(v) {
let list_of_pairs = to_list(dict)
do_values_loop(list_of_pairs, [])
}
Expand Down Expand Up @@ -363,12 +347,8 @@ fn do_take_loop(
/// // -> from_list([#("a", 0), #("b", 2), #("c", 3)])
/// ```
///
pub fn merge(into dict: Dict(k, v), from new_entries: Dict(k, v)) -> Dict(k, v) {
do_merge(dict, new_entries)
}

@external(erlang, "maps", "merge")
fn do_merge(dict: Dict(k, v), new_entries: Dict(k, v)) -> Dict(k, v) {
pub fn merge(into dict: Dict(k, v), from new_entries: Dict(k, v)) -> Dict(k, v) {
new_entries
|> to_list
|> fold_inserts(dict)
Expand Down
6 changes: 1 addition & 5 deletions src/gleam/dynamic.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ fn put_expected(error: DecodeError, expected: String) -> DecodeError {
/// // -> "String"
/// ```
///
pub fn classify(data: Dynamic) -> String {
do_classify(data)
}

@external(erlang, "gleam_stdlib", "classify_dynamic")
@external(javascript, "../gleam_stdlib.mjs", "classify_dynamic")
fn do_classify(a: Dynamic) -> String
pub fn classify(data: Dynamic) -> String

/// Checks to see whether a `Dynamic` value is an int, and returns that int if it
/// is.
Expand Down
36 changes: 6 additions & 30 deletions src/gleam/float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ import gleam/order.{type Order}
/// // -> Error(Nil)
/// ```
///
pub fn parse(string: String) -> Result(Float, Nil) {
do_parse(string)
}

@external(erlang, "gleam_stdlib", "parse_float")
@external(javascript, "../gleam_stdlib.mjs", "parse_float")
fn do_parse(a: String) -> Result(Float, Nil)
pub fn parse(string: String) -> Result(Float, Nil)

/// Returns the string representation of the provided `Float`.
///
Expand All @@ -46,13 +42,9 @@ fn do_parse(a: String) -> Result(Float, Nil)
/// // -> "2.3"
/// ```
///
pub fn to_string(x: Float) -> String {
do_to_string(x)
}

@external(erlang, "gleam_stdlib", "float_to_string")
@external(javascript, "../gleam_stdlib.mjs", "float_to_string")
fn do_to_string(a: Float) -> String
pub fn to_string(x: Float) -> String

/// Restricts a `Float` between a lower and upper bound.
///
Expand Down Expand Up @@ -196,13 +188,9 @@ pub fn max(a: Float, b: Float) -> Float {
/// // -> 3.0
/// ```
///
pub fn ceiling(x: Float) -> Float {
do_ceiling(x)
}

@external(erlang, "math", "ceil")
@external(javascript, "../gleam_stdlib.mjs", "ceiling")
fn do_ceiling(a: Float) -> Float
pub fn ceiling(x: Float) -> Float

/// Rounds the value to the next lowest whole number as a `Float`.
///
Expand All @@ -213,13 +201,9 @@ fn do_ceiling(a: Float) -> Float
/// // -> 2.0
/// ```
///
pub fn floor(x: Float) -> Float {
do_floor(x)
}

@external(erlang, "math", "floor")
@external(javascript, "../gleam_stdlib.mjs", "floor")
fn do_floor(a: Float) -> Float
pub fn floor(x: Float) -> Float

/// Rounds the value to the nearest whole number as an `Int`.
///
Expand All @@ -235,12 +219,8 @@ fn do_floor(a: Float) -> Float
/// // -> 3
/// ```
///
pub fn round(x: Float) -> Int {
do_round(x)
}

@external(erlang, "erlang", "round")
fn do_round(x: Float) -> Int {
pub fn round(x: Float) -> Int {
case x >=. 0.0 {
True -> js_round(x)
_ -> 0 - js_round(negate(x))
Expand All @@ -259,13 +239,9 @@ fn js_round(a: Float) -> Int
/// // -> 2
/// ```
///
pub fn truncate(x: Float) -> Int {
do_truncate(x)
}

@external(erlang, "erlang", "trunc")
@external(javascript, "../gleam_stdlib.mjs", "truncate")
fn do_truncate(a: Float) -> Int
pub fn truncate(x: Float) -> Int

/// Converts the value to a given precision as a `Float`.
/// The precision is the number of allowed decimal places.
Expand Down
18 changes: 3 additions & 15 deletions src/gleam/int.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ pub fn square_root(x: Int) -> Result(Float, Nil) {
/// // -> Error(Nil)
/// ```
///
pub fn parse(string: String) -> Result(Int, Nil) {
do_parse(string)
}

@external(erlang, "gleam_stdlib", "parse_int")
@external(javascript, "../gleam_stdlib.mjs", "parse_int")
fn do_parse(a: String) -> Result(Int, Nil)
pub fn parse(string: String) -> Result(Int, Nil)

/// Parses a given string as an int in a given base if possible.
/// Supports only bases 2 to 36, for values outside of which this function returns an `Error(Nil)`.
Expand Down Expand Up @@ -162,13 +158,9 @@ fn do_base_parse(a: String, b: Int) -> Result(Int, Nil)
/// // -> "2"
/// ```
///
pub fn to_string(x: Int) {
do_to_string(x)
}

@external(erlang, "erlang", "integer_to_binary")
@external(javascript, "../gleam_stdlib.mjs", "to_string")
fn do_to_string(a: Int) -> String
pub fn to_string(x: Int) -> String

/// Prints a given int to a string using the base number provided.
/// Supports only bases 2 to 36, for values outside of which this function returns an `Error(Nil)`.
Expand Down Expand Up @@ -283,13 +275,9 @@ pub fn to_base36(x: Int) -> String {
/// // -> -3.0
/// ```
///
pub fn to_float(x: Int) -> Float {
do_to_float(x)
}

@external(erlang, "erlang", "float")
@external(javascript, "../gleam_stdlib.mjs", "identity")
fn do_to_float(a: Int) -> Float
pub fn to_float(x: Int) -> Float

/// Restricts an int between a lower and upper bound.
///
Expand Down
24 changes: 4 additions & 20 deletions src/gleam/io.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ import gleam/string
/// // Hi mum
/// ```
///
pub fn print(string: String) -> Nil {
do_print(string)
}

@external(erlang, "gleam_stdlib", "print")
@external(javascript, "../gleam_stdlib.mjs", "print")
fn do_print(string string: String) -> Nil
pub fn print(string: String) -> Nil

/// Writes a string to standard error (stderr).
///
Expand All @@ -32,13 +28,9 @@ fn do_print(string string: String) -> Nil
/// // Hi pop
/// ```
///
pub fn print_error(string: String) -> Nil {
do_print_error(string)
}

@external(erlang, "gleam_stdlib", "print_error")
@external(javascript, "../gleam_stdlib.mjs", "print_error")
fn do_print_error(string string: String) -> Nil
pub fn print_error(string: String) -> Nil

/// Writes a string to standard output (stdout), appending a newline to the end.
///
Expand All @@ -50,13 +42,9 @@ fn do_print_error(string string: String) -> Nil
/// // Hi mum
/// ```
///
pub fn println(string: String) -> Nil {
do_println(string)
}

@external(erlang, "gleam_stdlib", "println")
@external(javascript, "../gleam_stdlib.mjs", "console_log")
fn do_println(string string: String) -> Nil
pub fn println(string: String) -> Nil

/// Writes a string to standard error (stderr), appending a newline to the end.
///
Expand All @@ -68,13 +56,9 @@ fn do_println(string string: String) -> Nil
/// // Hi pop
/// ```
///
pub fn println_error(string: String) -> Nil {
do_println_error(string)
}

@external(erlang, "gleam_stdlib", "println_error")
@external(javascript, "../gleam_stdlib.mjs", "console_error")
fn do_println_error(string string: String) -> Nil
pub fn println_error(string: String) -> Nil

/// Writes a value to standard error (stderr) yielding Gleam syntax.
///
Expand Down
29 changes: 5 additions & 24 deletions src/gleam/regex.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,11 @@ pub type Options {
/// ```
///
@deprecated("Please use the gleam_regexp package instead")
pub fn compile(
pattern: String,
with options: Options,
) -> Result(Regex, CompileError) {
do_compile(pattern, options)
}

@external(erlang, "gleam_stdlib", "compile_regex")
@external(javascript, "../gleam_stdlib.mjs", "compile_regex")
fn do_compile(
pub fn compile(
pattern: String,
with with: Options,
with options: Options,
) -> Result(Regex, CompileError)

/// Creates a new `Regex`.
Expand Down Expand Up @@ -115,13 +108,9 @@ pub fn from_string(pattern: String) -> Result(Regex, CompileError) {
/// ```
///
@deprecated("Please use the gleam_regexp package instead")
pub fn check(with regex: Regex, content string: String) -> Bool {
do_check(regex, string)
}

@external(erlang, "gleam_stdlib", "regex_check")
@external(javascript, "../gleam_stdlib.mjs", "regex_check")
fn do_check(regex: Regex, string: String) -> Bool
pub fn check(with regex: Regex, content string: String) -> Bool

/// Splits a string.
///
Expand All @@ -134,13 +123,9 @@ fn do_check(regex: Regex, string: String) -> Bool
/// ```
///
@deprecated("Please use the gleam_regexp package instead")
pub fn split(with regex: Regex, content string: String) -> List(String) {
do_split(regex, string)
}

@external(erlang, "gleam_stdlib", "regex_split")
@external(javascript, "../gleam_stdlib.mjs", "regex_split")
fn do_split(regex: Regex, string: String) -> List(String)
pub fn split(with regex: Regex, content string: String) -> List(String)

/// Collects all matches of the regular expression.
///
Expand Down Expand Up @@ -195,13 +180,9 @@ fn do_split(regex: Regex, string: String) -> List(String)
/// ```
///
@deprecated("Please use the gleam_regexp package instead")
pub fn scan(with regex: Regex, content string: String) -> List(Match) {
do_scan(regex, string)
}

@external(erlang, "gleam_stdlib", "regex_scan")
@external(javascript, "../gleam_stdlib.mjs", "regex_scan")
fn do_scan(regex: Regex, string: String) -> List(Match)
pub fn scan(with regex: Regex, content string: String) -> List(Match)

/// Creates a new `String` by replacing all substrings that match the regular
/// expression.
Expand Down
Loading

0 comments on commit 4411f58

Please sign in to comment.