Skip to content

Commit bb1360d

Browse files
giacomocavalierilpil
authored andcommitted
various changes
1 parent 8a68cac commit bb1360d

File tree

13 files changed

+127
-134
lines changed

13 files changed

+127
-134
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fixed a bug that would result in `list.unique` having quadratic runtime.
6+
- Fixed the implementation of `list.key_set` to be tail recursive.
67

78
## v0.53.0 - 2025-01-23
89

src/gleam/bit_array.gleam

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn is_utf8_loop(bits: BitArray) -> Bool {
7878
fn is_utf8_loop(bits: BitArray) -> Bool {
7979
case to_string(bits) {
8080
Ok(_) -> True
81-
_ -> False
81+
Error(_) -> False
8282
}
8383
}
8484

@@ -111,7 +111,7 @@ fn unsafe_to_string(a: BitArray) -> String
111111
pub fn concat(bit_arrays: List(BitArray)) -> BitArray
112112

113113
/// Encodes a BitArray into a base 64 encoded string.
114-
///
114+
///
115115
/// If the bit array does not contain a whole number of bytes then it is padded
116116
/// with zero bits prior to being encoded.
117117
///

src/gleam/bytes_tree.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn prepend_tree(to second: BytesTree, prefix first: BytesTree) -> BytesTree
6868
pub fn append_tree(to first: BytesTree, suffix second: BytesTree) -> BytesTree {
6969
case second {
7070
Many(trees) -> Many([first, ..trees])
71-
_ -> Many([first, second])
71+
Text(_) | Bytes(_) -> Many([first, second])
7272
}
7373
}
7474

src/gleam/dict.gleam

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn from_list_loop(
9595
) -> Dict(k, v) {
9696
case list {
9797
[] -> initial
98-
[x, ..rest] -> from_list_loop(rest, insert(initial, x.0, x.1))
98+
[#(key, value), ..rest] -> from_list_loop(rest, insert(initial, key, value))
9999
}
100100
}
101101

@@ -210,21 +210,20 @@ fn do_map_values(f: fn(k, v) -> a, dict: Dict(k, v)) -> Dict(k, a) {
210210
///
211211
@external(erlang, "maps", "keys")
212212
pub fn keys(dict: Dict(k, v)) -> List(k) {
213-
let list_of_pairs = to_list(dict)
214-
do_keys_loop(list_of_pairs, [])
215-
}
216-
217-
fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) {
218-
case remaining {
219-
[] -> accumulator
220-
[item, ..rest] -> reverse_and_concat(rest, [item, ..accumulator])
221-
}
213+
do_keys_loop(to_list(dict), [])
222214
}
223215

224216
fn do_keys_loop(list: List(#(k, v)), acc: List(k)) -> List(k) {
225217
case list {
226218
[] -> reverse_and_concat(acc, [])
227-
[first, ..rest] -> do_keys_loop(rest, [first.0, ..acc])
219+
[#(key, _value), ..rest] -> do_keys_loop(rest, [key, ..acc])
220+
}
221+
}
222+
223+
fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) {
224+
case remaining {
225+
[] -> accumulator
226+
[first, ..rest] -> reverse_and_concat(rest, [first, ..accumulator])
228227
}
229228
}
230229

@@ -250,7 +249,7 @@ pub fn values(dict: Dict(k, v)) -> List(v) {
250249
fn do_values_loop(list: List(#(k, v)), acc: List(v)) -> List(v) {
251250
case list {
252251
[] -> reverse_and_concat(acc, [])
253-
[first, ..rest] -> do_values_loop(rest, [first.1, ..acc])
252+
[#(_key, value), ..rest] -> do_values_loop(rest, [value, ..acc])
254253
}
255254
}
256255

@@ -283,7 +282,7 @@ fn do_filter(f: fn(k, v) -> Bool, dict: Dict(k, v)) -> Dict(k, v) {
283282
let insert = fn(dict, k, v) {
284283
case f(k, v) {
285284
True -> insert(dict, k, v)
286-
_ -> dict
285+
False -> dict
287286
}
288287
}
289288

@@ -324,7 +323,7 @@ fn do_take_loop(
324323
let insert = fn(taken, key) {
325324
case get(dict, key) {
326325
Ok(value) -> insert(taken, key, value)
327-
_ -> taken
326+
Error(_) -> taken
328327
}
329328
}
330329
case desired_keys {
@@ -354,17 +353,17 @@ pub fn merge(into dict: Dict(k, v), from new_entries: Dict(k, v)) -> Dict(k, v)
354353
|> fold_inserts(dict)
355354
}
356355

357-
fn insert_pair(dict: Dict(k, v), pair: #(k, v)) -> Dict(k, v) {
358-
insert(dict, pair.0, pair.1)
359-
}
360-
361356
fn fold_inserts(new_entries: List(#(k, v)), dict: Dict(k, v)) -> Dict(k, v) {
362357
case new_entries {
363358
[] -> dict
364359
[first, ..rest] -> fold_inserts(rest, insert_pair(dict, first))
365360
}
366361
}
367362

363+
fn insert_pair(dict: Dict(k, v), pair: #(k, v)) -> Dict(k, v) {
364+
insert(dict, pair.0, pair.1)
365+
}
366+
368367
/// Creates a new dict from a given dict with all the same entries except for the
369368
/// one with a given key, if it exists.
370369
///
@@ -443,11 +442,10 @@ pub fn upsert(
443442
update key: k,
444443
with fun: fn(Option(v)) -> v,
445444
) -> Dict(k, v) {
446-
dict
447-
|> get(key)
448-
|> option.from_result
449-
|> fun
450-
|> insert(dict, key, _)
445+
case get(dict, key) {
446+
Ok(value) -> insert(dict, key, fun(option.Some(value)))
447+
Error(_) -> insert(dict, key, fun(option.None))
448+
}
451449
}
452450

453451
/// Combines all entries into a single value by calling a given function on each

src/gleam/dynamic/decode.gleam

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub fn run(data: Dynamic, decoder: Decoder(t)) -> Result(t, List(DecodeError)) {
349349
let #(maybe_invalid_data, errors) = decoder.function(data)
350350
case errors {
351351
[] -> Ok(maybe_invalid_data)
352-
_ -> Error(errors)
352+
[_, ..] -> Error(errors)
353353
}
354354
}
355355

@@ -784,7 +784,7 @@ pub fn dict(
784784
// don't need to run the decoders, instead return the existing acc.
785785
case a.1 {
786786
[] -> fold_dict(a, k, v, key.function, value.function)
787-
_ -> a
787+
[_, ..] -> a
788788
}
789789
})
790790
}
@@ -897,7 +897,7 @@ pub fn collapse_errors(decoder: Decoder(a), name: String) -> Decoder(a) {
897897
let #(data, errors) as layer = decoder.function(dynamic_data)
898898
case errors {
899899
[] -> layer
900-
_ -> #(data, decode_error(name, dynamic_data))
900+
[_, ..] -> #(data, decode_error(name, dynamic_data))
901901
}
902902
})
903903
}
@@ -913,7 +913,7 @@ pub fn then(decoder: Decoder(a), next: fn(a) -> Decoder(b)) -> Decoder(b) {
913913
let #(data, _) as layer = decoder.function(dynamic_data)
914914
case errors {
915915
[] -> layer
916-
_ -> #(data, errors)
916+
[_, ..] -> #(data, errors)
917917
}
918918
})
919919
}
@@ -944,7 +944,7 @@ pub fn one_of(
944944
let #(_, errors) as layer = first.function(dynamic_data)
945945
case errors {
946946
[] -> layer
947-
_ -> run_decoders(dynamic_data, layer, alternatives)
947+
[_, ..] -> run_decoders(dynamic_data, layer, alternatives)
948948
}
949949
})
950950
}
@@ -961,7 +961,7 @@ fn run_decoders(
961961
let #(_, errors) as layer = decoder.function(data)
962962
case errors {
963963
[] -> layer
964-
_ -> run_decoders(data, failure, decoders)
964+
[_, ..] -> run_decoders(data, failure, decoders)
965965
}
966966
}
967967
}

src/gleam/float.gleam

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//// Functions for working with floats.
2-
////
2+
////
33
//// ## Float representation
4-
////
4+
////
55
//// Floats are represented as 64 bit floating point numbers on both the Erlang
66
//// and JavaScript runtimes. The floating point behaviour is native to their
77
//// respective runtimes, so their exact behaviour will be slightly different on
8-
//// the two runtimes.
9-
////
8+
//// the two runtimes.
9+
////
1010
//// ### Infinity and NaN
11-
////
11+
////
1212
//// Under the JavaScript runtime, exceeding the maximum (or minimum)
1313
//// representable value for a floating point value will result in Infinity (or
1414
//// -Infinity). Should you try to divide two infinities you will get NaN as a
15-
//// result.
16-
////
15+
//// result.
16+
////
1717
//// When running on BEAM, exceeding the maximum (or minimum) representable
1818
//// value for a floating point value will raise an error.
1919
////
@@ -240,7 +240,7 @@ pub fn floor(x: Float) -> Float
240240
pub fn round(x: Float) -> Int {
241241
case x >=. 0.0 {
242242
True -> js_round(x)
243-
_ -> 0 - js_round(negate(x))
243+
False -> 0 - js_round(negate(x))
244244
}
245245
}
246246

@@ -311,7 +311,7 @@ fn do_to_float(a: Int) -> Float
311311
pub fn absolute_value(x: Float) -> Float {
312312
case x >=. 0.0 {
313313
True -> x
314-
_ -> 0.0 -. x
314+
False -> 0.0 -. x
315315
}
316316
}
317317

@@ -409,7 +409,7 @@ pub fn sum(numbers: List(Float)) -> Float {
409409

410410
fn sum_loop(numbers: List(Float), initial: Float) -> Float {
411411
case numbers {
412-
[x, ..rest] -> sum_loop(rest, x +. initial)
412+
[first, ..rest] -> sum_loop(rest, first +. initial)
413413
[] -> initial
414414
}
415415
}
@@ -424,15 +424,12 @@ fn sum_loop(numbers: List(Float), initial: Float) -> Float {
424424
/// ```
425425
///
426426
pub fn product(numbers: List(Float)) -> Float {
427-
case numbers {
428-
[] -> 1.0
429-
_ -> product_loop(numbers, 1.0)
430-
}
427+
product_loop(numbers, 1.0)
431428
}
432429

433430
fn product_loop(numbers: List(Float), initial: Float) -> Float {
434431
case numbers {
435-
[x, ..rest] -> product_loop(rest, x *. initial)
432+
[first, ..rest] -> product_loop(rest, first *. initial)
436433
[] -> initial
437434
}
438435
}

src/gleam/int.gleam

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ pub fn absolute_value(x: Int) -> Int {
6565
/// ```
6666
///
6767
pub fn power(base: Int, of exponent: Float) -> Result(Float, Nil) {
68-
base
69-
|> to_float()
68+
to_float(base)
7069
|> float.power(exponent)
7170
}
7271

@@ -85,8 +84,7 @@ pub fn power(base: Int, of exponent: Float) -> Result(Float, Nil) {
8584
/// ```
8685
///
8786
pub fn square_root(x: Int) -> Result(Float, Nil) {
88-
x
89-
|> to_float()
87+
to_float(x)
9088
|> float.square_root()
9189
}
9290

@@ -420,7 +418,7 @@ pub fn sum(numbers: List(Int)) -> Int {
420418

421419
fn sum_loop(numbers: List(Int), initial: Int) -> Int {
422420
case numbers {
423-
[x, ..rest] -> sum_loop(rest, x + initial)
421+
[first, ..rest] -> sum_loop(rest, first + initial)
424422
[] -> initial
425423
}
426424
}
@@ -435,15 +433,12 @@ fn sum_loop(numbers: List(Int), initial: Int) -> Int {
435433
/// ```
436434
///
437435
pub fn product(numbers: List(Int)) -> Int {
438-
case numbers {
439-
[] -> 1
440-
_ -> product_loop(numbers, 1)
441-
}
436+
product_loop(numbers, 1)
442437
}
443438

444439
fn product_loop(numbers: List(Int), initial: Int) -> Int {
445440
case numbers {
446-
[x, ..rest] -> product_loop(rest, x * initial)
441+
[first, ..rest] -> product_loop(rest, first * initial)
447442
[] -> initial
448443
}
449444
}
@@ -535,8 +530,8 @@ fn undigits_loop(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) {
535530
///
536531
pub fn random(max: Int) -> Int {
537532
{ float.random() *. to_float(max) }
538-
|> float.floor()
539-
|> float.round()
533+
|> float.floor
534+
|> float.round
540535
}
541536

542537
/// Performs a truncated integer division.

0 commit comments

Comments
 (0)