diff options
author | inoas <mail@inoas.com> | 2022-11-29 06:57:45 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-12-01 10:10:52 +0000 |
commit | 6f2498d0f5d7bdd96d2a65c19905bb409b525663 (patch) | |
tree | 2f4b8c8b342ba27febfdaaa160b97293bb7c5a4c | |
parent | 323600561189b7d58066f29c563d4bacb554ec01 (diff) | |
download | gleam_stdlib-6f2498d0f5d7bdd96d2a65c19905bb409b525663.tar.gz gleam_stdlib-6f2498d0f5d7bdd96d2a65c19905bb409b525663.zip |
improve function.{curry2, .., curry6) docblocks
-rw-r--r-- | src/gleam/function.gleam | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/src/gleam/function.gleam b/src/gleam/function.gleam index 312dfba..164a43c 100644 --- a/src/gleam/function.gleam +++ b/src/gleam/function.gleam @@ -5,37 +5,84 @@ pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c { fn(a) { fun2(fun1(a)) } } -/// Takes a function with arity two -/// and returns a curried equivalent: +/// Takes a function with `2` arguments (an arity of `2`), and returns the +/// curried equivalent. +/// /// `fn(a, b) -> c` becomes `fn(a) -> fn(b) -> c`. /// +/// ## Examples +/// +/// *Currying* creates a new function that is identical to the given function +/// except that arguments must now be supplied one by one over several function +/// calls. It thus is the process of taking a function with `n` arguments +/// and producing a sequence of `n` single-argument functions. Given: +/// +/// ```gleam +/// > fn my_fun(i: Int, s: String) -> String { ... } +/// ``` +/// +/// …calling `curry2(my_fun)` would return the curried equivalent, like so: +/// +/// ```gleam +/// > curry2(my_fun) +/// fn(Int) -> fn(String) -> String +/// ``` +/// +/// Currying is useful when you want to partially apply a function with +/// some arguments and then pass it somewhere else, for example: +/// +/// ```gleam +/// > import gleam/list +/// > let multiply = curry2(fn(x, y) { x * y }) +/// > let doubles = list.map([1, 2, 3], multiply(2)) +/// [2, 4, 6] +/// ``` +/// pub fn curry2(fun: fn(a, b) -> value) { fn(a) { fn(b) { fun(a, b) } } } -/// Takes a function with arity three -/// and returns a curried equivalent: +/// Takes a function with `3` arguments (an arity of `3`), and returns the +/// curried equivalent. +/// /// `fn(a, b, c) -> d` becomes `fn(a) -> fn(b) -> fn(c) -> d`. /// +/// See [`curry2`](#curry2) for a detailed explanation. +/// pub fn curry3(fun: fn(a, b, c) -> value) { fn(a) { fn(b) { fn(c) { fun(a, b, c) } } } } -/// Takes a function with arity four -/// and returns a curried equivalent. +/// Takes a function with `4` arguments (an arity of `4`), and returns the +/// curried equivalent. +/// +/// `fn(a, b, c, d) -> e` becomes `fn(a) -> fn(b) -> fn(c) -> fn(d) -> e`. +/// +/// See [`curry2`](#curry2) for a detailed explanation. /// pub fn curry4(fun: fn(a, b, c, d) -> value) { fn(a) { fn(b) { fn(c) { fn(d) { fun(a, b, c, d) } } } } } -/// Takes a function with arity five -/// and returns a curried equivalent. +/// Takes a function with `5` arguments (an arity of `5`), and returns the +/// curried equivalent. +/// +/// `fn(a, b, c, d, e) -> f` becomes +/// `fn(a) -> fn(b) -> fn(c) -> fn(d) -> fn(e) -> f`. +/// +/// See [`curry2`](#curry2) for a detailed explanation. +/// pub fn curry5(fun: fn(a, b, c, d, e) -> value) { fn(a) { fn(b) { fn(c) { fn(d) { fn(e) { fun(a, b, c, d, e) } } } } } } -/// Takes a function with arity six -/// and returns a curried equivalent. +/// Takes a function with `6` arguments (an arity of `6`), and returns the +/// curried equivalent. +/// +/// `fn(a, b, c, d, e, f) -> g` becomes +/// `fn(a) -> fn(b) -> fn(c) -> fn(d) -> fn(e) -> fn(f) -> g`. +/// +/// See [`curry2`](#curry2) for a detailed explanation. /// pub fn curry6(fun: fn(a, b, c, d, e, f) -> value) { fn(a) { |