hof

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 0 Imported by: 0

README

hof

Pure function combinators — compose, partially apply, or compare.

normalize := hof.Pipe(strings.TrimSpace, strings.ToLower)
slice.From(inputs).Transform(normalize)

For resilience decorators (retry, circuit breaker, throttle, debounce), see the cb package.

What It Looks Like

// Compose two functions left-to-right
normalize := hof.Pipe(strings.TrimSpace, strings.ToLower)
// Partial application — fix one argument
add := func(a, b int) int { return a + b }
add5 := hof.Bind(add, 5)
slice.From(nums).Transform(add5)
// Apply separate functions to separate values
both := hof.Cross(double, toUpper)
d, u := both(5, "hello")  // 10, "HELLO"
// Equality predicate for Every/Any
allSkipped := slice.From(statuses).Every(hof.Eq(Skipped))

Operations

  • Pipe[A, B, C](f func(A) B, g func(B) C) func(A) C — left-to-right composition
  • Bind[A, B, C](f func(A, B) C, a A) func(B) C — fix first arg
  • BindR[A, B, C](f func(A, B) C, b B) func(A) C — fix second arg
  • Cross[A, B, C, D](f func(A) C, g func(B) D) func(A, B) (C, D) — apply separate fns to separate args
  • Eq[T comparable](target T) func(T) bool — equality predicate factory

All functions panic on nil inputs.

See pkg.go.dev for complete API documentation and the main README for installation.

Documentation

Overview

Package hof provides pure function combinators: composition, partial application, and independent application. Based on Stone's "Algorithms: A Functional Programming Approach" (pipe, sect, cross).

For resilience decorators (retry, circuit breaker, throttle, debounce), see the [cb] package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind[A, B, C any](f func(A, B) C, a A) func(B) C

Bind fixes the first argument of a binary function: Bind(f, x)(y) = f(x, y). Panics if f is nil.

Example
package main

import (
	"fmt"

	"github.com/binaryphile/fluentfp/hof"
)

func main() {
	// Fix the first argument of a binary function.
	add := func(a, b int) int { return a + b }
	addFive := hof.Bind(add, 5)

	fmt.Println(addFive(3))
}
Output:

8

func BindR

func BindR[A, B, C any](f func(A, B) C, b B) func(A) C

BindR fixes the second argument of a binary function: BindR(f, y)(x) = f(x, y). Panics if f is nil.

Example
package main

import (
	"fmt"

	"github.com/binaryphile/fluentfp/hof"
)

func main() {
	// Fix the second argument of a binary function.
	subtract := func(a, b int) int { return a - b }
	subtractThree := hof.BindR(subtract, 3)

	fmt.Println(subtractThree(10))
}
Output:

7

func Cross

func Cross[A, B, C, D any](f func(A) C, g func(B) D) func(A, B) (C, D)

Cross applies two functions independently to two separate arguments. Cross(f, g)(a, b) = (f(a), g(b)). Panics if f or g is nil.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/binaryphile/fluentfp/hof"
)

func main() {
	// Apply separate functions to separate arguments.
	double := func(n int) int { return n * 2 }
	toUpper := func(s string) string { return strings.ToUpper(s) }

	both := hof.Cross(double, toUpper)
	d, u := both(5, "hello")

	fmt.Println(d, u)
}
Output:

10 HELLO

func Eq

func Eq[T comparable](target T) func(T) bool

Eq returns a predicate that checks equality to target. T is inferred from target: hof.Eq(Skipped) returns func(Status) bool.

func Pipe

func Pipe[A, B, C any](f func(A) B, g func(B) C) func(A) C

Pipe composes two functions left-to-right: Pipe(f, g)(x) = g(f(x)). Panics if f or g is nil.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/binaryphile/fluentfp/hof"
)

func main() {
	// Compose TrimSpace then ToLower into a single transform.
	normalize := hof.Pipe(strings.TrimSpace, strings.ToLower)

	fmt.Println(normalize("  Hello World  "))
}
Output:

hello world
Example (Chaining)
package main

import (
	"fmt"
	"strconv"

	"github.com/binaryphile/fluentfp/hof"
)

func main() {
	// Multi-step composition uses intermediate variables.
	double := func(n int) int { return n * 2 }
	addOne := func(n int) int { return n + 1 }
	toString := func(n int) string { return strconv.Itoa(n) }

	doubleAddOne := hof.Pipe(double, addOne)
	full := hof.Pipe(doubleAddOne, toString)

	fmt.Println(full(5))
}
Output:

11

Types

This section is empty.

Jump to

Keyboard shortcuts

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