diff options
author | Tristan Cacqueray <tdecacqu@redhat.com> | 2021-12-06 22:23:03 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-12-09 17:58:06 +0000 |
commit | 3599e3c29c16c2cfd12c76a5dcbe6d55e9f396d3 (patch) | |
tree | 5a2d95b0083e4857ba50c7a504093f92ecc50699 | |
parent | 88a9c8a5c0230827ec04b137994dd0e310515a94 (diff) | |
download | gleam_stdlib-3599e3c29c16c2cfd12c76a5dcbe6d55e9f396d3.tar.gz gleam_stdlib-3599e3c29c16c2cfd12c76a5dcbe6d55e9f396d3.zip |
Add function.constant
This change adds a new helper to the function module.
-rw-r--r-- | src/gleam/function.gleam | 7 | ||||
-rw-r--r-- | test/gleam/function_test.gleam | 7 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index a6d2063..5aebb76 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -51,3 +51,10 @@ pub fn flip(fun: fn(a, b) -> c) -> fn(b, a) -> c { pub fn identity(x: a) -> a { x } + +/// A function that takes a single argument and returns a new function that +/// ignores its argument and always returns the input value. +/// +pub fn constant(value: a) -> fn(b) -> a { + fn(_) { value } +} diff --git a/test/gleam/function_test.gleam b/test/gleam/function_test.gleam index e86beb3..1495d2a 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -2,6 +2,7 @@ import gleam/should import gleam/dynamic import gleam/function import gleam/int +import gleam/pair import gleam/list import gleam/result import gleam/string @@ -107,3 +108,9 @@ pub fn identity_test() { |> function.identity |> should.equal(#(1, 2.0)) } + +pub fn always_test() { + #(1, 2) + |> pair.map_first(function.constant(42)) + |> should.equal(#(42, 2)) +} |