Documentation
¶
Overview ¶
Package partial provides helpers for partial function application.
Each function, f, in this package has a suffix for arity that indicates the number of arguments to the provided input function, i. Each function f returns a function g with a single argument that can be used to "partially apply" function i with one argument already bound.
Functions have the prefix "Left" if they bind the left-most argument first, or "Right" if they bind the right-most argument first.
Each input function must return exactly one value. The fun/result and fun/maybe packages provide useful functions that can wrap functions that return (value, ok) or (value, error).
For example,
f(a, b, c) becomes f(b, c), with a bound, by calling Left3(f)(a).
f(a, b, c) becomes f(a, b), with c bound, by calling Right3(f)(c).
Example ¶
package main import ( "fmt" "github.com/tawesoft/golib/v2/fun/partial" ) func main() { // The formula for a line can be given by "y = mx + c", where m is the // gradient, and c is the offset where the line crosses the x-axis. line := func(x int, m int, c int) int { // solves for y return (m * x) + c // y = mx + c } // That's the general formula. Let's say we know c and m, and instead want // a single function y = f(x) that we can plug in values for x and get // a result for y. // For example, y = 3x + 5 where f(n) is f((3*n) + 5). // Given c, we can partially apply the function so y = f(mx) where // f(n) = n+5. To put it another way, c is now bound, and we only need m // and x as inputs now. lineC := partial.Right3(line)(5) // f(x, m, c) => f(x, m) // Given m, we can partially apply the function so y = f(g(x)) where g(n) // = m * n. To put it another way, m is now also bound, and we only need x // as an input now. lineMC := partial.Right2(lineC)(2) // f(x, m) => f(x) // Now we have a function y = 2x + 5. // Inputting x = 3... fmt.Println(lineMC(3)) // y = (2 * 3) + 5 = 11 }
Output: 11
Index ¶
- func Left2[A any, B any, Return any](f func(a A, b B) Return) func(a A) func(b B) Return
- func Left3[A any, B any, C any, Return any](f func(a A, b B, c C) Return) func(a A) func(b B, c C) Return
- func Left4[A any, B any, C any, D any, Return any](f func(a A, b B, c C, d D) Return) func(a A) func(b B, c C, d D) Return
- func Right2[A any, B any, Return any](f func(a A, b B) Return) func(b B) func(a A) Return
- func Right3[A any, B any, C any, Return any](f func(a A, b B, c C) Return) func(c C) func(a A, b B) Return
- func Right4[A any, B any, C any, D any, Return any](f func(a A, b B, c C, d D) Return) func(d D) func(a A, b B, c C) Return
- func Single[T any, Return any](f func(t T) Return) func(t T) func() Return
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Single ¶ added in v2.2.0
Single takes a function with a single argument and return value and constructs a function that takes a single argument and returns a function that takes no arguments and returns a single value.
For example,
opener := partial.Single(result.WrapFunc(os.Open)) openFoo := opener("foo.txt") f, err := openFoo().Unpack()
Types ¶
This section is empty.