aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Rothuis <alex.rothuis@gmail.com>2022-11-01 01:03:48 +0100
committerLouis Pilfold <louis@lpil.uk>2022-11-06 16:45:54 +0000
commit610b56061c0687d3590a079639cbaee6ee9d746a (patch)
tree2864002c8c2a29c419f6df90cad1d90637595814
parent7dc27b39843c31b1ac35845b83ec5c1ca7b15c1f (diff)
downloadgleam_stdlib-610b56061c0687d3590a079639cbaee6ee9d746a.tar.gz
gleam_stdlib-610b56061c0687d3590a079639cbaee6ee9d746a.zip
Add operator functions
Allows using operators as functions. Useful in higher order functions or pipes.
-rw-r--r--src/gleam/bool.gleam48
-rw-r--r--src/gleam/float.gleam72
-rw-r--r--src/gleam/int.gleam72
-rw-r--r--test/gleam/bool_test.gleam28
-rw-r--r--test/gleam/float_test.gleam42
-rw-r--r--test/gleam/int_test.gleam42
6 files changed, 304 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
+}
diff --git a/test/gleam/bool_test.gleam b/test/gleam/bool_test.gleam
index 24607fb..e4dddd9 100644
--- a/test/gleam/bool_test.gleam
+++ b/test/gleam/bool_test.gleam
@@ -2,6 +2,34 @@ import gleam/bool
import gleam/order
import gleam/should
+pub fn and_test() {
+ bool.and(True, True)
+ |> should.be_true
+
+ bool.and(False, True)
+ |> should.be_false
+
+ True |> bool.and(_, True)
+ |> should.be_true
+
+ False |> bool.and(True, _)
+ |> should.be_false
+}
+
+pub fn or_test() {
+ bool.or(True, True)
+ |> should.be_true
+
+ bool.or(False, True)
+ |> should.be_true
+
+ True |> bool.or(_, False)
+ |> should.be_true
+
+ False |> bool.or(True, _)
+ |> should.be_true
+}
+
pub fn negate_test() {
bool.negate(True)
|> should.be_false
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index e952908..ff11dff 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -390,3 +390,45 @@ pub fn divide_test() {
float.divide(1.0, by: 0.0)
|> should.equal(Error(Nil))
}
+
+pub fn add_test() {
+ float.add(1.0, 2.0)
+ |> should.equal(3.0)
+
+ list.fold([1.0, 2.0, 3.0], 0.0, float.add)
+ |> should.equal(6.0)
+
+ 3.0 |> float.add(2.0)
+ |> should.equal(5.0)
+}
+
+pub fn multiply_test() {
+ float.multiply(2.0, 4.0)
+ |> should.equal(8.0)
+
+ list.fold([2.0, 3.0, 4.0], 1.0, float.multiply)
+ |> should.equal(24.0)
+
+ 3.0 |> float.multiply(2.0)
+ |> should.equal(6.0)
+}
+
+pub fn subtract_test() {
+ float.subtract(3.0, 1.0)
+ |> should.equal(2.0)
+
+ list.fold([1.0, 2.0, 3.0], 10.0, float.subtract)
+ |> should.equal(4.0)
+
+ 3.0 |> float.subtract(_, 2.0)
+ |> should.equal(1.0)
+
+ 3.0 |> float.subtract(2.0, _)
+ |> should.equal(-1.0)
+
+ 3.0 |> float.subtract(subtrahend: 2.0)
+ |> should.equal(1.0)
+
+ 3.0 |> float.subtract(minuend: 2.0)
+ |> should.equal(-1.0)
+}
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 87de970..90f7e8d 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -500,3 +500,45 @@ pub fn floor_divide_test() {
int.floor_divide(-99, by: 2)
|> should.equal(Ok(-50))
}
+
+pub fn add_test() {
+ int.add(1, 2)
+ |> should.equal(3)
+
+ list.fold([1, 2, 3], 0, int.add)
+ |> should.equal(6)
+
+ 3 |> int.add(2)
+ |> should.equal(5)
+}
+
+pub fn multiply_test() {
+ int.multiply(2, 4)
+ |> should.equal(8)
+
+ list.fold([2, 3, 4], 1, int.multiply)
+ |> should.equal(24)
+
+ 3 |> int.multiply(2)
+ |> should.equal(6)
+}
+
+pub fn subtract_test() {
+ int.subtract(3, 1)
+ |> should.equal(2)
+
+ list.fold([1, 2, 3], 10, int.subtract)
+ |> should.equal(4)
+
+ 3 |> int.subtract(_, 2)
+ |> should.equal(1)
+
+ 3 |> int.subtract(2, _)
+ |> should.equal(-1)
+
+ 3 |> int.subtract(subtrahend: 2)
+ |> should.equal(1)
+
+ 3 |> int.subtract(minuend: 2)
+ |> should.equal(-1)
+}