From bb1360d47d7069db87094244a3df51c2871e16e8 Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Mon, 27 Jan 2025 18:21:23 +0100 Subject: [PATCH] various changes --- CHANGELOG.md | 1 + src/gleam/bit_array.gleam | 4 +- src/gleam/bytes_tree.gleam | 2 +- src/gleam/dict.gleam | 44 ++++++----- src/gleam/dynamic/decode.gleam | 12 +-- src/gleam/float.gleam | 27 +++---- src/gleam/int.gleam | 19 ++--- src/gleam/list.gleam | 129 +++++++++++++++++---------------- src/gleam/option.gleam | 8 +- src/gleam/string.gleam | 6 +- src/gleam/uri.gleam | 4 +- test/gleam/list_test.gleam | 2 +- test/gleam/should.gleam | 3 +- 13 files changed, 127 insertions(+), 134 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fb78ec5..4982cd24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug that would result in `list.unique` having quadratic runtime. +- Fixed the implementation of `list.key_set` to be tail recursive. ## v0.53.0 - 2025-01-23 diff --git a/src/gleam/bit_array.gleam b/src/gleam/bit_array.gleam index 173fab89..df75be59 100644 --- a/src/gleam/bit_array.gleam +++ b/src/gleam/bit_array.gleam @@ -78,7 +78,7 @@ fn is_utf8_loop(bits: BitArray) -> Bool { fn is_utf8_loop(bits: BitArray) -> Bool { case to_string(bits) { Ok(_) -> True - _ -> False + Error(_) -> False } } @@ -111,7 +111,7 @@ fn unsafe_to_string(a: BitArray) -> String pub fn concat(bit_arrays: List(BitArray)) -> BitArray /// Encodes a BitArray into a base 64 encoded string. -/// +/// /// If the bit array does not contain a whole number of bytes then it is padded /// with zero bits prior to being encoded. /// diff --git a/src/gleam/bytes_tree.gleam b/src/gleam/bytes_tree.gleam index f3ef9756..0418644b 100644 --- a/src/gleam/bytes_tree.gleam +++ b/src/gleam/bytes_tree.gleam @@ -68,7 +68,7 @@ pub fn prepend_tree(to second: BytesTree, prefix first: BytesTree) -> BytesTree pub fn append_tree(to first: BytesTree, suffix second: BytesTree) -> BytesTree { case second { Many(trees) -> Many([first, ..trees]) - _ -> Many([first, second]) + Text(_) | Bytes(_) -> Many([first, second]) } } diff --git a/src/gleam/dict.gleam b/src/gleam/dict.gleam index 112bf15c..ace76e4e 100644 --- a/src/gleam/dict.gleam +++ b/src/gleam/dict.gleam @@ -95,7 +95,7 @@ fn from_list_loop( ) -> Dict(k, v) { case list { [] -> initial - [x, ..rest] -> from_list_loop(rest, insert(initial, x.0, x.1)) + [#(key, value), ..rest] -> from_list_loop(rest, insert(initial, key, value)) } } @@ -210,21 +210,20 @@ fn do_map_values(f: fn(k, v) -> a, dict: Dict(k, v)) -> Dict(k, a) { /// @external(erlang, "maps", "keys") pub fn keys(dict: Dict(k, v)) -> List(k) { - let list_of_pairs = to_list(dict) - do_keys_loop(list_of_pairs, []) -} - -fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) { - case remaining { - [] -> accumulator - [item, ..rest] -> reverse_and_concat(rest, [item, ..accumulator]) - } + do_keys_loop(to_list(dict), []) } fn do_keys_loop(list: List(#(k, v)), acc: List(k)) -> List(k) { case list { [] -> reverse_and_concat(acc, []) - [first, ..rest] -> do_keys_loop(rest, [first.0, ..acc]) + [#(key, _value), ..rest] -> do_keys_loop(rest, [key, ..acc]) + } +} + +fn reverse_and_concat(remaining: List(a), accumulator: List(a)) -> List(a) { + case remaining { + [] -> accumulator + [first, ..rest] -> reverse_and_concat(rest, [first, ..accumulator]) } } @@ -250,7 +249,7 @@ pub fn values(dict: Dict(k, 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_loop(rest, [first.1, ..acc]) + [#(_key, value), ..rest] -> do_values_loop(rest, [value, ..acc]) } } @@ -283,7 +282,7 @@ fn do_filter(f: fn(k, v) -> Bool, dict: Dict(k, v)) -> Dict(k, v) { let insert = fn(dict, k, v) { case f(k, v) { True -> insert(dict, k, v) - _ -> dict + False -> dict } } @@ -324,7 +323,7 @@ fn do_take_loop( let insert = fn(taken, key) { case get(dict, key) { Ok(value) -> insert(taken, key, value) - _ -> taken + Error(_) -> taken } } case desired_keys { @@ -354,10 +353,6 @@ pub fn merge(into dict: Dict(k, v), from new_entries: Dict(k, v)) -> Dict(k, v) |> fold_inserts(dict) } -fn insert_pair(dict: Dict(k, v), pair: #(k, v)) -> Dict(k, v) { - insert(dict, pair.0, pair.1) -} - fn fold_inserts(new_entries: List(#(k, v)), dict: Dict(k, v)) -> Dict(k, v) { case new_entries { [] -> dict @@ -365,6 +360,10 @@ fn fold_inserts(new_entries: List(#(k, v)), dict: Dict(k, v)) -> Dict(k, v) { } } +fn insert_pair(dict: Dict(k, v), pair: #(k, v)) -> Dict(k, v) { + insert(dict, pair.0, pair.1) +} + /// Creates a new dict from a given dict with all the same entries except for the /// one with a given key, if it exists. /// @@ -443,11 +442,10 @@ pub fn upsert( update key: k, with fun: fn(Option(v)) -> v, ) -> Dict(k, v) { - dict - |> get(key) - |> option.from_result - |> fun - |> insert(dict, key, _) + case get(dict, key) { + Ok(value) -> insert(dict, key, fun(option.Some(value))) + Error(_) -> insert(dict, key, fun(option.None)) + } } /// Combines all entries into a single value by calling a given function on each diff --git a/src/gleam/dynamic/decode.gleam b/src/gleam/dynamic/decode.gleam index 6bb9cadf..b75bb195 100644 --- a/src/gleam/dynamic/decode.gleam +++ b/src/gleam/dynamic/decode.gleam @@ -349,7 +349,7 @@ pub fn run(data: Dynamic, decoder: Decoder(t)) -> Result(t, List(DecodeError)) { let #(maybe_invalid_data, errors) = decoder.function(data) case errors { [] -> Ok(maybe_invalid_data) - _ -> Error(errors) + [_, ..] -> Error(errors) } } @@ -784,7 +784,7 @@ pub fn dict( // don't need to run the decoders, instead return the existing acc. case a.1 { [] -> fold_dict(a, k, v, key.function, value.function) - _ -> a + [_, ..] -> a } }) } @@ -897,7 +897,7 @@ pub fn collapse_errors(decoder: Decoder(a), name: String) -> Decoder(a) { let #(data, errors) as layer = decoder.function(dynamic_data) case errors { [] -> layer - _ -> #(data, decode_error(name, dynamic_data)) + [_, ..] -> #(data, decode_error(name, dynamic_data)) } }) } @@ -913,7 +913,7 @@ pub fn then(decoder: Decoder(a), next: fn(a) -> Decoder(b)) -> Decoder(b) { let #(data, _) as layer = decoder.function(dynamic_data) case errors { [] -> layer - _ -> #(data, errors) + [_, ..] -> #(data, errors) } }) } @@ -944,7 +944,7 @@ pub fn one_of( let #(_, errors) as layer = first.function(dynamic_data) case errors { [] -> layer - _ -> run_decoders(dynamic_data, layer, alternatives) + [_, ..] -> run_decoders(dynamic_data, layer, alternatives) } }) } @@ -961,7 +961,7 @@ fn run_decoders( let #(_, errors) as layer = decoder.function(data) case errors { [] -> layer - _ -> run_decoders(data, failure, decoders) + [_, ..] -> run_decoders(data, failure, decoders) } } } diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 8d427387..83bfa6e6 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -1,19 +1,19 @@ //// Functions for working with floats. -//// +//// //// ## Float representation -//// +//// //// Floats are represented as 64 bit floating point numbers on both the Erlang //// and JavaScript runtimes. The floating point behaviour is native to their //// respective runtimes, so their exact behaviour will be slightly different on -//// the two runtimes. -//// +//// the two runtimes. +//// //// ### Infinity and NaN -//// +//// //// Under the JavaScript runtime, exceeding the maximum (or minimum) //// representable value for a floating point value will result in Infinity (or //// -Infinity). Should you try to divide two infinities you will get NaN as a -//// result. -//// +//// result. +//// //// When running on BEAM, exceeding the maximum (or minimum) representable //// value for a floating point value will raise an error. //// @@ -240,7 +240,7 @@ pub fn floor(x: Float) -> Float pub fn round(x: Float) -> Int { case x >=. 0.0 { True -> js_round(x) - _ -> 0 - js_round(negate(x)) + False -> 0 - js_round(negate(x)) } } @@ -311,7 +311,7 @@ fn do_to_float(a: Int) -> Float pub fn absolute_value(x: Float) -> Float { case x >=. 0.0 { True -> x - _ -> 0.0 -. x + False -> 0.0 -. x } } @@ -409,7 +409,7 @@ pub fn sum(numbers: List(Float)) -> Float { fn sum_loop(numbers: List(Float), initial: Float) -> Float { case numbers { - [x, ..rest] -> sum_loop(rest, x +. initial) + [first, ..rest] -> sum_loop(rest, first +. initial) [] -> initial } } @@ -424,15 +424,12 @@ fn sum_loop(numbers: List(Float), initial: Float) -> Float { /// ``` /// pub fn product(numbers: List(Float)) -> Float { - case numbers { - [] -> 1.0 - _ -> product_loop(numbers, 1.0) - } + product_loop(numbers, 1.0) } fn product_loop(numbers: List(Float), initial: Float) -> Float { case numbers { - [x, ..rest] -> product_loop(rest, x *. initial) + [first, ..rest] -> product_loop(rest, first *. initial) [] -> initial } } diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 14f98169..39c16422 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -65,8 +65,7 @@ pub fn absolute_value(x: Int) -> Int { /// ``` /// pub fn power(base: Int, of exponent: Float) -> Result(Float, Nil) { - base - |> to_float() + to_float(base) |> float.power(exponent) } @@ -85,8 +84,7 @@ pub fn power(base: Int, of exponent: Float) -> Result(Float, Nil) { /// ``` /// pub fn square_root(x: Int) -> Result(Float, Nil) { - x - |> to_float() + to_float(x) |> float.square_root() } @@ -420,7 +418,7 @@ pub fn sum(numbers: List(Int)) -> Int { fn sum_loop(numbers: List(Int), initial: Int) -> Int { case numbers { - [x, ..rest] -> sum_loop(rest, x + initial) + [first, ..rest] -> sum_loop(rest, first + initial) [] -> initial } } @@ -435,15 +433,12 @@ fn sum_loop(numbers: List(Int), initial: Int) -> Int { /// ``` /// pub fn product(numbers: List(Int)) -> Int { - case numbers { - [] -> 1 - _ -> product_loop(numbers, 1) - } + product_loop(numbers, 1) } fn product_loop(numbers: List(Int), initial: Int) -> Int { case numbers { - [x, ..rest] -> product_loop(rest, x * initial) + [first, ..rest] -> product_loop(rest, first * initial) [] -> initial } } @@ -535,8 +530,8 @@ fn undigits_loop(numbers: List(Int), base: Int, acc: Int) -> Result(Int, Nil) { /// pub fn random(max: Int) -> Int { { float.random() *. to_float(max) } - |> float.floor() - |> float.round() + |> float.floor + |> float.round } /// Performs a truncated integer division. diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index fb66b890..2dc9512a 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -61,7 +61,7 @@ pub fn length(of list: List(a)) -> Int { fn length_loop(list: List(a), count: Int) -> Int { case list { [_, ..list] -> length_loop(list, count + 1) - _ -> count + [] -> count } } @@ -124,13 +124,14 @@ pub fn count(list: List(a), where predicate: fn(a) -> Bool) -> Int { /// @external(erlang, "lists", "reverse") pub fn reverse(list: List(a)) -> List(a) { - reverse_loop(list, []) + reverse_and_prepend(list, []) } -fn reverse_loop(remaining: List(a), accumulator: List(a)) -> List(a) { - case remaining { - [] -> accumulator - [item, ..rest] -> reverse_loop(rest, [item, ..accumulator]) +// Reverses a list and prepends it to another list +fn reverse_and_prepend(list prefix: List(a), to suffix: List(a)) -> List(a) { + case prefix { + [] -> suffix + [first, ..rest] -> reverse_and_prepend(list: rest, to: [first, ..suffix]) } } @@ -221,7 +222,7 @@ pub fn contains(list: List(a), any elem: a) -> Bool { pub fn first(list: List(a)) -> Result(a, Nil) { case list { [] -> Error(Nil) - [x, ..] -> Ok(x) + [first, ..] -> Ok(first) } } @@ -650,7 +651,7 @@ pub fn append(first: List(a), second: List(a)) -> List(a) { fn append_loop(first: List(a), second: List(a)) -> List(a) { case first { [] -> second - [item, ..rest] -> append_loop(rest, [item, ..second]) + [first, ..rest] -> append_loop(rest, [first, ..second]) } } @@ -671,14 +672,6 @@ pub fn prepend(to list: List(a), this item: a) -> List(a) { [item, ..list] } -// Reverses a list and prepends it to another list -fn reverse_and_prepend(list prefix: List(a), to suffix: List(a)) -> List(a) { - case prefix { - [] -> suffix - [first, ..rest] -> reverse_and_prepend(list: rest, to: [first, ..suffix]) - } -} - fn flatten_loop(lists: List(List(a)), acc: List(a)) -> List(a) { case lists { [] -> reverse(acc) @@ -732,7 +725,7 @@ pub fn fold( ) -> acc { case list { [] -> initial - [x, ..rest] -> fold(rest, fun(initial, x), fun) + [first, ..rest] -> fold(rest, fun(initial, first), fun) } } @@ -754,7 +747,7 @@ pub fn fold_right( ) -> acc { case list { [] -> initial - [x, ..rest] -> fun(fold_right(rest, initial, fun), x) + [first, ..rest] -> fun(fold_right(rest, initial, fun), first) } } @@ -889,10 +882,10 @@ pub fn find( ) -> Result(a, Nil) { case list { [] -> Error(Nil) - [x, ..rest] -> - case is_desired(x) { - True -> Ok(x) - _ -> find(in: rest, one_that: is_desired) + [first, ..rest] -> + case is_desired(first) { + True -> Ok(first) + False -> find(in: rest, one_that: is_desired) } } } @@ -925,10 +918,10 @@ pub fn find_map( ) -> Result(b, Nil) { case list { [] -> Error(Nil) - [x, ..rest] -> - case fun(x) { - Ok(x) -> Ok(x) - _ -> find_map(in: rest, with: fun) + [first, ..rest] -> + case fun(first) { + Ok(first) -> Ok(first) + Error(_) -> find_map(in: rest, with: fun) } } } @@ -1136,14 +1129,15 @@ fn unzip_loop( pub fn intersperse(list: List(a), with elem: a) -> List(a) { case list { [] | [_] -> list - [x, ..rest] -> intersperse_loop(rest, elem, [x]) + [first, ..rest] -> intersperse_loop(rest, elem, [first]) } } fn intersperse_loop(list: List(a), separator: a, acc: List(a)) -> List(a) { case list { [] -> reverse(acc) - [x, ..rest] -> intersperse_loop(rest, separator, [x, separator, ..acc]) + [first, ..rest] -> + intersperse_loop(rest, separator, [first, separator, ..acc]) } } @@ -1266,7 +1260,7 @@ fn sequences( // Notice how we have to reverse the accumulator we're growing: since // we always add items to the head, `growing` is built in the opposite // sorting order of what it actually is in the original list. - Ascending -> [reverse_loop(growing, []), ..acc] + Ascending -> [reverse(growing), ..acc] Descending -> [growing, ..acc] } @@ -1287,7 +1281,7 @@ fn sequences( // be the one we just found. order.Gt, Ascending | order.Lt, Descending | order.Eq, Descending -> { let acc = case direction { - Ascending -> [reverse_loop(growing, []), ..acc] + Ascending -> [reverse(growing), ..acc] Descending -> [growing, ..acc] } case rest { @@ -1328,7 +1322,7 @@ fn merge_all( // If we have a single list in descending order, we reverse it to make sure // it's in ascending order and we're done. - [sequence], Descending -> reverse_loop(sequence, []) + [sequence], Descending -> reverse(sequence) // Merging together sequences that are in ascending (descending) order // reverses their order, so the recursive call will assume to be merging @@ -1355,12 +1349,12 @@ fn merge_ascending_pairs( acc: List(List(a)), ) { case sequences { - [] -> reverse_loop(acc, []) + [] -> reverse(acc) // Beware, if we have just one item left we must reverse it: we take // ascending lists as input and have to return descending ones. // If we returned it like it is it would be sorted in ascending order. - [sequence] -> reverse_loop([reverse_loop(sequence, []), ..acc], []) + [sequence] -> reverse([reverse(sequence), ..acc]) [ascending1, ascending2, ..rest] -> { let descending = merge_ascendings(ascending1, ascending2, compare, []) @@ -1377,9 +1371,9 @@ fn merge_descending_pairs( acc: List(List(a)), ) { case sequences { - [] -> reverse_loop(acc, []) + [] -> reverse(acc) - [sequence] -> reverse_loop([reverse_loop(sequence, []), ..acc], []) + [sequence] -> reverse([reverse(sequence), ..acc]) [descending1, descending2, ..rest] -> { let ascending = merge_descendings(descending1, descending2, compare, []) @@ -1403,7 +1397,7 @@ fn merge_ascendings( acc: List(a), ) -> List(a) { case list1, list2 { - [], list | list, [] -> reverse_loop(list, acc) + [], list | list, [] -> reverse_and_prepend(list, acc) [first1, ..rest1], [first2, ..rest2] -> case compare(first1, first2) { @@ -1430,7 +1424,7 @@ fn merge_descendings( acc: List(a), ) -> List(a) { case list1, list2 { - [], list | list, [] -> reverse_loop(list, acc) + [], list | list, [] -> reverse_and_prepend(list, acc) [first1, ..rest1], [first2, ..rest2] -> case compare(first1, first2) { order.Lt -> merge_descendings(list1, rest2, compare, [first2, ..acc]) @@ -1567,8 +1561,8 @@ fn split_while_loop( [] -> #(reverse(acc), []) [first, ..rest] -> case f(first) { + True -> split_while_loop(rest, f, [first, ..acc]) False -> #(reverse(acc), list) - _ -> split_while_loop(rest, f, [first, ..acc]) } } } @@ -1673,10 +1667,10 @@ pub fn pop( fn pop_loop(haystack, predicate, checked) { case haystack { [] -> Error(Nil) - [x, ..rest] -> - case predicate(x) { - True -> Ok(#(x, append(reverse(checked), rest))) - False -> pop_loop(rest, predicate, [x, ..checked]) + [first, ..rest] -> + case predicate(first) { + True -> Ok(#(first, append(reverse(checked), rest))) + False -> pop_loop(rest, predicate, [first, ..checked]) } } } @@ -1717,10 +1711,10 @@ fn pop_map_loop( ) -> Result(#(b, List(a)), Nil) { case list { [] -> Error(Nil) - [x, ..rest] -> - case mapper(x) { - Ok(y) -> Ok(#(y, append(reverse(checked), rest))) - Error(_) -> pop_map_loop(rest, mapper, [x, ..checked]) + [first, ..rest] -> + case mapper(first) { + Ok(mapped) -> Ok(#(mapped, append(reverse(checked), rest))) + Error(_) -> pop_map_loop(rest, mapper, [first, ..checked]) } } } @@ -1751,9 +1745,9 @@ fn pop_map_loop( pub fn key_pop(list: List(#(k, v)), key: k) -> Result(#(v, List(#(k, v))), Nil) { pop_map(list, fn(entry) { let #(k, v) = entry - case k { - k if k == key -> Ok(v) - _ -> Error(Nil) + case k == key { + True -> Ok(v) + False -> Error(Nil) } }) } @@ -1776,10 +1770,20 @@ pub fn key_pop(list: List(#(k, v)), key: k) -> Result(#(v, List(#(k, v))), Nil) /// ``` /// pub fn key_set(list: List(#(k, v)), key: k, value: v) -> List(#(k, v)) { + key_set_loop(list, key, value, []) +} + +fn key_set_loop( + list: List(#(k, v)), + key: k, + value: v, + inspected: List(#(k, v)), +) -> List(#(k, v)) { case list { - [] -> [#(key, value)] - [#(k, _), ..rest] if k == key -> [#(key, value), ..rest] - [first, ..rest] -> [first, ..key_set(rest, key, value)] + [#(k, _), ..rest] if k == key -> + reverse_and_prepend(inspected, [#(k, value), ..rest]) + [first, ..rest] -> key_set_loop(rest, key, value, [first, ..inspected]) + [] -> reverse([#(key, value), ..inspected]) } } @@ -1879,7 +1883,7 @@ fn partition_loop(list, categorise, trues, falses) { pub fn permutations(list: List(a)) -> List(List(a)) { case list { [] -> [[]] - _ -> + [_, ..] -> index_map(list, fn(i, i_idx) { index_fold(list, [], fn(acc, j, j_idx) { case i_idx == j_idx { @@ -2025,14 +2029,14 @@ fn chunk_loop( [first, ..rest] -> { let key = f(first) case key == previous_key { + True -> chunk_loop(rest, f, key, [first, ..current_chunk], acc) False -> { let new_acc = [reverse(current_chunk), ..acc] chunk_loop(rest, f, key, [first], new_acc) } - _true -> chunk_loop(rest, f, key, [first, ..current_chunk], acc) } } - _empty -> reverse([reverse(current_chunk), ..acc]) + [] -> reverse([reverse(current_chunk), ..acc]) } } @@ -2161,8 +2165,7 @@ fn scan_loop( /// ``` /// pub fn last(list: List(a)) -> Result(a, Nil) { - list - |> reduce(fn(_, elem) { elem }) + reduce(list, fn(_, elem) { elem }) } /// Return unique combinations of elements in the list. @@ -2252,8 +2255,8 @@ pub fn transpose(list_of_list: List(List(a))) -> List(List(a)) { let take_first = fn(list) { case list { [] -> [] - [f] -> [f] - [f, ..] -> [f] + [first] -> [first] + [first, ..] -> [first] } } @@ -2326,7 +2329,7 @@ pub fn max( reduce(over: list, with: fn(acc, other) { case compare(acc, other) { order.Gt -> acc - _ -> other + order.Lt | order.Eq -> other } }) } @@ -2375,7 +2378,6 @@ fn sample_loop( ) -> Dict(Int, a) { let skip = { let assert Ok(log_result) = float.logarithm(1.0 -. w) - log_random() /. log_result |> float.floor |> float.round } @@ -2383,10 +2385,9 @@ fn sample_loop( case drop(list, skip) { [] -> reservoir - [elem, ..rest] -> { - let reservoir = int.random(k) |> dict.insert(reservoir, _, elem) + [first, ..rest] -> { + let reservoir = dict.insert(reservoir, int.random(k), first) let w = w *. float.exponential(log_random() /. int.to_float(k)) - sample_loop(rest, reservoir, k, index, w) } } diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam index 585ec2d1..f1fc003b 100644 --- a/src/gleam/option.gleam +++ b/src/gleam/option.gleam @@ -44,14 +44,14 @@ pub fn all(list: List(Option(a))) -> Option(List(a)) { fn all_loop(list: List(Option(a)), acc: List(a)) -> Option(List(a)) { case list { [] -> Some(acc) - [x, ..rest] -> { + [first, ..rest] -> { let accumulate = fn(acc, item) { case acc, item { Some(values), Some(value) -> Some([value, ..values]) _, _ -> None } } - accumulate(all_loop(rest, acc), x) + accumulate(all_loop(rest, acc), first) } } } @@ -109,7 +109,7 @@ pub fn is_none(option: Option(a)) -> Bool { pub fn to_result(option: Option(a), e) -> Result(a, e) { case option { Some(a) -> Ok(a) - _ -> Error(e) + None -> Error(e) } } @@ -130,7 +130,7 @@ pub fn to_result(option: Option(a), e) -> Result(a, e) { pub fn from_result(result: Result(a, e)) -> Option(a) { case result { Ok(a) -> Some(a) - _ -> None + Error(_) -> None } } diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index fd43b29e..3aa7c35e 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -148,7 +148,7 @@ pub fn compare(a: String, b: String) -> order.Order { _ -> case less_than(a, b) { True -> order.Lt - _ -> order.Gt + False -> order.Gt } } } @@ -716,7 +716,7 @@ pub fn to_graphemes(string: String) -> List(String) { fn to_graphemes_loop(string: String, acc: List(String)) -> List(String) { case pop_grapheme(string) { Ok(#(grapheme, rest)) -> to_graphemes_loop(rest, [grapheme, ..acc]) - _ -> acc + Error(_) -> acc } } @@ -911,7 +911,7 @@ pub fn last(string: String) -> Result(String, Nil) { pub fn capitalise(string: String) -> String { case pop_grapheme(string) { Ok(#(first, rest)) -> append(to: uppercase(first), suffix: lowercase(rest)) - _ -> "" + Error(_) -> "" } } diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 29545cf4..b64a1c09 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -628,11 +628,11 @@ fn remove_dot_segments_loop( pub fn to_string(uri: Uri) -> String { let parts = case uri.fragment { Some(fragment) -> ["#", fragment] - _ -> [] + None -> [] } let parts = case uri.query { Some(query) -> ["?", query, ..parts] - _ -> parts + None -> parts } let parts = [uri.path, ..parts] let parts = case uri.host, string.starts_with(uri.path, "/") { diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 14d02d73..74c0a69b 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -441,7 +441,7 @@ pub fn find_map_test() { |> list.find_map(with: fn(x) { case x == recursion_test_cycles { True -> Ok(recursion_test_cycles) - _ -> Error(Nil) + False -> Error(Nil) } }) } diff --git a/test/gleam/should.gleam b/test/gleam/should.gleam index b11770a7..85740476 100644 --- a/test/gleam/should.gleam +++ b/test/gleam/should.gleam @@ -41,7 +41,8 @@ pub fn not_equal(a: a, b: a) -> Nil { pub fn be_ok(a: Result(a, e)) -> a { case a { Ok(e) -> e - _ -> panic as { string.concat(["\n", string.inspect(a), "\nshould be ok"]) } + Error(_) -> + panic as { string.concat(["\n", string.inspect(a), "\nshould be ok"]) } } }