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.
For example,
f(a, b, c) == Left3(f)(a)(b, c) == Right3(f)(c)(a, b)
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) // 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 bound and we only need // x as an input now. lineMC := partial.Right2(lineC)(2) // 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.