diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/atom.gleam | 8 | ||||
-rw-r--r-- | src/gleam/dynamic.gleam | 49 | ||||
-rw-r--r-- | src/gleam/float.gleam | 25 | ||||
-rw-r--r-- | src/gleam/int.gleam | 18 | ||||
-rw-r--r-- | src/gleam/iodata.gleam | 37 | ||||
-rw-r--r-- | src/gleam/list.gleam | 162 | ||||
-rw-r--r-- | src/gleam/map.gleam | 80 | ||||
-rw-r--r-- | src/gleam/result.gleam | 5 | ||||
-rw-r--r-- | src/gleam/should.gleam | 21 | ||||
-rw-r--r-- | src/gleam/string.gleam | 77 |
10 files changed, 244 insertions, 238 deletions
diff --git a/src/gleam/atom.gleam b/src/gleam/atom.gleam index 8e9cb04..b101139 100644 --- a/src/gleam/atom.gleam +++ b/src/gleam/atom.gleam @@ -13,7 +13,7 @@ /// user input into atoms) we may hit the max limit of atoms and cause the /// virtual machine to crash. /// -pub external type Atom; +pub external type Atom /// An error returned when no atom is found in the virtual machine's atom table /// for a given string when calling the [`from_string`](#from_string) function. @@ -35,7 +35,7 @@ pub type FromStringError { /// Error(AtomNotLoaded) /// pub external fn from_string(String) -> Result(Atom, FromStringError) = - "gleam_stdlib" "atom_from_string"; + "gleam_stdlib" "atom_from_string" /// Create an atom from a string, inserting a new value into the virtual /// machine's atom table if an atom does not already exist for the given @@ -47,7 +47,7 @@ pub external fn from_string(String) -> Result(Atom, FromStringError) = /// virtual machine to crash! /// pub external fn create_from_string(String) -> Atom = - "gleam_stdlib" "atom_create_from_string"; + "gleam_stdlib" "atom_create_from_string" /// Retuns a `String` corresponding to the text representation of the given /// `Atom`. @@ -59,4 +59,4 @@ pub external fn create_from_string(String) -> Atom = /// "ok" /// pub external fn to_string(Atom) -> String = - "gleam_stdlib" "atom_to_string"; + "gleam_stdlib" "atom_to_string" diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 4355e59..bab30d3 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -5,11 +5,12 @@ import gleam/result /// `Dynamic` data is data that we don"t know the type of yet. /// We likely get data like this from interop with Erlang, or from /// IO with the outside world. -pub external type Dynamic; +pub external type Dynamic /// Convert any Gleam data into `Dynamic` data. /// -pub external fn from(a) -> Dynamic = "gleam_stdlib" "identity"; +pub external fn from(a) -> Dynamic = + "gleam_stdlib" "identity" /// Unsafely cast a Dynamic value into any other type. /// @@ -18,7 +19,8 @@ pub external fn from(a) -> Dynamic = "gleam_stdlib" "identity"; /// /// If you can avoid using this function, do! /// -pub external fn unsafe_coerce(Dynamic) -> a = "gleam_stdlib" "identity"; +pub external fn unsafe_coerce(Dynamic) -> a = + "gleam_stdlib" "identity" /// Check to see whether a Dynamic value is a string, and return the string if /// it is. @@ -31,8 +33,8 @@ pub external fn unsafe_coerce(Dynamic) -> a = "gleam_stdlib" "identity"; /// > string(from(123)) /// Error("Expected a String, got `123`") /// -pub external fn string(from: Dynamic) -> Result(String, String) - = "gleam_stdlib" "decode_string" +pub external fn string(from: Dynamic) -> Result(String, String) = + "gleam_stdlib" "decode_string" /// Check to see whether a Dynamic value is an int, and return the int if it /// is. @@ -45,8 +47,8 @@ pub external fn string(from: Dynamic) -> Result(String, String) /// > int(from("Hello")) /// Error("Expected an Int, got `\"Hello World\"`") /// -pub external fn int(from: Dynamic) -> Result(Int, String) - = "gleam_stdlib" "decode_int" +pub external fn int(from: Dynamic) -> Result(Int, String) = + "gleam_stdlib" "decode_int" /// Check to see whether a Dynamic value is an float, and return the float if /// it is. @@ -59,8 +61,8 @@ pub external fn int(from: Dynamic) -> Result(Int, String) /// > float(from(123)) /// Error("Expected a Float, got `123`") /// -pub external fn float(from: Dynamic) -> Result(Float, String) - = "gleam_stdlib" "decode_float" +pub external fn float(from: Dynamic) -> Result(Float, String) = + "gleam_stdlib" "decode_float" /// Check to see whether a Dynamic value is an atom, and return the atom if /// it is. @@ -74,8 +76,8 @@ pub external fn float(from: Dynamic) -> Result(Float, String) /// > atom(from(123)) /// Error("Expected an Atom, got `123`") /// -pub external fn atom(from: Dynamic) -> Result(atom.Atom, String) - = "gleam_stdlib" "decode_atom" +pub external fn atom(from: Dynamic) -> Result(atom.Atom, String) = + "gleam_stdlib" "decode_atom" /// Check to see whether a Dynamic value is an bool, and return the bool if /// it is. @@ -88,8 +90,8 @@ pub external fn atom(from: Dynamic) -> Result(atom.Atom, String) /// > bool(from(123)) /// Error("Expected a Bool, got `123`") /// -pub external fn bool(from: Dynamic) -> Result(Bool, String) - = "gleam_stdlib" "decode_bool" +pub external fn bool(from: Dynamic) -> Result(Bool, String) = + "gleam_stdlib" "decode_bool" /// Check to see whether a Dynamic value is a function that takes no arguments, /// and return the function if it is. @@ -104,11 +106,11 @@ pub external fn bool(from: Dynamic) -> Result(Bool, String) /// > thunk(from(123)) /// Error("Expected a zero arity function, got `123`") /// -pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String) - = "gleam_stdlib" "decode_thunk" +pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String) = + "gleam_stdlib" "decode_thunk" -external fn list_dynamic(from: Dynamic) -> Result(List(Dynamic), String) - = "gleam_stdlib" "decode_list" +external fn list_dynamic(from: Dynamic) -> Result(List(Dynamic), String) = + "gleam_stdlib" "decode_list" /// Check to see whether a Dynamic value is a list, and return the list if it /// is. @@ -130,7 +132,7 @@ pub fn list( ) -> Result(List(inner), String) { dynamic |> list_dynamic - |> result.then(_, list_mod.traverse(_, decoder_type)) + |> result.then(list_mod.traverse(_, decoder_type)) } /// Check to see if a Dynamic value is a map with a specific field, and return @@ -147,8 +149,8 @@ pub fn list( /// > field(from(123), "Hello") /// Error("Expected a map with key `\"Hello\"`, got an Int") /// -pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String) - = "gleam_stdlib" "decode_field" +pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String) = + "gleam_stdlib" "decode_field" /// Check to see if the Dynamic value is a tuple large enough to have a certain /// index, and return the value of that index if it is. @@ -164,5 +166,8 @@ pub external fn field(from: Dynamic, named: a) -> Result(Dynamic, String) /// > element(from(""), 2) /// Error("Expected a Tuple, got a binary") /// -pub external fn element(from: Dynamic, position: Int) -> Result(Dynamic, String) - = "gleam_stdlib" "decode_element" +pub external fn element( + from: Dynamic, + position: Int, +) -> Result(Dynamic, String) = + "gleam_stdlib" "decode_element" diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 918ecc2..6556204 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -15,8 +15,8 @@ pub type Float = /// > parse("ABC") /// None /// -pub external fn parse(String) -> Option(Float) - = "gleam_stdlib" "parse_float"; +pub external fn parse(String) -> Option(Float) = + "gleam_stdlib" "parse_float" /// Return the string representation of the provided float. /// @@ -39,11 +39,10 @@ pub fn to_string(f: Float) -> String { pub fn compare(a: Float, with b: Float) -> Order { case a == b { True -> order.Eq - False -> - case a <. b { - True -> order.Lt - False -> order.Gt - } + False -> case a <. b { + True -> order.Lt + False -> order.Gt + } } } @@ -82,7 +81,8 @@ pub fn max(a: Float, b: Float) -> Float { /// > ceiling(2.3) /// 3.0 /// -pub external fn ceiling(Float) -> Float = "math" "ceil"; +pub external fn ceiling(Float) -> Float = + "math" "ceil" /// Rounds the value to the next lowest whole number as a float. /// @@ -91,7 +91,8 @@ pub external fn ceiling(Float) -> Float = "math" "ceil"; /// > floor(2.3) /// 2.0 /// -pub external fn floor(Float) -> Float = "math" "floor"; +pub external fn floor(Float) -> Float = + "math" "floor" /// Rounds the value to the nearest whole number as an int. /// @@ -103,7 +104,8 @@ pub external fn floor(Float) -> Float = "math" "floor"; /// > round(2.5) /// 3 /// -pub external fn round(Float) -> Int = "erlang" "round"; +pub external fn round(Float) -> Int = + "erlang" "round" /// Returns the value as an int, truncating all decimal digits. /// @@ -112,4 +114,5 @@ pub external fn round(Float) -> Int = "erlang" "round"; /// > truncate(2.4343434847383438) /// 2 /// -pub external fn truncate(Float) -> Int = "erlang" "trunc"; +pub external fn truncate(Float) -> Int = + "erlang" "trunc" diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index a0a0462..7399aac 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -14,7 +14,8 @@ pub type Int = /// > parse("ABC") /// Error(Nil) /// -pub external fn parse(String) -> Option(Int) = "gleam_stdlib" "parse_int"; +pub external fn parse(String) -> Option(Int) = + "gleam_stdlib" "parse_int" /// Print a given int to a string. /// @@ -23,7 +24,8 @@ pub external fn parse(String) -> Option(Int) = "gleam_stdlib" "parse_int"; /// > to_string(2) /// "2" /// -pub external fn to_string(Int) -> String = "erlang" "integer_to_binary" +pub external fn to_string(Int) -> String = + "erlang" "integer_to_binary" /// Print a given int to a string using the base number provided. /// @@ -38,7 +40,8 @@ pub external fn to_string(Int) -> String = "erlang" "integer_to_binary" /// > to_base_string(48, 36) /// "1C" /// -pub external fn to_base_string(Int, Int) -> String = "erlang" "integer_to_binary" +pub external fn to_base_string(Int, Int) -> String = + "erlang" "integer_to_binary" /// Compares two ints, returning an order. /// @@ -56,11 +59,10 @@ pub external fn to_base_string(Int, Int) -> String = "erlang" "integer_to_binary pub fn compare(a: Int, with b: Int) -> Order { case a == b { True -> order.Eq - False -> - case a < b { - True -> order.Lt - False -> order.Gt - } + False -> case a < b { + True -> order.Lt + False -> order.Gt + } } } diff --git a/src/gleam/iodata.gleam b/src/gleam/iodata.gleam index 5310b5e..fe24974 100644 --- a/src/gleam/iodata.gleam +++ b/src/gleam/iodata.gleam @@ -9,56 +9,56 @@ /// using minimal memory, and then can be efficiently converted to a string /// using the `to_string` function. /// -pub external type Iodata; +pub external type Iodata /// Prepend a String onto the start of some Iodata. /// /// Runs in constant time. /// pub external fn prepend(to: Iodata, prefix: String) -> Iodata = - "gleam_stdlib" "iodata_prepend"; + "gleam_stdlib" "iodata_prepend" /// Append a String onto the end of some Iodata. /// /// Runs in constant time. /// pub external fn append(to: Iodata, suffix: String) -> Iodata = - "gleam_stdlib" "iodata_append"; + "gleam_stdlib" "iodata_append" /// Prepend some Iodata onto the start of another. /// /// Runs in constant time. /// pub external fn prepend_iodata(to: Iodata, prefix: Iodata) -> Iodata = - "gleam_stdlib" "iodata_prepend"; + "gleam_stdlib" "iodata_prepend" /// Append some Iodata onto the end of another. /// /// Runs in constant time. /// pub external fn append_iodata(to: Iodata, suffix: Iodata) -> Iodata = - "gleam_stdlib" "iodata_append"; + "gleam_stdlib" "iodata_append" /// Convert a list of strings into iodata. /// /// Runs in constant time. /// pub external fn from_strings(List(String)) -> Iodata = - "gleam_stdlib" "identity"; + "gleam_stdlib" "identity" /// Joins a list of iodata into a single iodata. /// /// Runs in constant time. /// pub external fn concat(List(Iodata)) -> Iodata = - "gleam_stdlib" "identity"; + "gleam_stdlib" "identity" /// Convert a string into iodata. /// /// Runs in constant time. /// pub external fn new(String) -> Iodata = - "gleam_stdlib" "identity"; + "gleam_stdlib" "identity" /// Turns an `Iodata` into a `String` /// @@ -66,32 +66,35 @@ pub external fn new(String) -> Iodata = /// optimised. /// pub external fn to_string(Iodata) -> String = - "erlang" "iolist_to_binary"; + "erlang" "iolist_to_binary" /// Returns the size of the Iodata in bytes. /// pub external fn byte_size(Iodata) -> Int = - "erlang" "iolist_size"; + "erlang" "iolist_size" /// Creates textual representation of the given float as iodata. /// pub external fn from_float(Float) -> Iodata = - "io_lib_format" "fwrite_g"; + "io_lib_format" "fwrite_g" /// Converts Iodata to a new Iodata where valid UTF-8 string data is /// lowercased. /// -pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase" +pub external fn lowercase(Iodata) -> Iodata = + "string" "lowercase" /// Converts Iodata to a new Iodata where valid UTF-8 string data is /// uppercased. /// -pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase" +pub external fn uppercase(Iodata) -> Iodata = + "string" "uppercase" /// Converts Iodata to a new Iodata where valid UTF-8 string data is /// reversed. /// -pub external fn reverse(Iodata) -> Iodata = "string" "reverse" +pub external fn reverse(Iodata) -> Iodata = + "string" "reverse" type Direction { All @@ -135,7 +138,8 @@ pub fn replace( /// True /// /// -pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal" +pub external fn is_equal(Iodata, Iodata) -> Bool = + "string" "equal" /// Inspect some iodata to determine if it is equivalent to an empty string. /// @@ -151,4 +155,5 @@ pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal" /// True /// /// -pub external fn is_empty(Iodata) -> Bool = "string" "is_empty" +pub external fn is_empty(Iodata) -> Bool = + "string" "is_empty" diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 67bd778..a0098a1 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -51,7 +51,8 @@ pub type LengthMismatch { /// > length([1, 2]) /// 2 /// -pub external fn length(of: List(a)) -> Int = "erlang" "length" +pub external fn length(of: List(a)) -> Int = + "erlang" "length" /// Create a new list from a given list containing the same elements but in the /// opposite order. @@ -73,7 +74,8 @@ pub external fn length(of: List(a)) -> Int = "erlang" "length" /// > reverse([1, 2]) /// [2, 1] /// -pub external fn reverse(List(a)) -> List(a) = "lists" "reverse" +pub external fn reverse(List(a)) -> List(a) = + "lists" "reverse" /// Determine whether or not the list is empty. /// @@ -119,7 +121,7 @@ pub fn is_empty(list: List(a)) -> Bool { pub fn contains(list: List(a), has elem: a) -> Bool { case list { [] -> False - [head | rest] -> head == elem || contains(rest, elem) + [head, ..rest] -> head == elem || contains(rest, elem) } } @@ -139,7 +141,7 @@ pub fn contains(list: List(a), has elem: a) -> Bool { pub fn head(list: List(a)) -> Option(a) { case list { [] -> result.none() - [x | _] -> Ok(x) + [x, ..] -> Ok(x) } } @@ -162,16 +164,16 @@ pub fn head(list: List(a)) -> Option(a) { pub fn tail(list: List(a)) -> Option(List(a)) { case list { [] -> result.none() - [_ | xs] -> Ok(xs) + [_, ..xs] -> Ok(xs) } } fn do_filter(list: List(a), fun: fn(a) -> Bool, acc: List(a)) -> List(a) { case list { [] -> reverse(acc) - [x | xs] -> { + [x, ..xs] -> { let new_acc = case fun(x) { - True -> [x | acc] + True -> [x, ..acc] False -> acc } do_filter(xs, fun, new_acc) @@ -197,7 +199,7 @@ pub fn filter(list: List(a), for predicate: fn(a) -> Bool) -> List(a) { fn do_map(list: List(a), fun: fn(a) -> b, acc: List(b)) -> List(b) { case list { [] -> reverse(acc) - [x | xs] -> do_map(xs, fun, [fun(x) | acc]) + [x, ..xs] -> do_map(xs, fun, [fun(x), ..acc]) } } @@ -221,7 +223,7 @@ fn do_index_map( ) -> List(b) { case list { [] -> reverse(acc) - [x | xs] -> do_index_map(xs, fun, index + 1, [fun(index, x) | acc]) + [x, ..xs] -> do_index_map(xs, fun, index + 1, [fun(index, x), ..acc]) } } @@ -240,7 +242,6 @@ pub fn index_map(list: List(a), with fun: fn(Int, a) -> b) -> List(b) { do_index_map(list, fun, 0, []) } - fn do_traverse( list: List(a), fun: fn(a) -> Result(b, e), @@ -248,11 +249,10 @@ fn do_traverse( ) -> Result(List(b), e) { case list { [] -> Ok(reverse(acc)) - [x | xs] -> - case fun(x) { - Ok(y) -> do_traverse(xs, fun, [y | acc]) - Error(error) -> Error(error) - } + [x, ..xs] -> case fun(x) { + Ok(y) -> do_traverse(xs, fun, [y, ..acc]) + Error(error) -> Error(error) + } } } @@ -306,22 +306,20 @@ pub fn traverse( pub fn drop(from list: List(a), up_to n: Int) -> List(a) { case n <= 0 { True -> list - False -> - case list { - [] -> [] - [_ | xs] -> drop(xs, n - 1) - } + False -> case list { + [] -> [] + [_, ..xs] -> drop(xs, n - 1) + } } } fn do_take(list: List(a), n: Int, acc: List(a)) -> List(a) { case n <= 0 { True -> reverse(acc) - False -> - case list { - [] -> reverse(acc) - [x | xs] -> do_take(xs, n - 1, [x | acc]) - } + False -> case list { + [] -> reverse(acc) + [x, ..xs] -> do_take(xs, n - 1, [x, ..acc]) + } } } @@ -366,13 +364,13 @@ pub fn new() -> List(a) { /// > append([1, 2], [3]) /// [1, 2, 3] /// -pub external fn append(List(a), List(a)) -> List(a) - = "lists" "append"; +pub external fn append(List(a), List(a)) -> List(a) = + "lists" "append" fn do_flatten(lists: List(List(a)), acc: List(a)) -> List(a) { case lists { [] -> acc - [l | rest] -> do_flatten(rest, append(acc, l)) + [l, ..rest] -> do_flatten(rest, append(acc, l)) } } @@ -400,7 +398,7 @@ pub fn flatten(lists: List(List(a))) -> List(a) { pub fn fold(list: List(a), from initial: b, with fun: fn(a, b) -> b) -> b { case list { [] -> initial - [x | rest] -> fold(rest, fun(x, initial), fun) + [x, ..rest] -> fold(rest, fun(x, initial), fun) } } @@ -422,7 +420,7 @@ pub fn fold_right( ) -> b { case list { [] -> initial - [x | rest] -> fun(x, fold_right(rest, initial, fun)) + [x, ..rest] -> fun(x, fold_right(rest, initial, fun)) } } @@ -449,11 +447,10 @@ pub fn find( ) -> Option(a) { case haystack { [] -> result.none() - [x | rest] -> - case is_desired(x) { - True -> Ok(x) - _ -> find(in: rest, one_that: is_desired) - } + [x, ..rest] -> case is_desired(x) { + True -> Ok(x) + _ -> find(in: rest, one_that: is_desired) + } } } @@ -480,11 +477,10 @@ pub fn find_map( ) -> Option(b) { case haystack { [] -> result.none() - [x | rest] -> - case fun(x) { - Ok(x) -> Ok(x) - _ -> find_map(in: rest, with: fun) - } + [x, ..rest] -> case fun(x) { + Ok(x) -> Ok(x) + _ -> find_map(in: rest, with: fun) + } } } @@ -506,8 +502,7 @@ pub fn find_map( pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool { case list { [] -> True - [x | rest] -> - case predicate(x) { + [x, ..rest] -> case predicate(x) { True -> all(rest, predicate) _ -> False } @@ -535,8 +530,7 @@ pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool { pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool { case list { [] -> False - [x | rest] -> - case predicate(x) { + [x, ..rest] -> case predicate(x) { False -> any(rest, predicate) _ -> True } @@ -566,7 +560,7 @@ pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) { case xs, ys { [], _ -> [] _, [] -> [] - [x | xs], [y | ys] -> [tuple(x, y) | zip(xs, ys)] + [x, ..xs], [y, ..ys] -> [tuple(x, y), ..zip(xs, ys)] } } @@ -588,7 +582,10 @@ pub fn zip(xs: List(a), ys: List(b)) -> List(tuple(a, b)) { /// > strict_zip([1, 2], [3, 4]) /// Ok([tuple(1, 3), tuple(2, 4)]) /// -pub fn strict_zip(l1: List(a), l2: List(b)) -> Result(List(tuple(a, b)), LengthMismatch) { +pub fn strict_zip( + l1: List(a), + l2: List(b), +) -> Result(List(tuple(a, b)), LengthMismatch) { case length(of: l1) == length(of: l2) { True -> Ok(zip(l1, l2)) False -> Error(LengthMismatch) @@ -610,7 +607,7 @@ pub fn strict_zip(l1: List(a), l2: List(b)) -> Result(List(tuple(a, b)), LengthM pub fn intersperse(list: List(a), with elem: a) -> List(a) { case list { [] | [_] -> list - [x | rest] -> [x | [elem | intersperse(rest, elem)]] + [x, ..rest] -> [x, elem, ..intersperse(rest, elem)] } } @@ -630,14 +627,12 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) { pub fn at(in list: List(a), get index: Int) -> Option(a) { case index < 0 { True -> result.none() - False -> - case list { + False -> case list { [] -> result.none() - [x | rest] -> - case index == 0 { - True -> Ok(x) - False -> at(rest, index - 1) - } + [x, ..rest] -> case index == 0 { + True -> Ok(x) + False -> at(rest, index - 1) + } } } } @@ -654,7 +649,7 @@ pub fn at(in list: List(a), get index: Int) -> Option(a) { pub fn unique(list: List(a)) -> List(a) { case list { [] -> [] - [x | rest] -> [x | unique(filter(rest, fn(y) { y != x }))] + [x, ..rest] -> [x, ..unique(filter(rest, fn(y) { y != x }))] } } @@ -662,15 +657,18 @@ fn merge_sort(a: List(a), b: List(a), compare: fn(a, a) -> Order) -> List(a) { case a, b { [], _ -> b _, [] -> a - [ax | ar], [bx | br] -> - case compare(ax, bx) { - order.Lt -> [ax | merge_sort(ar, b, compare)] - _ -> [bx | merge_sort(a, br, compare)] - } + [ax, ..ar], [bx, ..br] -> case compare(ax, bx) { + order.Lt -> [ax, ..merge_sort(ar, b, compare)] + _ -> [bx, ..merge_sort(a, br, compare)] + } } } -fn do_sort(list: List(a), compare: fn(a, a) -> Order, list_length: Int) -> List(a) { +fn do_sort( + list: List(a), + compare: fn(a, a) -> Order, + list_length: Int, +) -> List(a) { case list_length < 2 { True -> list False -> { @@ -715,15 +713,15 @@ pub fn sort(list: List(a), sort_by compare: fn(a, a) -> Order) -> List(a) { pub fn range(from start: Int, to stop: Int) -> List(Int) { case int.compare(start, stop) { order.Eq -> [] - order.Gt -> [start | range(start - 1, stop)] - order.Lt -> [start | range(start + 1, stop)] + order.Gt -> [start, ..range(start - 1, stop)] + order.Lt -> [start, ..range(start + 1, stop)] } } fn do_repeat(a: a, times: Int, acc: List(a)) -> List(a) { case times <= 0 { True -> acc - False -> do_repeat(a, times - 1, [a | acc]) + False -> do_repeat(a, times - 1, [a, ..acc]) } } @@ -744,11 +742,10 @@ pub fn repeat(item a: a, times times: Int) -> List(a) { fn do_split(list: List(a), n: Int, taken: List(a)) -> tuple(List(a), List(a)) { case n <= 0 { True -> tuple(reverse(taken), list) - False -> - case list { - [] -> tuple(reverse(taken), []) - [x | xs] -> do_split(xs, n - 1, [x | taken]) - } + False -> case list { + [] -> tuple(reverse(taken), []) + [x, ..xs] -> do_split(xs, n - 1, [x, ..taken]) + } } } @@ -779,11 +776,10 @@ fn do_split_while( ) -> tuple(List(a), List(a)) { case list { [] -> tuple(reverse(acc), []) - [x | xs] -> - case f(x) { - False -> tuple(reverse(acc), list) - _ -> do_split_while(xs, f, [x | acc]) - } + [x, ..xs] -> case f(x) { + False -> tuple(reverse(acc), list) + _ -> do_split_while(xs, f, [x, ..acc]) + } } } @@ -808,7 +804,6 @@ pub fn split_while( do_split_while(list, predicate, []) } - /// Given a list of 2 element tuples, find the first tuple that has a given /// key as the first element and return the second element. /// @@ -832,11 +827,14 @@ pub fn key_find( in keyword_list: List(tuple(k, v)), find desired_key: k, ) -> Option(v) { - find_map(keyword_list, fn(keyword) { - let tuple(key, value) = keyword - case key == desired_key { - True -> Ok(value) - False -> result.none() - } - }) + find_map( + keyword_list, + fn(keyword) { + let tuple(key, value) = keyword + case key == desired_key { + True -> Ok(value) + False -> result.none() + } + }, + ) } diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam index daaee6e..df7b71e 100644 --- a/src/gleam/map.gleam +++ b/src/gleam/map.gleam @@ -16,7 +16,7 @@ import gleam/result.{Option} /// See [the Erlang map module](https://erlang.org/doc/man/maps.html) for more /// information. /// -pub external type Map(key, value); +pub external type Map(key, value) /// Determine the number of key-value pairs in the map. /// This function runs in constant time and does not need to iterate the map. @@ -30,8 +30,8 @@ pub external type Map(key, value); /// 1 /// /// -pub external fn size(Map(k, v)) -> Int - = "maps" "size" +pub external fn size(Map(k, v)) -> Int = + "maps" "size" /// Convert the map to a list of 2-element tuples `tuple(key, value)`, one for /// each key-value pair in the map. @@ -46,19 +46,19 @@ pub external fn size(Map(k, v)) -> Int /// > new() |> insert("key", 0) |> to_list() /// [tuple("key", 0)] /// -pub external fn to_list(Map(key, value)) -> List(tuple(key, value)) - = "maps" "to_list" +pub external fn to_list(Map(key, value)) -> List(tuple(key, value)) = + "maps" "to_list" /// Convert a list of 2-element tuples `tuple(key, value)` to a map. /// /// If two tuples have the same key the last one in the list will be the one /// that is present in the map. /// -pub external fn from_list(List(tuple(key, value))) -> Map(key, value) - = "maps" "from_list" +pub external fn from_list(List(tuple(key, value))) -> Map(key, value) = + "maps" "from_list" -external fn is_key(key, Map(key, v)) -> Bool - = "maps" "is_key" +external fn is_key(key, Map(key, v)) -> Bool = + "maps" "is_key" /// Determind whether or not a value present in the map for a given key. /// @@ -74,11 +74,10 @@ pub fn has_key(map: Map(k, v), key: k) -> Bool { is_key(key, map) } - /// Create a fresh map that contains no values. /// -pub external fn new() -> Map(key, value) - = "maps" "new" +pub external fn new() -> Map(key, value) = + "maps" "new" /// Fetch a value from a map for a given key. /// @@ -93,11 +92,11 @@ pub external fn new() -> Map(key, value) /// > new() |> insert("a", 0) |> get("b") /// Error(Nil) /// -pub external fn get(from: Map(key, value), get: key) -> Option(value) - = "gleam_stdlib" "map_get"; +pub external fn get(from: Map(key, value), get: key) -> Option(value) = + "gleam_stdlib" "map_get" -external fn erl_insert(key, value, Map(key, value)) -> Map(key, value) - = "maps" "put"; +external fn erl_insert(key, value, Map(key, value)) -> Map(key, value) = + "maps" "put" /// Insert a value into the map with the given key. /// @@ -116,9 +115,8 @@ pub fn insert(into map: Map(k, v), for key: k, insert value: v) -> Map(k, v) { erl_insert(key, value, map) } -external fn erl_map_values(fn(key, a) -> b, Map(key, value)) - -> Map(key, b) - = "maps" "map"; +external fn erl_map_values(fn(key, a) -> b, Map(key, value)) -> Map(key, b) = + "maps" "map" /// Update all values in a given map by calling a given function on each key /// and value. @@ -146,8 +144,8 @@ pub fn map_values(in map: Map(k, v), with fun: fn(k, v) -> w) -> Map(k, w) { /// > keys([tuple("a", 0), tuple("b", 1)]) /// ["a", "b"] /// -pub external fn keys(Map(keys, v)) -> List(keys) - = "maps" "keys" +pub external fn keys(Map(keys, v)) -> List(keys) = + "maps" "keys" /// Get a list of all values in a given map. /// @@ -160,12 +158,14 @@ pub external fn keys(Map(keys, v)) -> List(keys) /// > keys(from_list([tuple("a", 0), tuple("b", 1)])) /// [0, 1] /// -pub external fn values(Map(k, values)) -> List(values) - = "maps" "values" +pub external fn values(Map(k, values)) -> List(values) = + "maps" "values" -external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) - -> Map(key, value) - = "maps" "filter"; +external fn erl_filter( + fn(key, value) -> Bool, + Map(key, value), +) -> Map(key, value) = + "maps" "filter" /// Create a new map from a given map, minus any entries that a given function /// returns False for. @@ -184,8 +184,8 @@ pub fn filter(in map: Map(k, v), for predicate: fn(k, v) -> Bool) -> Map(k, v) { erl_filter(predicate, map) } -external fn erl_take(List(k), Map(k, v)) -> Map(k, v) - = "maps" "with" +external fn erl_take(List(k), Map(k, v)) -> Map(k, v) = + "maps" "with" /// Create a new map from a given map, only including any entries for which the /// keys are in a given list. @@ -216,12 +216,11 @@ pub fn take(from map: Map(k, v), drop desired_keys: List(k)) -> Map(k, v) { /// > merge(a, b) /// from_list([tuple("a", 0), tuple("b", 2), tuple("c", 3)]) /// -pub external fn merge(into: Map(k, v), merge: Map(k, v)) -> Map(k, v) - = "maps" "merge" - -external fn erl_delete(k, Map(k, v)) -> Map(k, v) - = "maps" "remove" +pub external fn merge(into: Map(k, v), merge: Map(k, v)) -> Map(k, v) = + "maps" "merge" +external fn erl_delete(k, Map(k, v)) -> Map(k, v) = + "maps" "remove" /// Create a new map from a given map with all the same entries except for the /// one with a given key, if it exists. @@ -253,9 +252,7 @@ pub fn delete(from map: Map(k, v), delete key: k) -> Map(k, v) { /// from_list([]) /// pub fn drop(from map: Map(k, v), drop disallowed_keys: List(k)) -> Map(k, v) { - list.fold(disallowed_keys, map, fn(key, acc) { - delete(acc, key) - }) + list.fold(disallowed_keys, map, fn(key, acc) { delete(acc, key) }) } /// Create a new map with one entry updated using a given function. @@ -284,7 +281,10 @@ pub fn update( update key: k, with fun: fn(Option(v)) -> v, ) -> Map(k, v) { - map |> get(_, key) |> fun |> insert(map, key, _) + map + |> get(key) + |> fun + |> insert(map, key, _) } fn do_fold( @@ -294,7 +294,7 @@ fn do_fold( ) -> acc { case list { [] -> initial - [tuple(k, v) | tail] -> do_fold(tail, fun(k, v, initial), fun) + [tuple(k, v), ..tail] -> do_fold(tail, fun(k, v, initial), fun) } } @@ -320,5 +320,7 @@ pub fn fold( from initial: acc, with fun: fn(k, v, acc) -> acc, ) -> acc { - map |> to_list |> do_fold(_, initial, fun) + map + |> to_list + |> do_fold(initial, fun) } diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index b002f8e..7dd2076 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -71,10 +71,7 @@ pub fn is_error(result: Result(a, e)) -> Bool { /// > map(over: Error(1), with: fn(x) { x + 1 }) /// Error(1) /// -pub fn map( - over result: Result(a, e), - with fun: fn(a) -> b, -) -> Result(b, e) { +pub fn map(over result: Result(a, e), with fun: fn(a) -> b) -> Result(b, e) { case result { Ok(x) -> Ok(fun(x)) Error(e) -> Error(e) diff --git a/src/gleam/should.gleam b/src/gleam/should.gleam index b0a1dc5..de07f10 100644 --- a/src/gleam/should.gleam +++ b/src/gleam/should.gleam @@ -6,20 +6,25 @@ // TODO: Move this module into another package so it can be used as a // dep only in test. +pub external type Expectation -pub external type Expectation; +pub external fn equal(a, a) -> Expectation = + "gleam_stdlib" "should_equal" -pub external fn equal(a, a) -> Expectation = "gleam_stdlib" "should_equal"; +pub external fn not_equal(a, a) -> Expectation = + "gleam_stdlib" "should_not_equal" -pub external fn not_equal(a, a) -> Expectation = "gleam_stdlib" "should_not_equal"; +pub external fn be_true(Bool) -> Expectation = + "gleam_stdlib" "should_be_true" -pub external fn be_true(Bool) -> Expectation = "gleam_stdlib" "should_be_true"; +pub external fn be_false(Bool) -> Expectation = + "gleam_stdlib" "should_be_false" -pub external fn be_false(Bool) -> Expectation = "gleam_stdlib" "should_be_false"; +pub external fn be_ok(Result(a, b)) -> Expectation = + "gleam_stdlib" "should_be_ok" -pub external fn be_ok(Result(a, b)) -> Expectation = "gleam_stdlib" "should_be_ok"; - -pub external fn be_error(Result(a, b)) -> Expectation = "gleam_stdlib" "should_be_error"; +pub external fn be_error(Result(a, b)) -> Expectation = + "gleam_stdlib" "should_be_error" pub fn fail() -> Expectation { be_true(False) diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index de6f58a..8c270bc 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -39,9 +39,10 @@ pub fn is_empty(str: String) -> Bool { /// /// > length("") /// 0 -/// -pub external fn length(String) -> Int = "string" "length" +pub external fn length(String) -> Int = + "string" "length" +/// /// Reverse a string. /// /// This function has to iterate across the whole string so it runs in linear @@ -51,7 +52,6 @@ pub external fn length(String) -> Int = "string" "length" /// /// > reverse("stressed") /// "desserts" -/// pub fn reverse(string: String) -> String { string |> iodata.new @@ -59,6 +59,7 @@ pub fn reverse(string: String) -> String { |> iodata.to_string } +/// /// Create a new string by replacing all occurrences of a given substring. /// /// ## Examples @@ -68,7 +69,6 @@ pub fn reverse(string: String) -> String { /// /// > replace("a,b,c,d,e", each: ",", with: "/") /// "a/b/c/d/e" -/// pub fn replace( in string: String, each pattern: String, @@ -76,10 +76,11 @@ pub fn replace( ) -> String { string |> iodata.new - |> iodata.replace(_, each: pattern, with: substitute) + |> iodata.replace(each: pattern, with: substitute) |> iodata.to_string } +/// /// Create a new string with all the graphemes in the input string converted to /// lowercase. /// @@ -89,9 +90,10 @@ pub fn replace( /// /// > lowercase("X-FILES") /// "x-files" -/// -pub external fn lowercase(String) -> String = "string" "lowercase" +pub external fn lowercase(String) -> String = + "string" "lowercase" +/// /// Create a new string with all the graphemes in the input string converted to /// uppercase. /// @@ -101,9 +103,10 @@ pub external fn lowercase(String) -> String = "string" "lowercase" /// /// > uppercase("skinner") /// "SKINNER" -/// -pub external fn uppercase(String) -> String = "string" "uppercase" +pub external fn uppercase(String) -> String = + "string" "uppercase" +/// /// Compares two strings to see which is "larger" by comparing their graphemes. /// /// This does not compare the size or length of the given strings. @@ -115,7 +118,6 @@ pub external fn uppercase(String) -> String = "string" "uppercase" /// /// > compare("A", "B") /// order.Gt -/// pub external fn compare(String, String) -> order.Order = "gleam_stdlib" "compare_strings" @@ -135,17 +137,15 @@ pub external fn compare(String, String) -> order.Order = // // // pub fn slice(out_of string: String, from start: Int, end: Int) -> String {} - // TODO -/// Drop *n* Graphemes from the left side of a -/// -/// ## Examples -/// > drop_left(from: "The Lone Gunmen", up_to: 2) -/// "e Lone Gunmen" -/// -/// +// Drop *n* Graphemes from the left side of a +// +// ## Examples +// > drop_left(from: "The Lone Gunmen", up_to: 2) +// "e Lone Gunmen" +// +// // pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String {} - // TODO // Drop *n* Graphemes from the right side of a // @@ -155,8 +155,7 @@ pub external fn compare(String, String) -> order.Order = // // // pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {} - - +/// /// Check if the first string contains the second. /// /// ## Examples @@ -169,12 +168,12 @@ pub external fn compare(String, String) -> order.Order = /// /// > contains(does: "theory", contain: "THE") /// False -/// external fn erl_contains(String, String) -> Bool = "gleam_stdlib" "string_contains" +/// pub fn contains(does haystack: String, contain needle: String) -> Bool { - erl_contains(haystack, needle) + erl_contains(haystack, needle) } // TODO @@ -187,7 +186,6 @@ pub fn contains(does haystack: String, contain needle: String) -> Bool { // // // pub fn starts_with(does string: String, start_with prefix: String) -> String {} - // TODO // TODO: Not sure about the name and labels here // See if the second string ends with the first one. @@ -198,22 +196,20 @@ pub fn contains(does haystack: String, contain needle: String) -> Bool { // // // pub fn ends_with(does string: String, end_with suffix: String) -> String {} - /// Create a list of strings by splitting a given string on a given substring. /// /// ## Examples /// /// > split("home/gleam/desktop/", on: "/") /// ["home","gleam","desktop", ""] -/// pub fn split(x: String, on substring: String) -> List(String) { x |> iodata.new - |> iodata.split(_, on: substring) - |> list.map(_, with: iodata.to_string) + |> iodata.split(on: substring) + |> list.map(with: iodata.to_string) } - +/// /// Create a new string by joining two strings together. /// /// This function copies both strings and runs in linear time. If you find @@ -224,14 +220,14 @@ pub fn split(x: String, on substring: String) -> List(String) { /// /// > append(to: "butter", suffix: "fly") /// "butterfly" -/// pub fn append(to first: String, suffix second: String) -> String { first |> iodata.new - |> iodata.append(_, second) + |> iodata.append(second) |> iodata.to_string } +/// /// Create a new string by joining many strings together. /// /// This function copies both strings and runs in linear time. If you find @@ -242,17 +238,17 @@ pub fn append(to first: String, suffix second: String) -> String { /// /// > concat(["never", "the", "less"]) /// "nevertheless" -/// pub fn concat(strings: List(String)) -> String { strings |> iodata.from_strings |> iodata.to_string } +/// fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String { case repeats <= 0 { - True -> concat(result) - False -> repeat_help(chunk, [chunk | result], repeats - 1) + True -> concat(result) + False -> repeat_help(chunk, [chunk, ..result], repeats - 1) } } @@ -263,11 +259,11 @@ fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String { /// ## Examples /// > repeat("ha", times: 3) /// "hahaha" -/// pub fn repeat(string: String, times times: Int) -> String { repeat_help(string, [], times) } +/// /// Join many strings together with a given separator. /// /// This function runs in linear time. @@ -276,14 +272,13 @@ pub fn repeat(string: String, times times: Int) -> String { /// /// > join(["home","evan","Desktop"], with: "/") /// "home/evan/Desktop" -/// pub fn join(strings: List(String), with separator: String) -> String { strings - |> list.intersperse(_, with: separator) + |> list.intersperse(with: separator) |> iodata.from_strings |> iodata.to_string } - +/// // TODO // Pad a string on the left until it has at least given number of Graphemes. // @@ -299,7 +294,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn pad_left(string: String, to size: Int, with: String) {} - // TODO // Pad a string on the right until it has a given length. // @@ -315,7 +309,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn pad_right(string: String, to size: Int, with: String) {} - // TODO // Get rid of whitespace on both sides of a String. // @@ -325,7 +318,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn trim(string: String) -> String {} - // TODO // Get rid of whitespace on the left of a String. // @@ -335,7 +327,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn trim_left(string: String) -> String {} - // TODO // Get rid of whitespace on the right of a String. // @@ -345,7 +336,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn trim_right(string: String) -> String {} - // TODO // /// Convert a string to a list of Graphemes. // /// @@ -354,7 +344,6 @@ pub fn join(strings: List(String), with separator: String) -> String { // // /// // pub fn to_graphemes(string: String) -> List(String) {} - // TODO // Split a non-empty string into its head and tail. This lets you // pattern match on strings exactly as you would with lists. |