From 10af1307dddc5cb89596c92d3fbb5a8c4491eec1 Mon Sep 17 00:00:00 2001 From: RJ Dellecese Date: Mon, 30 Dec 2019 14:06:52 -0500 Subject: Rename generic module as function module --- src/gleam/function.gleam | 11 +++++++++ src/gleam/generic.gleam | 11 --------- test/gleam/function_test.gleam | 54 ++++++++++++++++++++++++++++++++++++++++++ test/gleam/generic_test.gleam | 54 ------------------------------------------ 4 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 src/gleam/function.gleam delete mode 100644 src/gleam/generic.gleam create mode 100644 test/gleam/function_test.gleam delete mode 100644 test/gleam/generic_test.gleam diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam new file mode 100644 index 0000000..35423c0 --- /dev/null +++ b/src/gleam/function.gleam @@ -0,0 +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 } +} diff --git a/src/gleam/generic.gleam b/src/gleam/generic.gleam deleted file mode 100644 index 35423c0..0000000 --- a/src/gleam/generic.gleam +++ /dev/null @@ -1,11 +0,0 @@ -// 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 } -} diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam new file mode 100644 index 0000000..86ef9a6 --- /dev/null +++ b/test/gleam/function_test.gleam @@ -0,0 +1,54 @@ +import gleam/expect +import gleam/function.{flip, compose} +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 } + + let add_five = compose(add_two, add_three) + + 1 + |> add_five + |> expect.equal(_, 6) + + // Takes a list of ints and returns the head as a string (if there is one, or + // else "0" if there is not) + let head_to_string = + compose( + list.head, + fn(int_result: Result(Int, Nil)) { + result.unwrap(int_result, 0) + |> int_mod.to_string + } + ) + + [1] + |> head_to_string + |> expect.equal(_, "1") + + [] + |> head_to_string + |> expect.equal(_, "0") +} diff --git a/test/gleam/generic_test.gleam b/test/gleam/generic_test.gleam deleted file mode 100644 index 7088edb..0000000 --- a/test/gleam/generic_test.gleam +++ /dev/null @@ -1,54 +0,0 @@ -import gleam/expect -import gleam/generic.{flip, compose} -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 } - - let add_five = compose(add_two, add_three) - - 1 - |> add_five - |> expect.equal(_, 6) - - // Takes a list of ints and returns the head as a string (if there is one, or - // else "0" if there is not) - let head_to_string = - compose( - list.head, - fn(int_result: Result(Int, Nil)) { - result.unwrap(int_result, 0) - |> int_mod.to_string - } - ) - - [1] - |> head_to_string - |> expect.equal(_, "1") - - [] - |> head_to_string - |> expect.equal(_, "0") -} -- cgit v1.2.3