diff options
author | Giacomo Cavalieri <giacomo.cavalieri@icloud.com> | 2024-11-07 23:24:45 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-11-07 23:24:53 +0000 |
commit | 978afd77909904c87dd0306f9905586bfd5c1cc2 (patch) | |
tree | cb6a32010710fb0663582ca18f00a9b2c799a0f8 /src | |
parent | 959aa176b8de5a004cfc1e331095a4bd3c6875ec (diff) | |
download | gleam_stdlib-978afd77909904c87dd0306f9905586bfd5c1cc2.tar.gz gleam_stdlib-978afd77909904c87dd0306f9905586bfd5c1cc2.zip |
ensure codebase has a consistent style
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dict.gleam | 6 | ||||
-rw-r--r-- | src/gleam/dynamic.gleam | 14 | ||||
-rw-r--r-- | src/gleam/iterator.gleam | 22 | ||||
-rw-r--r-- | src/gleam/list.gleam | 24 | ||||
-rw-r--r-- | src/gleam/queue.gleam | 38 | ||||
-rw-r--r-- | src/gleam/result.gleam | 2 | ||||
-rw-r--r-- | src/gleam/set.gleam | 14 | ||||
-rw-r--r-- | src/gleam/uri.gleam | 60 |
8 files changed, 90 insertions, 90 deletions
diff --git a/src/gleam/dict.gleam b/src/gleam/dict.gleam index 4697c8d..0c0f8bd 100644 --- a/src/gleam/dict.gleam +++ b/src/gleam/dict.gleam @@ -86,16 +86,16 @@ pub fn to_list(dict: Dict(k, v)) -> List(#(k, v)) /// @external(erlang, "maps", "from_list") pub fn from_list(list: List(#(k, v))) -> Dict(k, v) { - fold_list_of_pair(list, new()) + from_list_loop(list, new()) } -fn fold_list_of_pair( +fn from_list_loop( over list: List(#(k, v)), from initial: Dict(k, v), ) -> Dict(k, v) { case list { [] -> initial - [x, ..rest] -> fold_list_of_pair(rest, insert(initial, x.0, x.1)) + [x, ..rest] -> from_list_loop(rest, insert(initial, x.0, x.1)) } } diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index f142034..b64362f 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -218,10 +218,6 @@ pub fn shallow_list(from value: Dynamic) -> Result(List(Dynamic), DecodeErrors) @external(javascript, "../gleam_stdlib.mjs", "decode_list") fn decode_list(a: Dynamic) -> Result(List(Dynamic), DecodeErrors) -@external(erlang, "gleam_stdlib", "decode_result") -@external(javascript, "../gleam_stdlib.mjs", "decode_result") -fn decode_result(a: Dynamic) -> Result(Result(a, e), DecodeErrors) - /// Checks to see whether a `Dynamic` value is a result of a particular type, and /// returns that result if it is. /// @@ -271,6 +267,10 @@ pub fn result( } } +@external(erlang, "gleam_stdlib", "decode_result") +@external(javascript, "../gleam_stdlib.mjs", "decode_result") +fn decode_result(a: Dynamic) -> Result(Result(a, e), DecodeErrors) + /// Checks to see whether a `Dynamic` value is a list of a particular type, and /// returns that list if it is. /// @@ -970,9 +970,9 @@ pub fn dict( to value_type: Decoder(v), ) -> Decoder(Dict(k, v)) { fn(value) { - use map <- result.try(decode_map(value)) + use dict <- result.try(decode_dict(value)) use pairs <- result.try( - map + dict |> dict.to_list |> list.try_map(fn(pair) { let #(k, v) = pair @@ -993,7 +993,7 @@ pub fn dict( @external(erlang, "gleam_stdlib", "decode_map") @external(javascript, "../gleam_stdlib.mjs", "decode_map") -fn decode_map(a: Dynamic) -> Result(Dict(Dynamic, Dynamic), DecodeErrors) +fn decode_dict(a: Dynamic) -> Result(Dict(Dynamic, Dynamic), DecodeErrors) /// Joins multiple decoders into one. When run they will each be tried in turn /// until one succeeds, or they all fail. diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index 812436b..970fc6b 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -175,17 +175,6 @@ pub fn transform( |> Iterator } -fn fold_loop( - continuation: fn() -> Action(e), - f: fn(acc, e) -> acc, - accumulator: acc, -) -> acc { - case continuation() { - Continue(elem, next) -> fold_loop(next, f, f(accumulator, elem)) - Stop -> accumulator - } -} - /// Reduces an iterator of elements into a single value by calling a given /// function on each element in turn. /// @@ -212,6 +201,17 @@ pub fn fold( |> fold_loop(f, initial) } +fn fold_loop( + continuation: fn() -> Action(e), + f: fn(acc, e) -> acc, + accumulator: acc, +) -> acc { + case continuation() { + Continue(elem, next) -> fold_loop(next, f, f(accumulator, elem)) + Stop -> accumulator + } +} + // TODO: test /// Evaluates all elements emitted by the given iterator. This function is useful for when /// you wish to trigger any side effects that would occur when evaluating diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 97084db..61b3c7b 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -55,12 +55,12 @@ import gleam/pair /// @external(erlang, "erlang", "length") pub fn length(of list: List(a)) -> Int { - count_length(list, 0) + length_loop(list, 0) } -fn count_length(list: List(a), count: Int) -> Int { +fn length_loop(list: List(a), count: Int) -> Int { case list { - [_, ..list] -> count_length(list, count + 1) + [_, ..list] -> length_loop(list, count + 1) _ -> count } } @@ -254,15 +254,6 @@ pub fn rest(list: List(a)) -> Result(List(a), Nil) { } } -fn update_group(f: fn(a) -> k) -> fn(Dict(k, List(a)), a) -> Dict(k, List(a)) { - fn(groups, elem) { - case dict.get(groups, f(elem)) { - Ok(existing) -> dict.insert(groups, f(elem), [elem, ..existing]) - Error(_) -> dict.insert(groups, f(elem), [elem]) - } - } -} - /// Takes a list and groups the values by a key /// which is built from a key function. /// @@ -299,6 +290,15 @@ pub fn group(list: List(v), by key: fn(v) -> k) -> Dict(k, List(v)) { fold(list, dict.new(), update_group(key)) } +fn update_group(f: fn(a) -> k) -> fn(Dict(k, List(a)), a) -> Dict(k, List(a)) { + fn(groups, elem) { + case dict.get(groups, f(elem)) { + Ok(existing) -> dict.insert(groups, f(elem), [elem, ..existing]) + Error(_) -> dict.insert(groups, f(elem), [elem]) + } + } +} + /// Returns a new list containing only the elements from the first list for /// which the given functions returns `True`. /// diff --git a/src/gleam/queue.gleam b/src/gleam/queue.gleam index 62e62a8..c3ea793 100644 --- a/src/gleam/queue.gleam +++ b/src/gleam/queue.gleam @@ -237,6 +237,25 @@ pub fn reverse(queue: Queue(a)) -> Queue(a) { Queue(in: queue.out, out: queue.in) } +/// Checks whether two queues have equal elements in the same order, where the +/// equality of elements is determined by a given equality checking function. +/// +/// This function is useful as the internal representation may be different for +/// two queues with the same elements in the same order depending on how they +/// were constructed, so the equality operator `==` may return surprising +/// results. +/// +/// This function runs in linear time multiplied by the time taken by the +/// element equality checking function. +/// +pub fn is_logically_equal( + a: Queue(a), + to b: Queue(a), + checking element_is_equal: fn(a, a) -> Bool, +) -> Bool { + check_equal(a.out, a.in, b.out, b.in, element_is_equal) +} + fn check_equal( xs: List(a), x_tail: List(a), @@ -257,25 +276,6 @@ fn check_equal( } } -/// Checks whether two queues have equal elements in the same order, where the -/// equality of elements is determined by a given equality checking function. -/// -/// This function is useful as the internal representation may be different for -/// two queues with the same elements in the same order depending on how they -/// were constructed, so the equality operator `==` may return surprising -/// results. -/// -/// This function runs in linear time multiplied by the time taken by the -/// element equality checking function. -/// -pub fn is_logically_equal( - a: Queue(a), - to b: Queue(a), - checking element_is_equal: fn(a, a) -> Bool, -) -> Bool { - check_equal(a.out, a.in, b.out, b.in, element_is_equal) -} - /// Checks whether two queues have the same elements in the same order. /// /// This function is useful as the internal representation may be different for diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index 65cae58..da56c29 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -277,7 +277,7 @@ pub fn unwrap_both(result: Result(a, a)) -> a { /// ``` /// pub fn nil_error(result: Result(a, e)) -> Result(a, Nil) { - map_error(result, fn(_) { Nil }) + replace_error(result, Nil) } /// Returns the first value if it is `Ok`, otherwise returns the second value. diff --git a/src/gleam/set.gleam b/src/gleam/set.gleam index f70c716..6ae5e9e 100644 --- a/src/gleam/set.gleam +++ b/src/gleam/set.gleam @@ -268,13 +268,6 @@ pub fn take(from set: Set(member), keeping desired: List(member)) -> Set(member) Set(dict.take(from: set.dict, keeping: desired)) } -fn order(first: Set(member), second: Set(member)) -> #(Set(member), Set(member)) { - case dict.size(first.dict) > dict.size(second.dict) { - True -> #(first, second) - False -> #(second, first) - } -} - /// Creates a new set that contains all members of both given sets. /// /// This function runs in loglinear time. @@ -291,6 +284,13 @@ pub fn union(of first: Set(member), and second: Set(member)) -> Set(member) { fold(over: smaller, from: larger, with: insert) } +fn order(first: Set(member), second: Set(member)) -> #(Set(member), Set(member)) { + case dict.size(first.dict) > dict.size(second.dict) { + True -> #(first, second) + False -> #(second, first) + } +} + /// Creates a new set that contains members that are present in both given sets. /// /// This function runs in loglinear time. diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index b317f4b..8c2ea7a 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -264,7 +264,27 @@ pub fn percent_decode(value: String) -> Result(String, Nil) { @external(javascript, "../gleam_stdlib.mjs", "percent_decode") fn do_percent_decode(a: String) -> Result(String, Nil) -fn do_remove_dot_segments( +/// Splits the path section of a URI into it's constituent segments. +/// +/// Removes empty segments and resolves dot-segments as specified in +/// [section 5.2](https://www.ietf.org/rfc/rfc3986.html#section-5.2) of the RFC. +/// +/// ## Examples +/// +/// ```gleam +/// path_segments("/users/1") +/// // -> ["users" ,"1"] +/// ``` +/// +pub fn path_segments(path: String) -> List(String) { + remove_dot_segments(string.split(path, "/")) +} + +fn remove_dot_segments(input: List(String)) -> List(String) { + remove_dot_segments_loop(input, []) +} + +fn remove_dot_segments_loop( input: List(String), accumulator: List(String), ) -> List(String) { @@ -278,31 +298,11 @@ fn do_remove_dot_segments( "..", [_, ..accumulator] -> accumulator segment, accumulator -> [segment, ..accumulator] } - do_remove_dot_segments(rest, accumulator) + remove_dot_segments_loop(rest, accumulator) } } } -fn remove_dot_segments(input: List(String)) -> List(String) { - do_remove_dot_segments(input, []) -} - -/// Splits the path section of a URI into it's constituent segments. -/// -/// Removes empty segments and resolves dot-segments as specified in -/// [section 5.2](https://www.ietf.org/rfc/rfc3986.html#section-5.2) of the RFC. -/// -/// ## Examples -/// -/// ```gleam -/// path_segments("/users/1") -/// // -> ["users" ,"1"] -/// ``` -/// -pub fn path_segments(path: String) -> List(String) { - remove_dot_segments(string.split(path, "/")) -} - /// Encodes a `Uri` value as a URI string. /// /// The opposite operation is `uri.parse`. @@ -376,14 +376,6 @@ pub fn origin(uri: Uri) -> Result(String, Nil) { } } -fn drop_last(elements: List(a)) -> List(a) { - list.take(from: elements, up_to: list.length(elements) - 1) -} - -fn join_segments(segments: List(String)) -> String { - string.join(["", ..segments], "/") -} - /// Resolves a URI with respect to the given base URI. /// /// The base URI must be an absolute URI or this function will return an error. @@ -445,3 +437,11 @@ pub fn merge(base: Uri, relative: Uri) -> Result(Uri, Nil) { _ -> Error(Nil) } } + +fn drop_last(elements: List(a)) -> List(a) { + list.take(from: elements, up_to: list.length(elements) - 1) +} + +fn join_segments(segments: List(String)) -> String { + string.join(["", ..segments], "/") +} |