diff options
author | Louis Pilfold <louis@lpil.uk> | 2020-01-05 18:28:04 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-01-05 18:33:28 +0000 |
commit | 330ea699f3c20519c174c2299215737cef5f4cbf (patch) | |
tree | a4afa9628640ac993a8cbb5eb643b04e13422969 | |
parent | debc834f87f3b5df7057dc924f577dd57d0fee8c (diff) | |
download | gleam_stdlib-330ea699f3c20519c174c2299215737cef5f4cbf.tar.gz gleam_stdlib-330ea699f3c20519c174c2299215737cef5f4cbf.zip |
function.identity
-rw-r--r-- | gen/src/gleam@function.erl | 5 | ||||
-rw-r--r-- | gen/test/gleam@function_test.erl | 23 | ||||
-rw-r--r-- | src/gleam/function.gleam | 5 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 50 |
4 files changed, 56 insertions, 27 deletions
diff --git a/gen/src/gleam@function.erl b/gen/src/gleam@function.erl index f014d37..b376ed0 100644 --- a/gen/src/gleam@function.erl +++ b/gen/src/gleam@function.erl @@ -1,10 +1,13 @@ -module(gleam@function). -compile(no_auto_import). --export([compose/2, flip/1]). +-export([compose/2, flip/1, identity/1]). compose(Fun1, Fun2) -> fun(A) -> Fun2(Fun1(A)) end. flip(Fun) -> fun(B, A) -> Fun(A, B) end. + +identity(X) -> + X. diff --git a/gen/test/gleam@function_test.erl b/gen/test/gleam@function_test.erl index 47758c5..58f6a57 100644 --- a/gen/test/gleam@function_test.erl +++ b/gen/test/gleam@function_test.erl @@ -1,7 +1,7 @@ -module(gleam@function_test). -compile(no_auto_import). --export([compose_test/0, flip_test/0]). +-export([compose_test/0, flip_test/0, identity_test/0]). compose_test() -> AddTwo = fun(Int) -> Int + 2 end, @@ -9,23 +9,24 @@ compose_test() -> AddFive = gleam@function:compose(AddTwo, AddThree), gleam@expect:equal(AddFive(1), 6), HeadToString = gleam@function:compose( - fun gleam@list:head/1, - fun(IntResult) -> - gleam@int:to_string(gleam@result:unwrap(IntResult, 0)) - end + gleam@function:compose( + fun gleam@list:head/1, + fun(Capture1) -> gleam@result:unwrap(Capture1, 0) end + ), + fun gleam@int:to_string/1 ), gleam@expect:equal(HeadToString([1]), <<"1">>), gleam@expect:equal(HeadToString([]), <<"0">>). flip_test() -> - Fun = fun(String, Int) -> + Fun = fun(S, I) -> gleam@string:append( gleam@string:append( gleam@string:append( - gleam@string:append(<<"String: '">>, String), + gleam@string:append(<<"String: '">>, S), <<"', Int: '">> ), - gleam@int:to_string(Int) + gleam@int:to_string(I) ), <<"'">> ) @@ -36,3 +37,9 @@ flip_test() -> FlippedFun(2, <<"Alice">>), <<"String: 'Alice', Int: '2'">> ). + +identity_test() -> + gleam@expect:equal(gleam@function:identity(1), 1), + gleam@expect:equal(gleam@function:identity(<<"">>), <<"">>), + gleam@expect:equal(gleam@function:identity([]), []), + gleam@expect:equal(gleam@function:identity({1, 2.0}), {1, 2.0}). diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 41ccf67..68fc619 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -9,3 +9,8 @@ pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c { pub fn flip(fun: fn(a, b) -> c) -> fn(b, a) -> c { fn(b, a) { fun(a, b) } } + +// A function that always returns its input value. +pub fn identity(x: a) -> a { + x +} diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index 5b3ca42..1c3b4b8 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,15 +1,15 @@ import gleam/expect -import gleam/function.{compose, flip} -import gleam/int as int_mod +import gleam/function +import gleam/int import gleam/list import gleam/result -import gleam/string as string_mod +import gleam/string 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) + let add_five = function.compose(add_two, add_three) 1 |> add_five @@ -18,13 +18,9 @@ pub fn compose_test() { // 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 - } - ) + list.head + |> function.compose(_, result.unwrap(_, 0)) + |> function.compose(_, int.to_string) [1] |> head_to_string @@ -36,15 +32,15 @@ pub fn compose_test() { } 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 fun = fn(s: String, i: Int) { + s + |> string.append("String: '", _) + |> string.append(_, "', Int: '") + |> string.append(_, int.to_string(i)) + |> string.append(_, "'") } - let flipped_fun = flip(fun) + let flipped_fun = function.flip(fun) fun("Bob", 1) |> expect.equal(_, "String: 'Bob', Int: '1'") @@ -52,3 +48,21 @@ pub fn flip_test() { flipped_fun(2, "Alice") |> expect.equal(_, "String: 'Alice', Int: '2'") } + +pub fn identity_test() { + 1 + |> function.identity + |> expect.equal(_, 1) + + "" + |> function.identity + |> expect.equal(_, "") + + [] + |> function.identity + |> expect.equal(_, []) + + tuple(1, 2.0) + |> function.identity + |> expect.equal(_, tuple(1, 2.0)) +} |