partial

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 0 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Left2

func Left2[A any, B any, Return any](
	f func(a A, b B) Return,
) func(a A) func(b B) Return

func Left3

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

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

func Right2[A any, B any, Return any](
	f func(a A, b B) Return,
) func(b B) func(a A) Return

func Right3

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

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 added in v2.2.0

func Single[T any, Return any](
	f func(t T) Return,
) func(t T) func() Return

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL