hofkit
is a lightweight and idiomatic Go library providing functional programming utilities such as Map
, Filter
, Reduce
, and more. It is designed to bring expressive, higher-order abstractions to Go while remaining performant and simple.
- Generic functions using Go 1.18+ type parameters
- Minimal allocations and performance close to native loops
- Fully benchmarked and tested
go get github.com/roman91DE/hofkit
Each function is generic and works with any type.
func Map[F any, T any](f func(F) T, xs []F) []T
Map applies function f
to each element of the slice xs
and returns a new slice with the results;
func Filter[T any](p func(T) bool, xs []T) []T
Filter applies predicate function p
to each element of slice xs
and returns a new slice with the elements that satisfy p
;
func Reduce[F any, T any](f func(F, T) T, init T, xs []F) T
Reduce applies reducer function f
to elements of slice xs
, accumulating the result with initial value init
, and returns the final result;
func All[T any](p func(T) bool, xs []T) bool
All applies predicate function p
to each element of slice xs
and returns true if all elements satisfy p
;
func Any[T any](p func(T) bool, xs []T) bool
Any applies predicate function p
to each element of slice xs
and returns true if any element satisfies p
;
func Find[T any](p func(T) bool, xs []T) (T, bool)
Find applies predicate function p
to each element of slice xs
and returns the first element that satisfies p
and true; if no match is found, it returns the zero value and false;
func FindIndex[T any](p func(T) bool, xs []T) (int, bool)
FindIndex applies predicate function p
to each element of slice xs
and returns the index of the first match and true; if no match is found, it returns -1 and false;
func TakeWhile[T any](p func(T) bool, xs []T) []T
TakeWhile applies predicate function p
to elements of slice xs
and returns a new slice of leading elements that satisfy p
;
func ForEach[T any](f func(T), xs []T)
ForEach applies function f
to each element of slice xs
for side effects;
func PartitionBy[T any](p func(T) bool, xs []T) ([]T, []T)
PartitionBy applies predicate function p
to elements of slice xs
and returns two slices: one with elements that satisfy p
, and one with the rest;
func Partial1[A any, B any, R any](f func(A, B) R, a A) func(B) R
Partial1 takes a function (A, B) R
and a value A
, and returns func(B) R
;
func Partial2[A any, B any, C any, R any](f func(A, B, C) R, a A, b B) func(C) R
Partial2 takes a function func(A, B, C) R
and values A, B
, and returns func(C) R
;
func Partial3[A any, B any, C any, D any, R any](f func(A, B, C, D) R, a A, b B, c C) func(D) R
Partial3 takes a function func(A, B, C, D) R
and values A, B, C
, and returns func(D) R
;
Benchmarks are located in the benchmark/
folder. They compare each hofkit
function against an idiomatic native Go equivalent using:
go test -bench=. -benchmem
Plots and performance reports are generated with the Python script plot_bench.py
.
Each benchmark is run on 10,000-element slices with non-trivial logic to stress CPU and memory.
Run all tests:
go test ./...
Pull requests welcome! This library is still young β feel free to open issues or suggest improvements.