diff options
author | Sebastian Porto <s@porto5.com> | 2020-10-18 12:38:27 +1100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-10-18 15:56:20 +0100 |
commit | 5239830bc8bf6d8c1df566e7bfe995ff24d8514a (patch) | |
tree | bed6a42451e0f8fcccefa21a40dce9350ff62d00 | |
parent | 1489e38aaa076112fe580cd096e4c827304d2bd3 (diff) | |
download | gleam_stdlib-5239830bc8bf6d8c1df566e7bfe995ff24d8514a.tar.gz gleam_stdlib-5239830bc8bf6d8c1df566e7bfe995ff24d8514a.zip |
Add function.curryN
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/function.gleam | 34 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 40 |
3 files changed, 75 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c67f7..1956083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- The `function` module gains `curry2` to `curry6`. - The `list` module gains the `each`, and `partition` functions. - The `int` and `float` modules gain the `negate` function. - The `result` module gains the `all` function. diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 70da466..08e7efd 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -7,6 +7,40 @@ pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c { fn(a) { fun2(fun1(a)) } } +/// Takes a function with arity two +/// and returns a curried equivalent. +/// fn(a, b) -> c becomes fn(a) -> fn(b) -> c +pub fn curry2(fun: fn(a, b) -> value) { + fn(a) { fn(b) { fun(a, b) } } +} + +/// Takes a function with arity three +/// and returns a curried equivalent. +/// fn(a, b, c) -> d becomes fn(a) -> fn(b) -> fn(c) -> d +pub fn curry3(fun: fn(a, b, c) -> value) { + fn(a) { fn(b) { fn(c) { fun(a, b, c) } } } +} + +/// Takes a function with arity four +/// and returns a curried equivalent. +pub fn curry4(fun: fn(a, b, c, d) -> value) { + fn(a) { fn(b) { fn(c) { fn(d) { fun(a, b, c, d) } } } } +} + +/// Takes a function with arity five +/// and returns a curried equivalent. +pub fn curry5(fun: fn(a, b, c, d, e) -> value) { + fn(a) { fn(b) { fn(c) { fn(d) { fn(e) { fun(a, b, c, d, e) } } } } } +} + +/// Takes a function with arity six +/// and returns a curried equivalent. +pub fn curry6(fun: fn(a, b, c, d, e, f) -> value) { + fn(a) { + fn(b) { fn(c) { fn(d) { fn(e) { fn(f) { fun(a, b, c, d, e, f) } } } } } + } +} + /// Takes a function that takes two arguments and returns a new function that /// takes the same two arguments, but in reverse order. /// diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 9aca032..c9ef8ad 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -32,6 +32,46 @@ pub fn compose_test() { |> should.equal("0") } +pub fn curry2_test() { + let fun = fn(a, b) { a + b } + let curried = function.curry2(fun) + + curried(1)(2) + |> should.equal(3) +} + +pub fn curry3_test() { + let fun = fn(a, b, c) { a + b + c } + let curried = function.curry3(fun) + + curried(1)(2)(4) + |> should.equal(7) +} + +pub fn curry4_test() { + let fun = fn(a, b, c, d) { a + b + c + d } + let curried = function.curry4(fun) + + curried(1)(2)(4)(8) + |> should.equal(15) +} + +pub fn curry5_test() { + let fun = fn(a, b, c, d, e) { a + b + c + d + e } + let curried = function.curry5(fun) + + curried(1)(2)(4)(8)(16) + |> should.equal(31) +} + +pub fn curry6_test() { + let fun = fn(a, b, c, d, e, f) { a + b + c + d + e + f } + let curried = function.curry6(fun) + + curried(1)(2)(4)(8)(16)(32) + |> should.equal(63) +} + pub fn flip_test() { let fun = fn(s: String, i: Int) { s |