From 6a701d1b1943891ee866e22c5d84800ade8c09f1 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Thu, 22 Feb 2024 14:08:30 +0000 Subject: [PATCH] Deprecate constant, compose --- CHANGELOG.md | 5 +++++ src/gleam/function.gleam | 8 ++------ test/gleam/function_test.gleam | 34 ---------------------------------- 3 files changed, 7 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 846dc7ed..af7ed730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- The `compose` and `constant` functions in the `function` module have been + deprecated in favour of the `fn` literal syntax. + ## v0.35.1 - 2024-02-15 - Fixed a warning on the JavaScript target. diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 180b6b21..6f424557 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -1,6 +1,4 @@ -/// 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. -/// +@deprecated("Use a fn literal instead, it is easier to understand") pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c { fn(a) { fun2(fun1(a)) } } @@ -103,9 +101,7 @@ pub fn identity(x: a) -> a { x } -/// Takes a single argument and returns a new function that -/// ignores its argument and always returns the input value. -/// +@deprecated("Use a fn literal instead, it is easier to understand") 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 26919f26..ce3c08fa 100644 --- a/test/gleam/function_test.gleam +++ b/test/gleam/function_test.gleam @@ -1,37 +1,9 @@ import gleam/function import gleam/int -import gleam/list import gleam/pair -import gleam/result import gleam/should 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 = function.compose(add_two, add_three) - - 1 - |> add_five - |> should.equal(6) - - // Takes a list of ints and returns the first as a string (if there is one, or - // else "0" if there is not) - let first_to_string = - list.first - |> function.compose(result.unwrap(_, 0)) - |> function.compose(int.to_string) - - [1] - |> first_to_string - |> should.equal("1") - - [] - |> first_to_string - |> should.equal("0") -} - pub fn curry2_test() { let fun = fn(a, b) { a + b } let curried = function.curry2(fun) @@ -108,12 +80,6 @@ pub fn identity_test() { |> should.equal(#(1, 2.0)) } -pub fn constant_test() { - #(1, 2) - |> pair.map_first(function.constant(42)) - |> should.equal(#(42, 2)) -} - pub fn tap_test() { "Thanks Joe & Louis" |> function.tap(fn(s: String) {