diff options
author | Alex Rothuis <alex.rothuis@gmail.com> | 2022-11-01 01:03:48 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-11-06 16:45:54 +0000 |
commit | 610b56061c0687d3590a079639cbaee6ee9d746a (patch) | |
tree | 2864002c8c2a29c419f6df90cad1d90637595814 /src | |
parent | 7dc27b39843c31b1ac35845b83ec5c1ca7b15c1f (diff) | |
download | gleam_stdlib-610b56061c0687d3590a079639cbaee6ee9d746a.tar.gz gleam_stdlib-610b56061c0687d3590a079639cbaee6ee9d746a.zip |
Add operator functions
Allows using operators as functions. Useful in higher order functions or pipes.
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/bool.gleam | 48 | ||||
-rw-r--r-- | src/gleam/float.gleam | 72 | ||||
-rw-r--r-- | src/gleam/int.gleam | 72 |
3 files changed, 192 insertions, 0 deletions
diff --git a/src/gleam/bool.gleam b/src/gleam/bool.gleam index 557aabd..b888dd2 100644 --- a/src/gleam/bool.gleam +++ b/src/gleam/bool.gleam @@ -8,6 +8,54 @@ import gleam/order.{Order} +/// Returns the and of two bools. +/// +/// It's the function equivalent of the `&&` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > and(True, True) +/// True +/// +/// > and(False, True) +/// False +/// +/// > True |> and(_, True) +/// True +/// +/// > False |> and(True, _) +/// False +/// ``` +pub fn and(a: Bool, b: Bool) { + a && b +} + +/// Returns the or of two bools. +/// +/// It's the function equivalent of the `||` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > or(True, True) +/// True +/// +/// > or(False, True) +/// True +/// +/// > False |> or(_, False) +/// False +/// +/// > False |> or(True, _) +/// True +/// ``` +pub fn or(a: Bool, b: Bool) { + a || b +} + /// Returns the opposite bool value. /// /// This is the same as the `!` or `not` operators in some other languages. diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 992499a..c2a8705 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -436,3 +436,75 @@ pub fn divide(a: Float, by b: Float) -> Result(Float, Nil) { b -> Ok(a /. b) } } + +/// Adds two floats together. +/// +/// It's the function equivalent of the `+.` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > add(1.0, 2.0) +/// 3.0 +/// +/// > list.fold([1.0, 2.0, 3.0], 0.0, add) +/// 6.0 +/// +/// > 3.0 |> add(2.0) +/// 5.0 +/// ``` +pub fn add(a: Float, b: Float) -> Float { + a +. b +} + +/// Multiplies two floats together. +/// +/// It's the function equivalent of the `*.` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > multiply(2.0, 4.0) +/// 8.0 +/// +/// > list.fold([2.0, 3.0, 4.0], 1.0, multiply) +/// 24.0 +/// +/// > 3.0 |> multiply(2.0) +/// 6.0 +/// ``` +pub fn multiply(a: Float, b: Float) -> Float { + a *. b +} + +/// Subtracts a float subtrahend from a float minuend.gleam/base +/// +/// It's the function equivalent of the `-.` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > subtract(3.0, 1.0) +/// 2.0 +/// +/// > list.fold([1.0, 2.0, 3.0], 10.0, subtract) +/// 4.0 +/// +/// > 3.0 |> subtract(_, 2.0) +/// 1.0 +/// +/// > 3.0 |> subtract(2.0, _) +/// -1.0 +/// +/// > 3.0 |> subtract(subtrahend: 2.0) +/// 1.0 +/// +/// > 3.0 |> subtract(minuend: 2.0) +/// -1.0 +/// ``` +pub fn subtract(minuend a: Float, subtrahend b: Float) -> Float { + a -. b +} diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 66bdf45..651257d 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -652,3 +652,75 @@ pub fn floor_divide(dividend: Int, by divisor: Int) -> Result(Int, Nil) { } } } + +/// Adds two integers together. +/// +/// It's the function equivalent of the `+` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > add(1, 2) +/// 3 +/// +/// > list.fold([1, 2, 3], 0, add) +/// 6 +/// +/// > 3 |> add(2) +/// 5 +/// ``` +pub fn add(a: Int, b: Int) -> Int { + a + b +} + +/// Multiplies two integers together. +/// +/// It's the function equivalent of the `*` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > multiply(2, 4) +/// 8 +/// +/// > list.fold([2, 3, 4], 1, multiply) +/// 24 +/// +/// > 3 |> multiply(2) +/// 6 +/// ``` +pub fn multiply(a: Int, b: Int) -> Int { + a * b +} + +/// Subtracts an integer subtrahend from an integer minuend. +/// +/// It's the function equivalent of the `-` operator. +/// This function is useful in higher order functions or pipes. +/// +/// ## Examples +/// +/// ```gleam +/// > subtract(3, 1) +/// 2.0 +/// +/// > list.fold([1, 2, 3], 10, subtract) +/// 4 +/// +/// > 3 |> subtract(_, 2) +/// 1 +/// +/// > 3 |> subtract(2, _) +/// -1 +/// +/// > 3 |> subtract(subtrahend: 2) +/// 1 +/// +/// > 3 |> subtract(minuend: 2) +/// -1 +/// ``` +pub fn subtract(minuend a: Int, subtrahend b: Int) -> Int { + a - b +} |