Skip to content

Commit

Permalink
fix-721
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Nov 6, 2024
1 parent 226a2ee commit 959aa17
Show file tree
Hide file tree
Showing 10 changed files with 723 additions and 722 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ indent_size = 2
[*.erl]
indent_size = 4

[*.{gleam, erl, mjs, js, ts}]
[*.{erl, mjs, js, ts}]
max_line_length = 80

15 changes: 7 additions & 8 deletions src/gleam/bit_array.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ pub fn slice(
/// Tests to see whether a bit array is valid UTF-8.
///
pub fn is_utf8(bits: BitArray) -> Bool {
do_is_utf8(bits)
is_utf8_loop(bits)
}

@target(erlang)
fn do_is_utf8(bits: BitArray) -> Bool {
fn is_utf8_loop(bits: BitArray) -> Bool {
case bits {
<<>> -> True
<<_:utf8, rest:bytes>> -> do_is_utf8(rest)
<<_:utf8, rest:bytes>> -> is_utf8_loop(rest)
_ -> False
}
}

@target(javascript)
fn do_is_utf8(bits: BitArray) -> Bool {
fn is_utf8_loop(bits: BitArray) -> Bool {
case to_string(bits) {
Ok(_) -> True
_ -> False
Expand Down Expand Up @@ -158,11 +158,11 @@ pub fn base16_decode(input: String) -> Result(BitArray, Nil)
/// ```
///
pub fn inspect(input: BitArray) -> String {
do_inspect(input, "<<") <> ">>"
inspect_loop(input, "<<") <> ">>"
}

@external(javascript, "../gleam_stdlib.mjs", "bit_array_inspect")
fn do_inspect(input: BitArray, accumulator: String) -> String {
fn inspect_loop(input: BitArray, accumulator: String) -> String {
case input {
<<>> -> accumulator

Expand All @@ -181,8 +181,7 @@ fn do_inspect(input: BitArray, accumulator: String) -> String {
}

let accumulator = accumulator <> int.to_string(x) <> suffix

do_inspect(rest, accumulator)
inspect_loop(rest, accumulator)
}

_ -> accumulator
Expand Down
44 changes: 23 additions & 21 deletions src/gleam/dict.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn keys(dict: Dict(k, v)) -> List(k) {
@external(erlang, "maps", "keys")
fn do_keys(dict: Dict(k, v)) -> List(k) {
let list_of_pairs = to_list(dict)
do_keys_acc(list_of_pairs, [])
do_keys_loop(list_of_pairs, [])
}

fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) {
Expand All @@ -233,10 +233,10 @@ fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) {
}
}

fn do_keys_acc(list: List(#(k, v)), acc: List(k)) -> List(k) {
fn do_keys_loop(list: List(#(k, v)), acc: List(k)) -> List(k) {
case list {
[] -> reverse_and_concat(acc, [])
[first, ..rest] -> do_keys_acc(rest, [first.0, ..acc])
[first, ..rest] -> do_keys_loop(rest, [first.0, ..acc])
}
}

Expand All @@ -260,13 +260,13 @@ pub fn values(dict: Dict(k, v)) -> List(v) {
@external(erlang, "maps", "values")
fn do_values(dict: Dict(k, v)) -> List(v) {
let list_of_pairs = to_list(dict)
do_values_acc(list_of_pairs, [])
do_values_loop(list_of_pairs, [])
}

fn do_values_acc(list: List(#(k, v)), acc: List(v)) -> List(v) {
fn do_values_loop(list: List(#(k, v)), acc: List(v)) -> List(v) {
case list {
[] -> reverse_and_concat(acc, [])
[first, ..rest] -> do_values_acc(rest, [first.1, ..acc])
[first, ..rest] -> do_values_loop(rest, [first.1, ..acc])
}
}

Expand Down Expand Up @@ -302,8 +302,8 @@ fn do_filter(f: fn(k, v) -> Bool, dict: Dict(k, v)) -> Dict(k, v) {
_ -> dict
}
}
dict
|> fold(from: new(), with: insert)

fold(dict, from: new(), with: insert)
}

/// Creates a new dict from a given dict, only including any entries for which the
Expand All @@ -329,10 +329,10 @@ pub fn take(from dict: Dict(k, v), keeping desired_keys: List(k)) -> Dict(k, v)

@external(erlang, "maps", "with")
fn do_take(desired_keys: List(k), dict: Dict(k, v)) -> Dict(k, v) {
insert_taken(dict, desired_keys, new())
do_take_loop(dict, desired_keys, new())
}

fn insert_taken(
fn do_take_loop(
dict: Dict(k, v),
desired_keys: List(k),
acc: Dict(k, v),
Expand All @@ -345,7 +345,7 @@ fn insert_taken(
}
case desired_keys {
[] -> acc
[first, ..rest] -> insert_taken(dict, rest, insert(acc, first))
[first, ..rest] -> do_take_loop(dict, rest, insert(acc, first))
}
}

Expand Down Expand Up @@ -470,13 +470,6 @@ pub fn upsert(
|> insert(dict, key, _)
}

fn do_fold(list: List(#(k, v)), initial: acc, fun: fn(acc, k, v) -> acc) -> acc {
case list {
[] -> initial
[#(k, v), ..rest] -> do_fold(rest, fun(initial, k, v), fun)
}
}

/// Combines all entries into a single value by calling a given function on each
/// one.
///
Expand Down Expand Up @@ -507,9 +500,18 @@ pub fn fold(
from initial: acc,
with fun: fn(acc, k, v) -> acc,
) -> acc {
dict
|> to_list
|> do_fold(initial, fun)
fold_loop(to_list(dict), initial, fun)
}

fn fold_loop(
list: List(#(k, v)),
initial: acc,
fun: fn(acc, k, v) -> acc,
) -> acc {
case list {
[] -> initial
[#(k, v), ..rest] -> fold_loop(rest, fun(initial, k, v), fun)
}
}

/// Calls a function for each key and value in a dict, discarding the return
Expand Down
13 changes: 6 additions & 7 deletions src/gleam/float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,13 @@ pub fn negate(x: Float) -> Float {
/// ```
///
pub fn sum(numbers: List(Float)) -> Float {
numbers
|> do_sum(0.0)
sum_loop(numbers, 0.0)
}

fn do_sum(numbers: List(Float), initial: Float) -> Float {
fn sum_loop(numbers: List(Float), initial: Float) -> Float {
case numbers {
[x, ..rest] -> sum_loop(rest, x +. initial)
[] -> initial
[x, ..rest] -> do_sum(rest, x +. initial)
}
}

Expand All @@ -426,14 +425,14 @@ fn do_sum(numbers: List(Float), initial: Float) -> Float {
pub fn product(numbers: List(Float)) -> Float {
case numbers {
[] -> 1.0
_ -> do_product(numbers, 1.0)
_ -> product_loop(numbers, 1.0)
}
}

fn do_product(numbers: List(Float), initial: Float) -> Float {
fn product_loop(numbers: List(Float), initial: Float) -> Float {
case numbers {
[x, ..rest] -> product_loop(rest, x *. initial)
[] -> initial
[x, ..rest] -> do_product(rest, x *. initial)
}
}

Expand Down
25 changes: 12 additions & 13 deletions src/gleam/int.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,13 @@ pub fn negate(x: Int) -> Int {
/// ```
///
pub fn sum(numbers: List(Int)) -> Int {
numbers
|> do_sum(0)
sum_loop(numbers, 0)
}

fn do_sum(numbers: List(Int), initial: Int) -> Int {
fn sum_loop(numbers: List(Int), initial: Int) -> Int {
case numbers {
[x, ..rest] -> sum_loop(rest, x + initial)
[] -> initial
[x, ..rest] -> do_sum(rest, x + initial)
}
}

Expand All @@ -450,14 +449,14 @@ fn do_sum(numbers: List(Int), initial: Int) -> Int {
pub fn product(numbers: List(Int)) -> Int {
case numbers {
[] -> 1
_ -> do_product(numbers, 1)
_ -> product_loop(numbers, 1)
}
}

fn do_product(numbers: List(Int), initial: Int) -> Int {
fn product_loop(numbers: List(Int), initial: Int) -> Int {
case numbers {
[x, ..rest] -> product_loop(rest, x * initial)
[] -> initial
[x, ..rest] -> do_product(rest, x * initial)
}
}

Expand All @@ -479,14 +478,14 @@ fn do_product(numbers: List(Int), initial: Int) -> Int {
pub fn digits(x: Int, base: Int) -> Result(List(Int), Nil) {
case base < 2 {
True -> Error(Nil)
False -> Ok(do_digits(x, base, []))
False -> Ok(digits_loop(x, base, []))
}
}

fn do_digits(x: Int, base: Int, acc: List(Int)) -> List(Int) {
fn digits_loop(x: Int, base: Int, acc: List(Int)) -> List(Int) {
case absolute_value(x) < base {
True -> [x, ..acc]
False -> do_digits(x / base, base, [x % base, ..acc])
False -> digits_loop(x / base, base, [x % base, ..acc])
}
}

Expand All @@ -513,15 +512,15 @@ fn do_digits(x: Int, base: Int, acc: List(Int)) -> List(Int) {
pub fn undigits(numbers: List(Int), base: Int) -> Result(Int, Nil) {
case base < 2 {
True -> Error(Nil)
False -> do_undigits(numbers, base, 0)
False -> undigits_loop(numbers, base, 0)
}
}

fn do_undigits(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) {
fn undigits_loop(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) {
case numbers {
[] -> Ok(acc)
[digit, ..] if digit >= base -> Error(Nil)
[digit, ..rest] -> do_undigits(rest, base, acc * base + digit)
[digit, ..rest] -> undigits_loop(rest, base, acc * base + digit)
}
}

Expand Down
Loading

0 comments on commit 959aa17

Please sign in to comment.