diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/bool.gleam | 372 | ||||
-rw-r--r-- | src/gleam/order.gleam | 178 |
2 files changed, 273 insertions, 277 deletions
diff --git a/src/gleam/bool.gleam b/src/gleam/bool.gleam index e25f7b5..684aa2f 100644 --- a/src/gleam/bool.gleam +++ b/src/gleam/bool.gleam @@ -1,207 +1,205 @@ -if erlang { - import gleam/order.{Order} +import gleam/order.{Order} - /// A type with two possible values, True and False. Used to indicate whether - /// things are... true or false! - /// - /// Often is it clearer and offers more type safety to define a custom type - /// than to use Bool. For example, rather than having a `is_teacher: Bool` - /// field consider having a `role: SchoolRole` field where SchoolRole is a custom - /// type that can be either Student or Teacher. - /// - pub type Bool = - Bool +/// A type with two possible values, True and False. Used to indicate whether +/// things are... true or false! +/// +/// Often is it clearer and offers more type safety to define a custom type +/// than to use Bool. For example, rather than having a `is_teacher: Bool` +/// field consider having a `role: SchoolRole` field where SchoolRole is a custom +/// type that can be either Student or Teacher. +/// +pub type Bool = + Bool - /// Returns the opposite bool value. - /// - /// This is the same as the `!` or `not` operators in some other languages. - /// - /// ## Examples - /// - /// > negate(True) - /// False - /// - /// > negate(False) - /// True - /// - pub fn negate(bool: Bool) -> Bool { - case bool { - True -> False - False -> True - } +/// Returns the opposite bool value. +/// +/// This is the same as the `!` or `not` operators in some other languages. +/// +/// ## Examples +/// +/// > negate(True) +/// False +/// +/// > negate(False) +/// True +/// +pub fn negate(bool: Bool) -> Bool { + case bool { + True -> False + False -> True } +} - /// Returns the nor of two bools - /// - /// ## Examples - /// - /// > nor(False, False) - /// True - /// - /// > nor(False, True) - /// False - /// - /// > nor(True, False) - /// False - /// - /// > nor(True, True) - /// False - /// - pub fn nor(a: Bool, b: Bool) -> Bool { - case a, b { - False, False -> True - False, True -> False - True, False -> False - True, True -> False - } +/// Returns the nor of two bools +/// +/// ## Examples +/// +/// > nor(False, False) +/// True +/// +/// > nor(False, True) +/// False +/// +/// > nor(True, False) +/// False +/// +/// > nor(True, True) +/// False +/// +pub fn nor(a: Bool, b: Bool) -> Bool { + case a, b { + False, False -> True + False, True -> False + True, False -> False + True, True -> False } +} - /// Returns the nand of two bools - /// - /// ## Examples - /// - /// > nand(False, False) - /// True - /// - /// > nand(False, True) - /// True - /// - /// > nand(True, False) - /// True - /// - /// > nand(True, True) - /// False - /// - pub fn nand(a: Bool, b: Bool) -> Bool { - case a, b { - False, False -> True - False, True -> True - True, False -> True - True, True -> False - } +/// Returns the nand of two bools +/// +/// ## Examples +/// +/// > nand(False, False) +/// True +/// +/// > nand(False, True) +/// True +/// +/// > nand(True, False) +/// True +/// +/// > nand(True, True) +/// False +/// +pub fn nand(a: Bool, b: Bool) -> Bool { + case a, b { + False, False -> True + False, True -> True + True, False -> True + True, True -> False } +} - /// Returns the exclusive or of two bools - /// - /// ## Examples - /// - /// > exclusive_or(False, False) - /// False - /// - /// > exclusive_or(False, True) - /// True - /// - /// > exclusive_or(True, False) - /// True - /// - /// > exclusive_or(True, True) - /// False - /// - pub fn exclusive_or(a: Bool, b: Bool) -> Bool { - case a, b { - False, False -> False - False, True -> True - True, False -> True - True, True -> False - } +/// Returns the exclusive or of two bools +/// +/// ## Examples +/// +/// > exclusive_or(False, False) +/// False +/// +/// > exclusive_or(False, True) +/// True +/// +/// > exclusive_or(True, False) +/// True +/// +/// > exclusive_or(True, True) +/// False +/// +pub fn exclusive_or(a: Bool, b: Bool) -> Bool { + case a, b { + False, False -> False + False, True -> True + True, False -> True + True, True -> False } +} - /// Returns the exclusive nor of two bools - /// - /// ## Examples - /// - /// > exclusive_nor(False, False) - /// True - /// - /// > exclusive_nor(False, True) - /// False - /// - /// > exclusive_nor(True, False) - /// False - /// - /// > exclusive_nor(True, True) - /// True - /// - pub fn exclusive_nor(a: Bool, b: Bool) -> Bool { - case a, b { - False, False -> True - False, True -> False - True, False -> False - True, True -> True - } +/// Returns the exclusive nor of two bools +/// +/// ## Examples +/// +/// > exclusive_nor(False, False) +/// True +/// +/// > exclusive_nor(False, True) +/// False +/// +/// > exclusive_nor(True, False) +/// False +/// +/// > exclusive_nor(True, True) +/// True +/// +pub fn exclusive_nor(a: Bool, b: Bool) -> Bool { + case a, b { + False, False -> True + False, True -> False + True, False -> False + True, True -> True } +} - /// Compares two bools and returns the first values Order to the second. - /// - /// ## Examples - /// - /// > import gleam/order - /// > compare(True, False) - /// order.Gt - /// - pub fn compare(a: Bool, with b: Bool) -> Order { - case a, b { - True, True -> order.Eq - True, False -> order.Gt - False, False -> order.Eq - False, True -> order.Lt - } +/// Compares two bools and returns the first values Order to the second. +/// +/// ## Examples +/// +/// > import gleam/order +/// > compare(True, False) +/// order.Gt +/// +pub fn compare(a: Bool, with b: Bool) -> Order { + case a, b { + True, True -> order.Eq + True, False -> order.Gt + False, False -> order.Eq + False, True -> order.Lt } +} - /// Returns True if either bool value is True. - /// - /// ## Examples - /// - /// > max(True, False) - /// True - /// - /// > max(False, True) - /// True - /// - /// > max(False, False) - /// False - /// - pub fn max(a: Bool, b: Bool) -> Bool { - case a { - True -> True - False -> b - } +/// Returns True if either bool value is True. +/// +/// ## Examples +/// +/// > max(True, False) +/// True +/// +/// > max(False, True) +/// True +/// +/// > max(False, False) +/// False +/// +pub fn max(a: Bool, b: Bool) -> Bool { + case a { + True -> True + False -> b } +} - /// Returns False if either bool value is False. - /// - /// ## Examples - /// - /// > max(True, False) - /// False - /// - /// > max(False, True) - /// False - /// - /// > max(False, False) - /// False - /// - pub fn min(a: Bool, b: Bool) -> Bool { - case a { - False -> False - True -> b - } +/// Returns False if either bool value is False. +/// +/// ## Examples +/// +/// > max(True, False) +/// False +/// +/// > max(False, True) +/// False +/// +/// > max(False, False) +/// False +/// +pub fn min(a: Bool, b: Bool) -> Bool { + case a { + False -> False + True -> b } +} - /// Returns a numeric representation of the given bool. - /// - /// ## Examples - /// - /// > to_int(True) - /// 1 - /// - /// > to_int(False) - /// 0 - /// - pub fn to_int(bool: Bool) -> Int { - case bool { - False -> 0 - True -> 1 - } +/// Returns a numeric representation of the given bool. +/// +/// ## Examples +/// +/// > to_int(True) +/// 1 +/// +/// > to_int(False) +/// 0 +/// +pub fn to_int(bool: Bool) -> Int { + case bool { + False -> 0 + True -> 1 } } diff --git a/src/gleam/order.gleam b/src/gleam/order.gleam index ee81a62..c030db4 100644 --- a/src/gleam/order.gleam +++ b/src/gleam/order.gleam @@ -1,103 +1,101 @@ -if erlang { - /// Represents the result of a single comparison to determine the precise - /// ordering of two values. - /// - pub type Order { - /// Less-than - Lt +/// Represents the result of a single comparison to determine the precise +/// ordering of two values. +/// +pub type Order { + /// Less-than + Lt - /// Equal - Eq + /// Equal + Eq - /// Greater than - Gt - } + /// Greater than + Gt +} - /// Inverts an order, so less-than becomes greater-than and greater-than - /// becomes less-than. - /// - /// ## Examples - /// - /// > reverse(Lt) - /// Gt - /// - /// > reverse(Eq) - /// Eq - /// - /// > reverse(Lt) - /// Gt - /// - pub fn reverse(order: Order) -> Order { - case order { - Lt -> Gt - Eq -> Eq - Gt -> Lt - } +/// Inverts an order, so less-than becomes greater-than and greater-than +/// becomes less-than. +/// +/// ## Examples +/// +/// > reverse(Lt) +/// Gt +/// +/// > reverse(Eq) +/// Eq +/// +/// > reverse(Lt) +/// Gt +/// +pub fn reverse(order: Order) -> Order { + case order { + Lt -> Gt + Eq -> Eq + Gt -> Lt } +} - /// Produces a numeric representation of the order. - /// - /// ## Examples - /// - /// > to_int(Lt) - /// -1 - /// - /// > to_int(Eq) - /// 0 - /// - /// > to_int(Gt) - /// 1 - /// - pub fn to_int(order: Order) -> Int { - case order { - Lt -> -1 - Eq -> 0 - Gt -> 1 - } +/// Produces a numeric representation of the order. +/// +/// ## Examples +/// +/// > to_int(Lt) +/// -1 +/// +/// > to_int(Eq) +/// 0 +/// +/// > to_int(Gt) +/// 1 +/// +pub fn to_int(order: Order) -> Int { + case order { + Lt -> -1 + Eq -> 0 + Gt -> 1 } +} - /// Compares two Order values to one another, producing a new Order. - /// - /// ## Examples - /// - /// > compare(Eq, with: Lt) - /// Gt - /// - pub fn compare(a: Order, with b: Order) -> Order { - case a, b { - x, y if x == y -> Eq - Lt, _ | Eq, Gt -> Lt - _, _ -> Gt - } +/// Compares two Order values to one another, producing a new Order. +/// +/// ## Examples +/// +/// > compare(Eq, with: Lt) +/// Gt +/// +pub fn compare(a: Order, with b: Order) -> Order { + case a, b { + x, y if x == y -> Eq + Lt, _ | Eq, Gt -> Lt + _, _ -> Gt } +} - /// Returns the largest of two orders. - /// - /// ## Examples - /// - /// > max(Eq, Lt) - /// Eq - /// - pub fn max(a: Order, b: Order) -> Order { - case a, b { - Gt, _ -> Gt - Eq, Lt -> Eq - _, _ -> b - } +/// Returns the largest of two orders. +/// +/// ## Examples +/// +/// > max(Eq, Lt) +/// Eq +/// +pub fn max(a: Order, b: Order) -> Order { + case a, b { + Gt, _ -> Gt + Eq, Lt -> Eq + _, _ -> b } +} - /// Returns the smallest of two orders. - /// - /// ## Examples - /// - /// > min(Eq, Lt) - /// Lt - /// - pub fn min(a: Order, b: Order) -> Order { - case a, b { - Lt, _ -> Lt - Eq, Gt -> Eq - _, _ -> b - } +/// Returns the smallest of two orders. +/// +/// ## Examples +/// +/// > min(Eq, Lt) +/// Lt +/// +pub fn min(a: Order, b: Order) -> Order { + case a, b { + Lt, _ -> Lt + Eq, Gt -> Eq + _, _ -> b } } |