gfunc

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

gfunc - Operations of Functions

Package gfunc provides operations of functions.

  • Import Path

    import "github.com/bytedance/gg/gfunc"

Package gfunc implements Partial Application of functions.

Partial Application refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.

Quick Start

  1. Import "github.com/bytedance/gg/gfunc".
  2. Create FuncN (function with N parameters, such as Func2)
  3. Use method Func2.Partial or Func2.PartialR to bind parameters, producing a FuncN-1 (such as Func1)
package main

import (
        "fmt"

        "github.com/bytedance/gg/gfunc"
)

func main() {
        f := func(a, b int) int {
                return a + b
        }
        add := gfunc.Partial2(f)   // Cast f to "partial application"-able function
        add1 := add.Partial(1)    // Bind argument a to 1
        fmt.Println(add1(0))      // 1 + 0 = 1
        fmt.Println(add1(1))      // add1 can be reused, 1 + 1 = 2
        add1n2 := add1.Partial(2) // Bind argument b to 2, all arguments are fixed
        fmt.Println(add1n2())     // 1 + 2 = 3
        // Output:
        // 1
        // 2
        // 3
}

This is just an example. In real world, we no need to write such add function for specify type, gvalue provides a generics gvalue.Add functions and various utils for writing generic code.

Limitation

No type inference for composite literals

According to the Type Parameters Proposal, Type inference for composite literals is not support at least in Go1.18. So we can not easily cast a function to “partial application”-able type.

Fortunately, Type inference for functions is supported, we provides MakeN functions for casting without specify all type parameters explicitly.

Arity Limitation

If you have a need for n-ary (where n > 10) functions, please file an issue.

Documentation

Overview

Package gfunc provide operations of functions.

Please refer to README.md for details.

Example
add := Partial2(gvalue.Add[int]) // Cast f to "partial application"-able function
add1 := add.Partial(1)           // Bind argument a to 1
fmt.Println(add1(0))             // 0 + 1 = 1
fmt.Println(add1(1))             // add1 can be reused, 1 + 1 = 2
add1n2 := add1.PartialR(2)       // Bind argument b to 2, all arguments are fixed
fmt.Println(add1n2())            // 1 + 2 = 3
Output:

1
2
3

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func0

type Func0[R any] func() R

Func0 is a function with 0 argument and 1 return value (nullary function).

type Func1

type Func1[T1, R any] func(T1) R

Func1 is a function with 1 argument and 1 return value (unary function), which supports partial application.

func Partial1

func Partial1[T1, R any](f Func1[T1, R]) Func1[T1, R]

Partial1 casts function f with 1 argument and 1 return value to Func1.

func (Func1[T1, R]) Partial

func (f Func1[T1, R]) Partial(t1 T1) Func0[R]

Partial binds the first argument (from left to right) of Func1 f to value t1, producing Func0 of smaller arity.

func (Func1[T1, R]) PartialR

func (f Func1[T1, R]) PartialR(t1 T1) Func0[R]

PartialR binds the first argument (from right to left) of Func1 f to value t1, producing Func0 of smaller arity.

type Func2

type Func2[T1, T2, R any] func(T1, T2) R

Func2 is a function with 2 arguments and 1 return value (2-ary function), which supports partial application.

func Partial2

func Partial2[T1, T2, R any](f Func2[T1, T2, R]) Func2[T1, T2, R]

Partial2 casts function f with 2 arguments and 1 return value to Func2

func (Func2[T1, T2, R]) Partial

func (f Func2[T1, T2, R]) Partial(t1 T1) Func1[T2, R]

Partial binds the first argument (from left to right) of Func2 f to value t1, producing Func1 of smaller arity.

func (Func2[T1, T2, R]) PartialR

func (f Func2[T1, T2, R]) PartialR(t2 T2) Func1[T1, R]

PartialR binds the first argument (from right to left) of Func2 f to value t2, producing Func1 of smaller arity.

type Func3

type Func3[T1, T2, T3, R any] func(T1, T2, T3) R

Func3 is a function with 3 arguments and 1 return value (3-ary function), which supports partial application.

func Partial3

func Partial3[T1, T2, T3, R any](f Func3[T1, T2, T3, R]) Func3[T1, T2, T3, R]

Partial3 casts function f with 3 arguments and 1 return value to Func3

func (Func3[T1, T2, T3, R]) Partial

func (f Func3[T1, T2, T3, R]) Partial(t1 T1) Func2[T2, T3, R]

Partial binds the first argument (from left to right) of Func3 f to value t1, producing Func2 of smaller arity.

func (Func3[T1, T2, T3, R]) PartialR

func (f Func3[T1, T2, T3, R]) PartialR(t3 T3) Func2[T1, T2, R]

PartialR binds the first argument (from right to left) of Func3 f to value t3, producing Func2 of smaller arity.

type Func4

type Func4[T1, T2, T3, T4, R any] func(T1, T2, T3, T4) R

Func4 is a function with 4 arguments and 1 return value (4-ary function), which supports partial application.

func Partial4

func Partial4[T1, T2, T3, T4, R any](f Func4[T1, T2, T3, T4, R]) Func4[T1, T2, T3, T4, R]

Partial4 casts function f with 4 arguments and 1 return value to Func4

func (Func4[T1, T2, T3, T4, R]) Partial

func (f Func4[T1, T2, T3, T4, R]) Partial(t1 T1) Func3[T2, T3, T4, R]

Partial binds the first argument (from left to right) of Func4 f to value t1, producing Func3 of smaller arity.

func (Func4[T1, T2, T3, T4, R]) PartialR

func (f Func4[T1, T2, T3, T4, R]) PartialR(t4 T4) Func3[T1, T2, T3, R]

PartialR binds the first argument (from right to left) of Func4 f to value t4, producing Func3 of smaller arity.

type Func5

type Func5[T1, T2, T3, T4, T5, R any] func(T1, T2, T3, T4, T5) R

Func5 is a function with 5 arguments and 1 return value (5-ary function), which supports partial application.

func Partial5

func Partial5[T1, T2, T3, T4, T5, R any](f Func5[T1, T2, T3, T4, T5, R]) Func5[T1, T2, T3, T4, T5, R]

Partial5 casts function f with 5 arguments and 1 return value to Func5

func (Func5[T1, T2, T3, T4, T5, R]) Partial

func (f Func5[T1, T2, T3, T4, T5, R]) Partial(t1 T1) Func4[T2, T3, T4, T5, R]

Partial binds the first argument (from left to right) of Func5 f to value t1, producing Func4 of smaller arity.

func (Func5[T1, T2, T3, T4, T5, R]) PartialR

func (f Func5[T1, T2, T3, T4, T5, R]) PartialR(t5 T5) Func4[T1, T2, T3, T4, R]

PartialR binds the first argument (from right to left) of Func5 f to value t5, producing Func4 of smaller arity.

type Func6

type Func6[T1, T2, T3, T4, T5, T6, R any] func(T1, T2, T3, T4, T5, T6) R

Func6 is a function with 6 arguments and 1 return value (6-ary function), which supports partial application.

func Partial6

func Partial6[T1, T2, T3, T4, T5, T6, R any](f Func6[T1, T2, T3, T4, T5, T6, R]) Func6[T1, T2, T3, T4, T5, T6, R]

Partial6 casts function f with 6 arguments and 1 return value to Func6

func (Func6[T1, T2, T3, T4, T5, T6, R]) Partial

func (f Func6[T1, T2, T3, T4, T5, T6, R]) Partial(t1 T1) Func5[T2, T3, T4, T5, T6, R]

Partial binds the first argument (from left to right) of Func6 f to value t1, producing Func5 of smaller arity.

func (Func6[T1, T2, T3, T4, T5, T6, R]) PartialR

func (f Func6[T1, T2, T3, T4, T5, T6, R]) PartialR(t6 T6) Func5[T1, T2, T3, T4, T5, R]

PartialR binds the first argument (from right to left) of Func6 f to value t6, producing Func5 of smaller arity.

type Func7

type Func7[T1, T2, T3, T4, T5, T6, T7, R any] func(T1, T2, T3, T4, T5, T6, T7) R

Func7 is a function with 7 arguments and 1 return value (7-ary function), which supports partial application.

func Partial7

func Partial7[T1, T2, T3, T4, T5, T6, T7, R any](f Func7[T1, T2, T3, T4, T5, T6, T7, R]) Func7[T1, T2, T3, T4, T5, T6, T7, R]

Partial7 casts function f with 7 arguments and 1 return value to Func7

func (Func7[T1, T2, T3, T4, T5, T6, T7, R]) Partial

func (f Func7[T1, T2, T3, T4, T5, T6, T7, R]) Partial(t1 T1) Func6[T2, T3, T4, T5, T6, T7, R]

Partial binds the first argument (from left to right) of Func7 f to value t1, producing Func6 of smaller arity.

func (Func7[T1, T2, T3, T4, T5, T6, T7, R]) PartialR

func (f Func7[T1, T2, T3, T4, T5, T6, T7, R]) PartialR(t7 T7) Func6[T1, T2, T3, T4, T5, T6, R]

PartialR binds the first argument (from right to left) of Func7 f to value t7, producing Func6 of smaller arity.

type Func8

type Func8[T1, T2, T3, T4, T5, T6, T7, T8, R any] func(T1, T2, T3, T4, T5, T6, T7, T8) R

Func8 is a function with 8 arguments and 1 return value (8-ary function), which supports partial application.

func Partial8

func Partial8[T1, T2, T3, T4, T5, T6, T7, T8, R any](f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]

Partial8 casts function f with 8 arguments and 1 return value to Func8

func (Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) Partial

func (f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) Partial(t1 T1) Func7[T2, T3, T4, T5, T6, T7, T8, R]

Partial binds the first argument (from left to right) of Func8 f to value t1, producing Func7 of smaller arity.

func (Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) PartialR

func (f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) PartialR(t8 T8) Func7[T1, T2, T3, T4, T5, T6, T7, R]

PartialR binds the first argument (from right to left) of Func8 f to value t8, producing Func7 of smaller arity.

type Func9

type Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any] func(T1, T2, T3, T4, T5, T6, T7, T8, T9) R

Func9 is a function with 9 arguments and 1 return value (9-ary function), which supports partial application.

func Partial9

func Partial9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]

Partial9 casts function f with 9 arguments and 1 return value to Func9

func (Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) Partial

func (f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) Partial(t1 T1) Func8[T2, T3, T4, T5, T6, T7, T8, T9, R]

Partial binds the first argument (from left to right) of Func9 f to value t1, producing Func8 of smaller arity.

func (Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) PartialR

func (f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) PartialR(t9 T9) Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]

PartialR binds the first argument (from right to left) of Func9 f to value t9, producing Func8 of smaller arity.

type Func10

type Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any] func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) R

Func10 is a function with 10 arguments and 1 return value (10-ary function), which supports partial application.

func Partial10

func Partial10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any](f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]

Partial10 casts function f with 10 arguments and 1 return value to Func10

func (Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) Partial

func (f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) Partial(t1 T1) Func9[T2, T3, T4, T5, T6, T7, T8, T9, T10, R]

Partial binds the first argument (from left to right) of Func10 f to value t1, producing Func9 of smaller arity.

func (Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) PartialR

func (f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) PartialR(t10 T10) Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]

PartialR binds the first argument (from right to left) of Func10 f to value t10, producing Func9 of smaller arity.

Jump to

Keyboard shortcuts

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