diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/function.gleam | 12 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 38 |
3 files changed, 26 insertions, 26 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 983544f..b589c32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- `generic` module created with `identity`, `always`, `flip`, and `compose`. +- `function` module created with `compose` and `flip`. ## v0.6.0 - 2019-12-23 diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 35423c0..41ccf67 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -1,11 +1,11 @@ -// Takes a function that takes two arguments and returns a new function that -// takes the same two arguments, but in reverse order. -pub fn flip(fun: fn(a, b) -> c) -> fn(b, a) -> c { - fn(b, a) { fun(a, b) } -} - // Takes two functions and chains them together to form one function that takes // the input from the first and returns the output of the second. pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c { fn(a) { fun1(a) |> fun2 } } + +// Takes a function that takes two arguments and returns a new function that +// takes the same two arguments, but in reverse order. +pub fn flip(fun: fn(a, b) -> c) -> fn(b, a) -> c { + fn(b, a) { fun(a, b) } +} diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 86ef9a6..5b3ca42 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,28 +1,10 @@ import gleam/expect -import gleam/function.{flip, compose} +import gleam/function.{compose, flip} import gleam/int as int_mod import gleam/list import gleam/result import gleam/string as string_mod -pub fn flip_test() { - let fun = fn(string: String, int: Int) { - string - |> string_mod.append("String: '", _) - |> string_mod.append(_, "', Int: '") - |> string_mod.append(_, int_mod.to_string(int)) - |> string_mod.append(_, "'") - } - - let flipped_fun = flip(fun) - - fun("Bob", 1) - |> expect.equal(_, "String: 'Bob', Int: '1'") - - flipped_fun(2, "Alice") - |> expect.equal(_, "String: 'Alice', Int: '2'") -} - pub fn compose_test() { let add_two = fn(int: Int) { int + 2 } let add_three = fn(int: Int) { int + 3 } @@ -52,3 +34,21 @@ pub fn compose_test() { |> head_to_string |> expect.equal(_, "0") } + +pub fn flip_test() { + let fun = fn(string: String, int: Int) { + string + |> string_mod.append("String: '", _) + |> string_mod.append(_, "', Int: '") + |> string_mod.append(_, int_mod.to_string(int)) + |> string_mod.append(_, "'") + } + + let flipped_fun = flip(fun) + + fun("Bob", 1) + |> expect.equal(_, "String: 'Bob', Int: '1'") + + flipped_fun(2, "Alice") + |> expect.equal(_, "String: 'Alice', Int: '2'") +} |