funk

package
v0.64.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 8 Imported by: 2

README

Benchmarks

BenchmarkMap-16                                      146           8168550 ns/op         8289055 B/op          1 all
BenchmarkUniq-16                                      10         103630461 ns/op        34581937 B/op         23 all
BenchmarkUniqThoas-16                                  4         283696922 ns/op        92759996 B/op    2000015 all
BenchmarkUniqStruct-16                                 6         177865631 ns/op        70916293 B/op         26 all
BenchmarkUniqThoasStruct-16                            3         374595307 ns/op        127689090 B/op   2000018 all
BenchmarkChunk-16                                    111          10232891 ns/op         9581239 B/op      10001 all
BenchmarkChunkReduce-16                               99          11395557 ns/op         9626752 B/op      10001 all
BenchmarkChunkThoas-16                                13         112813206 ns/op        49245217 B/op    1100025 all
BenchmarkDifferenceRandomStruct-16                 75243             13358 ns/op            7366 B/op         30 all
BenchmarkDifferenceThoasRandomStruct-16              470           2306334 ns/op          344273 B/op      20820 all
BenchmarkDifferenceRandom-16                       47986             24955 ns/op           13975 B/op         28 all
BenchmarkDifferenceThoasRandom-16                    868           1476350 ns/op          178580 B/op      20820 all
BenchmarkDifferenceStatic-16                      110392             10376 ns/op            3285 B/op         14 all
BenchmarkDifferenceThoasStatic-16                   1707            719145 ns/op           90498 B/op      10704 all
BenchmarkIntersect-16                              97228             13425 ns/op            5578 B/op         25 all
BenchmarkIntersectThoas-16                         27148             46202 ns/op           15720 B/op        517 all

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.8.3

func All[S ~[]T, T any, F func(T) bool](inp S, pred F) bool

All reports whether every element in inp satisfies pred.

func Any added in v0.8.3

func Any[S ~[]T, T any, F func(T) bool](inp S, pred F) bool

Any reports whether any element in inp satisfies pred.

func BoxSlice added in v0.40.3

func BoxSlice[T any](sl []T) []*T

BoxSlice returns a slice containing pointers to the values in sl.

func CastSlice

func CastSlice[T any, I ~[]any](sl I) ([]T, error)

CastSlice casts each element of sl from any to T. It returns an error when an element cannot be asserted to T.

func Chunk

func Chunk[S ~[]T, T any](sl S, size int) [][]T

Chunk splits sl into consecutive chunks of at most size elements. It returns nil for non-positive sizes and an empty slice for empty input.

func ChunkIntoBuckets added in v0.37.0

func ChunkIntoBuckets[S ~[]T, T any](sl S, numOfBuckets int) [][]T

ChunkIntoBuckets splits sl into chunks sized to fit the input into numOfBuckets buckets.

func Concat added in v0.63.7

func Concat[T any](sl ...[]T) []T

Concat appends all provided slices into a new slice in argument order.

func Contains

func Contains[T any](in []T, elem T) bool

Contains reports whether in contains an element deeply equal to elem.

func ContainsAll added in v0.21.5

func ContainsAll[T any](in []T, elements []T) bool

ContainsAll reports whether in contains every element from elements.

func ContainsFunc

func ContainsFunc[S ~[]T, T any](sl S, pred func(T) bool) bool

ContainsFunc reports whether any element in sl satisfies pred.

func Difference

func Difference[S ~[]T, T comparable](left, right S) (inLeft, inRight []T)

Difference returns elements only present in left and elements only present in right. Example: Difference([]int{1, 2, 3}, []int{2, 3, 4}) returns []int{1}, []int{4}.

func DifferenceKeyed

func DifferenceKeyed[S1 ~[]T1, S2 ~[]T2, T1, T2 mdl.Keyed](left S1, right S2) (inLeft S1, inRight S2)

DifferenceKeyed returns keyed elements only present in left and only present in right. Elements are compared by their mdl.Keyed key.

func DifferenceMaps

func DifferenceMaps[K comparable, V1, V2 any, M1 ~map[K]V1, M2 ~map[K]V2](left M1, right M2) (inLeft M1, inRight M2)

DifferenceMaps splits two maps into two new maps. The first result contains only the part of the left input map not present in the right input map while the second result contains the part from the right input map without keys in the left input map.

func Empty added in v0.8.3

func Empty[S ~[]T, T any](inp S) bool

Empty reports whether inp has no elements.

func Filter

func Filter[S ~[]T, T any](sl S, pred func(T) bool) []T

Filter returns the elements of sl for which pred returns true.

func FindFirst

func FindFirst[S ~[]T, T comparable](sl S, el T) (out T, ok bool)

FindFirst returns the first element in sl equal to el and whether such an element was found.

func FindFirstFunc

func FindFirstFunc[S ~[]T, T any](sl S, pred func(T) bool) (out T, ok bool)

FindFirstFunc returns the first element in sl satisfying pred and whether such an element was found.

func First

func First[S ~[]T, T any](sl S) (out T, ok bool)

First returns the first element of sl and whether sl was non-empty.

func Flatten

func Flatten[S ~[]T, T any](sl []S) []T

Flatten concatenates a slice of slices into a single slice.

func Index

func Index[T any](sl []T, e T) int

Index returns the first index of an element deeply equal to e, or -1 if none exists.

func Intersect

func Intersect[S ~[]T, T comparable](sl1, sl2 S) []T

Intersect returns the unique elements contained in both slices.

func IntersectKeyed

func IntersectKeyed[S ~[]T, T mdl.Keyed](s1, s2 S) S

IntersectKeyed returns keyed elements from s1 whose keys are also present in s2.

func IntersectMaps

func IntersectMaps[K comparable, V any, M ~map[K]V](m1, m2 M, ms ...M) M

IntersectMaps produces the intersection of at least two maps. The resulting map contains all elements from the first map with keys present in all input maps.

func IntersectMapsWith added in v0.40.4

func IntersectMapsWith[K comparable, V any, M ~map[K]V](combine func(V, V) V, m1, m2 M, ms ...M) M

IntersectMapsWith is similar to IntersectMaps, but selects the values based on a combination function.

func KeyedToMap

func KeyedToMap[S ~[]T, T mdl.Keyed](sl S) map[string]T

KeyedToMap indexes keyed elements by their mdl.Keyed key.

func Keys added in v0.36.1

func Keys[K comparable, V any, M ~map[K]V](m M) []K

Keys returns the keys from a map as a slice. The order of the result will be undefined.

func Last

func Last[T any](sl []T) T

Last returns the last element of sl, or the zero value of T when sl is empty.

func Map

func Map[S ~[]T1, T1, T2 any, F func(T1) T2](sl S, op F) []T2

Map applies op to each element of sl and returns the mapped values.

func MapFilter added in v0.48.0

func MapFilter[K comparable, V any, M ~map[K]V](m M, f func(key K, value V) bool) map[K]V

MapFilter removes entries from a map which don't satisfy a predicate. The order of the calls to the predicate is undefined.

func MapKeys

func MapKeys[K1 comparable, K2 comparable, V any, M1 ~map[K1]V](m M1, f func(key K1) K2) map[K2]V

MapKeys applies a function to every key from a map. If the function maps two keys to the same new value, the result is undefined (one of the values will be randomly chosen).

func MapKeysWith added in v0.40.4

func MapKeysWith[K1 comparable, K2 comparable, V any, M1 ~map[K1]V](m M1, f func(key K1) K2, combine func(V, V) V) map[K2]V

MapKeysWith is similar to MapKeys, but uses a combination function to resolve conflicts when mapping two keys to the same new key value. The order of the arguments of the combination function is randomly chosen and not defined.

func MapValues

func MapValues[K comparable, V1, V2 any, M1 ~map[K]V1](m M1, f func(value V1) V2) map[K]V2

MapValues applies a function to all values of a map. The order in which the function will be called for each value is undefined.

func MergeMaps

func MergeMaps[K comparable, V any, M ~map[K]V](m ...M) M

MergeMaps merges zero or more maps into one combined map. Elements from later arguments overwrite elements from earlier arguments in the case of collisions. If given a single argument, MergeMaps produces a swallow copy of the input.

func MergeMapsWith added in v0.40.4

func MergeMapsWith[K comparable, V any, M ~map[K]V](combine func(V, V) V, m ...M) M

MergeMapsWith is similar to MergeMaps, but uses a function to combine elements from keys present in multiple input maps.

func NilIfEmpty added in v0.52.0

func NilIfEmpty[S ~[]T, T any](inp S) S

NilIfEmpty returns nil for empty slices and inp otherwise.

func None added in v0.8.3

func None[S ~[]T, T any, F func(T) bool](inp S, pred F) bool

None reports whether no element in inp satisfies pred.

func NotEmpty added in v0.8.3

func NotEmpty[S ~[]T, T any](inp S) bool

NotEmpty reports whether inp has at least one element.

func Partition

func Partition[S ~[]T, T any, E comparable, F func(T) E](sl S, keyer F) map[E][]T

Partition groups the elements of sl by the key returned from keyer.

func PartitionMap added in v0.8.3

func PartitionMap[S ~[]T, T, K comparable, V any, F func(T) (K, V)](inp S, keyer F) map[K][]V

PartitionMap groups mapped values by the key returned from keyer for each input element.

func PopulateMap added in v0.40.4

func PopulateMap[V any, K comparable](value V, keys ...K) map[K]V

PopulateMap creates a new map from a value and a list of keys. All keys will be mapped to the given value.

func PopulateMapWith added in v0.40.4

func PopulateMapWith[T any, K comparable](generator func(K) T, keys ...K) map[K]T

PopulateMapWith creates a new map from a function and a list of keys by applying the function to each key.

func RangeSorted added in v0.51.1

func RangeSorted[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V]

RangeSorted returns an iterator that yields all key/value pairs from the given map in order sorted by key.

func Reduce

func Reduce[S ~[]T1, T1, T2 any](sl S, op func(T2, T1, int) T2, init T2) T2

Reduce folds sl from left to right, passing the accumulator, element, and index to op.

func Repeat

func Repeat[T any](el T, times int) []T

Repeat returns a slice containing el repeated times times. It returns nil for negative times and an empty slice for zero times.

func Reverse

func Reverse[S ~[]T, T any](sl S) S

Reverse returns a new slice with the elements of sl in reverse order.

func SetToSlice

func SetToSlice[T comparable](s Set[T]) []T

SetToSlice converts a set to a slice containing the elements from the set. The order of the elements of the returned slice is undefined.

func SliceToMap

func SliceToMap[S ~[]T, T, V any, K comparable](sl S, keyer func(T) (K, V)) map[K]V

SliceToMap builds a map from sl using keyer to derive each key and value.

func Tail

func Tail[T any](sl []T) []T

Tail returns sl without its first element. For slices with fewer than two elements, it returns sl unchanged.

func UnboxSlice added in v0.40.3

func UnboxSlice[T any](sl []*T) []T

UnboxSlice returns the values referenced by sl, using the zero value for nil pointers.

func Uniq

func Uniq[S ~[]T, T comparable](sl S) S

Uniq returns sl without duplicate elements, preserving the first occurrence of each element.

func UniqByType

func UniqByType[S ~[]T, T any](sl S) S

UniqByType returns sl without duplicate dynamic types, preserving the first element for each type.

func UniqFunc added in v0.36.1

func UniqFunc[S ~[]T, T any, K comparable](sl S, fn func(T) K) []T

UniqFunc returns sl without duplicate keys produced by fn, preserving the first element for each key.

func Values added in v0.40.4

func Values[K comparable, V any, M ~map[K]V](m M) []V

Values returns the values from a map as a slice. The order of the result will be undefined.

Types

type Set

type Set[T comparable] map[T]struct{}

A Set is a map which stores no values.

func NewSet added in v0.40.12

func NewSet[T comparable](items ...T) Set[T]

NewSet creates a new Set with the given elements.

func SliceToSet

func SliceToSet[S ~[]T, T comparable](sl S) Set[T]

SliceToSet returns a set containing the unique elements of sl.

func (Set[T]) Add added in v0.40.12

func (s Set[T]) Add(items ...T) bool

Add adds new values to the Set.

It returns true if any item was not contained in this Set.

func (Set[T]) AddAll added in v0.40.12

func (s Set[T]) AddAll(items Set[T]) bool

AddAll adds all elements of a Set to the Set.

It returns true if any item was not contained in this Set.

func (Set[T]) Contains

func (s Set[T]) Contains(item T) bool

Contains checks if a Set contains the given value.

func (Set[T]) ContainsAll added in v0.40.12

func (s Set[T]) ContainsAll(other Set[T]) bool

ContainsAll checks if a Set contains all values from the given other Set.

func (Set[T]) Empty added in v0.40.12

func (s Set[T]) Empty() bool

Empty returns true if the Set contains no entries.

func (Set[T]) Len added in v0.40.12

func (s Set[T]) Len() int

Len returns the number of distinct elements in the Set.

func (Set[T]) Remove added in v0.40.12

func (s Set[T]) Remove(items ...T) bool

Remove removes values from the Set.

It returns true if any item was contained in the Set.

func (Set[T]) RemoveAll added in v0.40.12

func (s Set[T]) RemoveAll(items Set[T]) bool

RemoveAll removes all elements of a Set from the Set.

It returns true if any item was contained in the Set.

func (Set[T]) Set deprecated

func (s Set[T]) Set(item T)

Set adds a new value to the Set.

Deprecated: Use Add instead

func (Set[T]) ToSlice added in v0.40.4

func (s Set[T]) ToSlice() []T

ToSlice is SetToSlice as a member function.

Jump to

Keyboard shortcuts

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