aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Lévesque <contact@etiennel.dev>2022-10-20 21:05:30 -0400
committerLouis Pilfold <louis@lpil.uk>2022-10-25 14:49:22 +0100
commited7bd3774c46d374c0a466cc0a946fa96fe2003f (patch)
tree26b0971c45b6e45769114acbde8c858b46148a1c
parenta0c6b16febffa0c52293016fc82dd23610f23009 (diff)
downloadgleam_stdlib-ed7bd3774c46d374c0a466cc0a946fa96fe2003f.tar.gz
gleam_stdlib-ed7bd3774c46d374c0a466cc0a946fa96fe2003f.zip
add function.applyX
-rw-r--r--src/gleam/function.gleam93
-rw-r--r--test/gleam/function_test.gleam48
2 files changed, 141 insertions, 0 deletions
diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam
index 377518f..ee472d8 100644
--- a/src/gleam/function.gleam
+++ b/src/gleam/function.gleam
@@ -72,3 +72,96 @@ pub fn tap(arg: a, effect: fn(a) -> b) -> a {
effect(arg)
arg
}
+
+/// Takes a function with arity one and an argument,
+/// calls that function with the argument and returns the function return value.
+///
+/// Useful for concisely calling functions returned as a part of a pipeline.
+///
+/// ## Example
+///
+/// ```gleam
+/// fn get_doubler() {
+/// fn(x: int) { x * 2 }
+/// }
+///
+/// fn use_apply() {
+/// get_doubler()
+/// |> function.apply1(2)
+/// |> should.equal(4)
+/// }
+///
+pub fn apply1(fun: fn(a) -> value, arg1: a) -> value {
+ fun(arg1)
+}
+
+/// Takes a function with arity two and two arguments,
+/// calls that function with the arguments
+/// and returns the function return value.
+///
+/// See `apply1` for more details
+///
+pub fn apply2(fun: fn(a, b) -> value, arg1: a, arg2: b) -> value {
+ fun(arg1, arg2)
+}
+
+/// Takes a function with arity three and three arguments,
+/// calls that function with the arguments
+/// and returns the function return value.
+///
+/// See `apply1` for more details
+///
+pub fn apply3(fun: fn(a, b, c) -> value, arg1: a, arg2: b, arg3: c) -> value {
+ fun(arg1, arg2, arg3)
+}
+
+/// Takes a function with arity four and four arguments,
+/// calls that function with the arguments
+/// and returns the function return value.
+///
+/// See `apply1` for more details
+///
+pub fn apply4(
+ fun: fn(a, b, c, d) -> value,
+ arg1: a,
+ arg2: b,
+ arg3: c,
+ arg4: d,
+) -> value {
+ fun(arg1, arg2, arg3, arg4)
+}
+
+/// Takes a function with arity five and five arguments,
+/// calls that function with the arguments
+/// and returns the function return value.
+///
+/// See `apply1` for more details
+///
+pub fn apply5(
+ fun: fn(a, b, c, d, e) -> value,
+ arg1: a,
+ arg2: b,
+ arg3: c,
+ arg4: d,
+ arg5: e,
+) -> value {
+ fun(arg1, arg2, arg3, arg4, arg5)
+}
+
+/// Takes a function with arity six and six arguments,
+/// calls that function with the arguments
+/// and returns the function return value.
+///
+/// See `apply1` for more details
+///
+pub fn apply6(
+ fun: fn(a, b, c, d, e, f) -> value,
+ arg1: a,
+ arg2: b,
+ arg3: c,
+ arg4: d,
+ arg5: e,
+ arg6: f,
+) -> value {
+ fun(arg1, arg2, arg3, arg4, arg5, arg6)
+}
diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam
index eff4dea..d481893 100644
--- a/test/gleam/function_test.gleam
+++ b/test/gleam/function_test.gleam
@@ -122,3 +122,51 @@ pub fn tap_test() {
})
|> should.equal("Thanks Joe & Louis")
}
+
+pub fn apply1_test() {
+ let fun = fn(x1) { x1 }
+
+ fun
+ |> function.apply1(1)
+ |> should.equal(1)
+}
+
+pub fn apply2_test() {
+ let fun = fn(x1, x2) { x1 + x2 }
+
+ fun
+ |> function.apply2(1, 2)
+ |> should.equal(3)
+}
+
+pub fn apply3_test() {
+ let fun = fn(x1, x2, x3) { x1 + x2 + x3 }
+
+ fun
+ |> function.apply3(1, 2, 3)
+ |> should.equal(6)
+}
+
+pub fn apply4_test() {
+ let fun = fn(x1, x2, x3, x4) { x1 + x2 + x3 + x4 }
+
+ fun
+ |> function.apply4(1, 2, 3, 4)
+ |> should.equal(10)
+}
+
+pub fn apply5_test() {
+ let fun = fn(x1, x2, x3, x4, x5) { x1 + x2 + x3 + x4 + x5 }
+
+ fun
+ |> function.apply5(1, 2, 3, 4, 5)
+ |> should.equal(15)
+}
+
+pub fn apply6_test() {
+ let fun = fn(x1, x2, x3, x4, x5, x6) { x1 + x2 + x3 + x4 + x5 + x6 }
+
+ fun
+ |> function.apply6(1, 2, 3, 4, 5, 6)
+ |> should.equal(21)
+}