gfn
is a Golang library that leverages generics to provide various methods, including common functional programming techniques such as Map
, Reduce
, and Filter
, along with other utilities like Contains
, Keys
, etc. The idea of this library is very simple, it aims to port as many small utilities from other languages to Go
as possible. The implementation is highly influenced byPython
, Ruby
, JavaScript
and Lodash
.
- No
reflect
. - No third-party packages.
- Time complexity of
O(n)
.
- Installation
- Usage
- Type
- Functional
- Math
- Array
- gfn.All
- gfn.Any
- gfn.Chunk
- gfn.Concat
- gfn.Contains
- gfn.Copy
- gfn.Count
- gfn.CountBy
- gfn.Counter
- gfn.CounterBy
- gfn.Difference
- gfn.DifferenceBy
- gfn.Equal
- gfn.EqualBy
- gfn.Fill
- gfn.Find
- gfn.FindLast
- gfn.ForEach
- gfn.GroupBy
- gfn.IndexOf
- gfn.Intersection
- gfn.IntersectionBy
- gfn.IsSorted
- gfn.IsSortedBy
- gfn.LastIndexOf
- gfn.Range
- gfn.RangeBy
- gfn.Remove
- gfn.Repeat
- gfn.Reverse
- gfn.Sample
- gfn.Shuffle
- gfn.ToSet
- gfn.Union
- gfn.UnionBy
- gfn.Uniq
- gfn.UniqBy
- gfn.Unzip
- gfn.Zip
- Map
go get github.com/suchen-sci/gfn
import "github.com/suchen-sci/gfn"
/*
byte: alias for uint8
rune: alias for int32
time.Duration: alias for int64
...
*/
type Int interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Uint interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Float interface {
~float32 | ~float64
}
type Complex interface {
~complex64 | ~complex128
}
type Pair[T, U any] struct {
First T
Second U
}
func Filter[T any](array []T, filter func(T) bool) []T
Filter returns a new array containing elements of the original array that satisfy the provided function.
array := []int{1, 2, 3, 4, 5, 6}
gfn.Filter(array, func(i int) bool { return i%2 == 0 })
// []int{2, 4, 6}
func FilterKV[K comparable, V any](m map[K]V, fn func(K, V) bool) map[K]V
FilterKV returns a new map containing elements of the original map that satisfy the provided function.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.FilterKV(m, func(k int, v string) bool {
return k == 1 || v == "c"
})
// map[int]string{1: "a", 3: "c"}
func Map[T any, R any](array []T, mapper func(T) R) []R
Map returns a new array with the results of calling the mapper function on each element. No MapKV because I don't know what to return, an array or a map? Instead, please use ForEachKV.
gfn.Map([]int{1, 2, 3}, func(i int) string { return i+1 })
// []int{2, 3, 4}
gfn.Map([]int{1, 2, 3}, func(i int) string {
return strconv.Itoa(i)
})
// []string{"1", "2", "3"}
func Reduce[T any, R any](array []T, init R, fn func(R, T) R) R
Reduce executes a reducer function on each element of the array, resulting in a single output value.
gfn.Reduce([]int{1, 2, 3}, 0, func(a, b int) int {
return a + b
})
// 6
func ReduceKV[K comparable, V any, R any](m map[K]V, init R, fn func(R, K, V) R) R
ReduceKV executes a reducer function on each element of the map, resulting in a single output value.
m := map[string]int{"a": 1, "b": 2, "c": 3}
total := gfn.ReduceKV(m, 0, func(value int, k string, v int) int {
return value + v
})
// 6
func Abs[T Int | Float](x T) T
Abs returns the absolute value of x.
gfn.Abs(-1) // 1
gfn.Abs(-100.99) // 100.99
func DivMod[T Int | Uint](a, b T) (T, T)
DivMod returns quotient and remainder of a/b.
gfn.DivMod(10, 3) // (3, 1)
func Max[T Int | Uint | Float | ~string](array ...T) T
Max returns the maximum value in the array. For float64 arrays, NaN values are skipped.
gfn.Max([]int16{1, 5, 9, 10}...) // 10
gfn.Max("ab", "cd", "e") // "e"
gfn.Max(1.1, math.NaN(), 2.2) // 2.2
gfn.Max([]float64{math.NaN(), math.NaN(), math.NaN()}...) // NaN
func MaxBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) T
MaxBy returns the maximum value in the array, using the given function to transform values.
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
p := gfn.MaxBy(products, func(p Product) int {
return p.amount
}) // {"orange", 30}
func Mean[T Int | Uint | Float](array ...T) float64
Mean returns the mean of all values in the array.
gfn.Mean(1, 2, 3) // 2.0
gfn.Mean([]int{1, 2, 3, 4}...) // 2.5
func MeanBy[T any, U Int | Uint | Float](array []T, fn func(T) U) float64
MeanBy returns the mean of all values in the array after applying fn to each value.
type Product struct {
name string
cost float64
}
products := []Product{
{"apple", 1.5},
{"banana", 2.5},
{"orange", 3.5},
{"lemon", 4.5},
}
gfn.MeanBy(products, func(p Product) float64 {
return p.cost
}) // 3.0
func Min[T Int | Uint | Float | ~string](array ...T) T
Min returns the minimum value in the array. For float64 arrays, NaN values are skipped.
gfn.Min(1.1, 2.2, 3.3) // 1.1
gfn.Min([]int16{1, 5, 9, 10}...) // 1
gfn.Min(1, -1, 10) // -1
gfn.Min([]float64{1.1, math.Inf(-1), math.NaN()}...) // math.Inf(-1)
func MinBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) T
MinBy returns the minimum value in the array, using the given function to transform values.
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
p := gfn.MinBy(products, func(p Product) int {
return p.amount
}) // {"apple", 10}
func MinMax[T Int | Uint | Float | ~string](array ...T) (T, T)
MinMax returns the minimum and maximum value in the array. For float64 arrays, please use MinMaxFloat64.
gfn.MinMax(1, 5, 9, 10) // 1, 10
gfn.MinMax(math.NaN(), 1.85, 2.2) // 1.85, 2.2
gfn.MinMax(math.NaN(), math.NaN(), math.NaN()) // NaN, NaN
func MinMaxBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) (T, T)
MinMaxBy returns the minimum and maximum value in the array, using the given function to transform values.
type Product struct {
name string
amount int
}
products := []Product{
{"banana", 20},
{"orange", 30},
{"apple", 10},
{"grape", 50},
{"lemon", 40},
}
gfn.MinMaxBy(products, func(p Product) int {
return p.amount
}) // {"apple", 10}, {"grape", 50}
func Mode[T comparable](array []T) T
Mode returns the most frequent value in the array.
gfn.Mode([]int{1, 1, 5, 5, 5, 2, 2})) // 5
func ModeBy[T any, U comparable](array []T, fn func(T) U) T
ModeBy returns the most frequent value in the array, using the given function to transform values.
type Product struct {
name string
amount int
}
products := []Product{
{"banana", 20},
{"banana", 20},
{"apple", 10},
}
gfn.ModeBy(products, func(p Product) int {
return p.amount
}) // {"banana", 20}
func Sum[T Int | Uint | Float | ~string | Complex](array ...T) T
Sum returns the sum of all values in the array. Be careful when using this function for float64 arrays with NaN and Inf values. Sum([math.NaN(), 0.5]) produces math.NaN(). Sum(math.Inf(1), math.Inf(-1)) produces math.NaN() too.
gfn.Sum([]int{1, 5, 9, 10}...) // 25
gfn.Sum(1.1, 2.2, 3.3) // 6.6
gfn.Sum("ab", "cd", "e") // "abcde"
func SumBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) U
SumBy returns the sum of all values in the array after applying fn to each value.
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
gfn.SumBy(products, func(p Product) int {
return p.amount
}) // 60
func All[T any](array []T, fn func(T) bool) bool
All returns true if all elements in an array pass a given test.
gfn.All([]int{1, 2, 3, 4}, func(i int) bool {
return i > 0
}
// true
func Any[T any](array []T, fn func(T) bool) bool
Any returns true if at least one element in an array passes a given test.
gfn.Any([]int{1, 2, 3, 4}, func(i int) bool {
return i > 3
}
// true
func Chunk[T any](array []T, size int) [][]T
Chunk splits an array into chunks of given size.
gfn.Chunk([]int{1, 2, 3, 4, 5}, 2) // [][]int{{1, 2}, {3, 4}, {5}}
func Concat[T any](arrays ...[]T) []T
Concat returns a new array that is the result of joining two or more arrays.
gfn.Concat([]int{1, 2}, []int{3, 4}) // []int{1, 2, 3, 4}
func Contains[T comparable](array []T, value T) bool
Contains returns true if the array contains the value.
gfn.Contains([]int{1, 2, 3}, 2) // true
gfn.Contains([]string{"a", "b", "c"}, "b") // true
gfn.Contains([]time.Duration{time.Second}, time.Second) // true
func Copy[T any](array []T) []T
Copy returns a new array that is a shallow copy of the original array.
gfn.Copy([]int{1, 2, 3}) // []int{1, 2, 3}
array := []int{1, 2, 3, 4, 5, 6}
gfn.Copy(array[2:])
// []int{3, 4, 5, 6}
func Count[T comparable](array []T, value T) int
Count returns the number of occurrences of a value in an array.
gfn.Count([]int{1, 2, 2, 2, 5, 6}, 2) // 3
func CountBy[T any](array []T, fn func(T) bool) int
CountBy returns the number of elements in an array that satisfy a predicate.
type Employee struct {
name string
department string
}
employees := []Employee{
{"Alice", "Accounting"},
{"Cindy", "Engineering"},
{"Dave", "Engineering"},
{"Eve", "Engineering"},
}
gfn.CountBy(employees, func(e Employee) bool {
return e.department == "Engineering"
}) // 3
func Counter[T comparable](array []T) map[T]int
Counter returns a map of values and their counts.
gfn.Counter([]int{1, 2, 2, 2, 2}) // map[int]int{1: 1, 2: 4}
func CounterBy[T any, U comparable](array []T, fn func(T) U) map[U]int
CounterBy returns a map of values and their counts. The values are calculated by the given function.
type Employee struct {
name string
department string
}
employees := []Employee{
{"Alice", "Accounting"},
{"Dave", "Engineering"},
{"Eve", "Engineering"},
}
gfn.CounterBy(employees, func(e Employee) string {
return e.department
}) // map[string]int{"Accounting": 1, "Engineering": 2}
func Difference[T comparable](array []T, others ...[]T) []T
Difference returns a new array that is a copy of the original array, removing all occurrences of any item that also appear in others. The order is preserved from the original array.
gfn.Difference([]int{1, 2, 3, 4}, []int{2, 4}) // []int{1, 3}
func DifferenceBy[T any, U comparable](fn func(T) U, array []T, others ...[]T) []T
DifferenceBy returns a new array that is a copy of the original array, removing all occurrences of any item that also appear in others. The occurrences are determined by applying a function to each element.
type Data struct {
value int
}
data1 := []Data{{1}, {3}, {2}, {4}, {5}, {2}}
data2 := []Data{{3}, {4}, {5}}
gfn.DifferenceBy(func(d Data) int { return d.value }, data1, data2)
// []Data{{1}, {2}, {2}}
func Equal[T comparable](a, b []T) bool
Equal returns true if two arrays are equal. Two arrays are considered equal if both are nil, or if their lengths are equal and their elements are equal. Elements are compared using == operator.
gfn.Equal([]int{1, 2, 3}, []int{1, 2, 3}) // true
gfn.Equal([]string{"a", "c", "b"}, []string{"a", "b", "c"}) // false
func EqualBy[T1, T2 any](a []T1, b []T2, fn func(T1, T2) bool) bool
EqualBy returns true if two arrays are equal by comparing their elements using the given function.
a := []int{1, 2, 3, 4, 5}
b := []rune{'a', 'b', 'c', 'd', 'e'}
gfn.EqualBy(a, b, func(aa int, bb rune) bool {
return (aa - 1) == int(bb-'a')
}) // true
func Fill[T any](array []T, value T)
Fill sets all elements of an array to a given value. You can control the start and end index by using the slice.
array := make([]bool, 5)
gfn.Fill(array, true)
// []bool{true, true, true, true, true}
// you can control the start and end index by using the slice
array2 := make([]int, 5)
gfn.Fill(array2[2:], 100)
// []int{0, 0, 100, 100, 100}
func Find[T any](array []T, fn func(T) bool) (T, int)
Find returns the first element in an array that passes a given test and corresponding index. Index of -1 is returned if no element passes the test.
value, index := gfn.Find([]string{"a", "ab", "abc"}, func(s string) bool {
return len(s) > 1
})
// "ab", 1
func FindLast[T any](array []T, fn func(T) bool) (T, int)
FindLast returns the last element in an array that passes a given test and corresponding index. Index of -1 is returned if no element passes the test.
value, index := gfn.FindLast([]string{"a", "ab", "abc"}, func(s string) bool {
return len(s) > 1
})
// "abc", 2
func ForEach[T any](array []T, fn func(value T))
ForEach executes a provided function once for each array element.
sum := 0
gfn.ForEach([]int{1, 2, 3}, func(i int) {
sum += i
})
// sum == 6
func GroupBy[T any, K comparable](array []T, groupFn func(T) K) map[K][]T
GroupBy generate a map of arrays by grouping the elements of an array according to a given function.
array := []int{1, 2, 3, 4, 5, 6, 7, 8}
groups := gfn.GroupBy(array, func(i int) string {
if i%2 == 0 {
return "even"
}
return "odd"
})
// map[string][]int{
// "even": []int{2, 4, 6, 8},
// "odd": []int{1, 3, 5, 7},
// }
func IndexOf[T comparable](array []T, value T) int
IndexOf returns the index of the first occurrence of a value in an array, or -1 if not found.
gfn.IndexOf([]int{1, 2, 3, 4}, 3) // 2
gfn.IndexOf([]int{1, 2, 3, 4}, 5) // -1
func Intersection[T comparable](arrays ...[]T) []T
Intersection returns a new array that is the intersection of two or more arrays.
arr1 := []int{1, 2, 3, 4, 5}
arr2 := []int{2, 3, 4, 5, 6}
arr3 := []int{5, 4, 3, 2}
arr4 := []int{2, 3}
gfn.Intersection(arr1, arr2, arr3, arr4) // []int{2, 3}
func IntersectionBy[T any, U comparable](fn func(T) U, arrays ...[]T) []T
IntersectionBy returns a new array that is the intersection of two or more arrays, where intersection is determined by a given function.
type Data struct {
value int
}
data1 := []Data{{1}, {3}, {2}, {4}, {5}}
data2 := []Data{{2}, {3}}
gfn.IntersectionBy(func(d Data) int { return d.value }, data1, data2)
// []Data{{3}, {2}}
func IsSorted[T Int | Uint | Float | ~string](array []T) bool
IsSorted returns true if the array is sorted in ascending order.
gfn.IsSorted([]int{1, 2, 3, 4}) // true
func IsSortedBy[T any](array []T, order func(a1, a2 T) bool) bool
IsSortedBy returns true if the array is sorted in the given order. The order function should return true if a1 is ok to be placed before a2.
gfn.IsSortedBy([]int{2, 2, 1, 1, -1, -1}, func(a, b int) bool { return a >= b })
// true
func LastIndexOf[T comparable](array []T, value T) int
LastIndexOf returns the index of the last occurrence of a value in an array, or -1 if not found.
gfn.LastIndexOf([]int{3, 3, 3, 4}, 3) // 2
gfn.LastIndexOf([]int{1, 2, 3, 4}, 5) // -1
func Range[T Int | Uint](start, end T) []T
Range function returns a sequence of numbers, starting from start, and increments by 1, until end is reached (not included).
gfn.Range(0, 7) // []int{0, 1, 2, 3, 4, 5, 6}
gfn.Range(3, 8) // []int{3, 4, 3, 6, 7}
gfn.Range(-10, -5) // []int{-10, -9, -8, -7, -6}
func RangeBy[T Int | Uint](start, end, step T) []T
RangeBy function returns a sequence of numbers, starting from start, and increments/decrements by step, until end is reached (not included). Zero step panics.
gfn.RangeBy(0, 7, 1) // []int{0, 1, 2, 3, 4, 5, 6}
gfn.RangeBy(0, 8, 2) // []int{0, 2, 4, 6}
gfn.RangeBy(10, 0, -2) // []int{10, 8, 6, 4, 2}
func Remove[T comparable](array []T, values ...T) []T
Remove removes all elements from an array that equal to given values.
gfn.Remove([]int{1, 2, 3, 4, 2, 3, 2, 3}, 2, 3) // []int{1, 4}
func Repeat[T any](array []T, repeat int) []T
Repeat returns a new array that is the result of repeating an array a given number of times.
gfn.Repeat([]int{1, 2, 3}, 3) // []int{1, 2, 3, 1, 2, 3, 1, 2, 3}
func Reverse[T any](array []T)
Reverse reverses an array in place.
array := []int{1, 2, 3, 4}
gfn.Reverse(array)
// []int{4, 3, 2, 1}
func Sample[T any](array []T, n int) []T
Sample returns a random sample of n elements from an array. Every position in the array are at most selected once. n should be less or equal to len(array).
gfn.Sample([]int{1, 2, 3, 4, 5}, 3) // []int{3, 1, 5} or other random choices.
func Shuffle[T any](array []T)
Shuffle randomizes the order of elements by using Fisher–Yates algorithm
array := []int{1, 2, 3, 4}
gfn.Shuffle(array)
// array: []int{2, 1, 4, 3} or other random order
func ToSet[T comparable](array []T) map[T]struct{}
ToSet converts an array to a set.
gfn.ToSet([]int{0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5})
// map[int]struct{}{0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}}
func Union[T comparable](arrays ...[]T) []T
Union returns an array with all duplicates removed from multiple arrays.
gfn.Union([]int{1, 2, 3}, []int{2, 3, 4}, []int{3, 4, 5})
// []int{1, 2, 3, 4, 5}
func UnionBy[T any, U comparable](fn func(T) U, arrays ...[]T) []T
UnionBy returns an array with all duplicates removed from multiple arrays by applying a function to each element.
type Employee struct {
name string
department string
}
group1 := []Employee{
{"Alice", "Accounting"},
{"Bob", "Accounting"},
{"Cindy", "Engineering"},
}
group2 := []Employee{
{"Alice", "Accounting"},
{"Cindy", "Engineering"},
{"Dave", "Engineering"},
{"Eve", "Engineering"},
}
gfn.UnionBy(func(e Employee) string { return e.name }, group1, group2)
// []Employee{
// {"Alice", "Accounting"},
// {"Bob", "Accounting"},
// {"Cindy", "Engineering"},
// {"Dave", "Engineering"},
// {"Eve", "Engineering"},
// }
func Uniq[T comparable](array []T) []T
Uniq returns an array with all duplicates removed.
gfn.Uniq([]int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}) // []int{1, 2, 3, 4}
func UniqBy[T any, U comparable](array []T, fn func(T) U) []T
UniqBy returns an array with all duplicates removed by applying a function to each element.
type Employee struct {
name string
department string
}
employees := []Employee{
{"Alice", "Accounting"},
{"Bob", "Accounting"},
{"Cindy", "Engineering"},
{"Dave", "Engineering"},
}
gfn.UniqBy(employees, func(e Employee) string {
return e.department
})
// []Employee{{"Alice", "Accounting"}, {"Cindy", "Engineering"}}
func Unzip[T, U any](n int, unzipFn func(i int) (T, U)) ([]T, []U)
Unzip returns two arrays built from the elements of a sequence of pairs.
pairs := []gfn.Pair[int, string]{
{First: 1, Second: "a"},
{First: 2, Second: "b"},
{First: 3, Second: "c"},
}
gfn.Unzip(len(pairs), func(i int) (int, string) {
return pairs[i].First, pairs[i].Second
})
// ([]int{1, 2, 3}, []string{"a", "b", "c"})
func Zip[T, U any](a []T, b []U) []Pair[T, U]
Zip returns a sequence of pairs built from the elements of two arrays.
gfn.Zip([]int{1, 2, 3}, []string{"a", "b", "c"})
// []gfn.Pair[int, string]{
// {First: 1, Second: "a"},
// {First: 2, Second: "b"},
// {First: 3, Second: "c"}
// }
func Clear[K comparable, V any](m map[K]V)
Clear removes all keys from a map.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.Clear(m)
// m is now an empty map
func Clone[K comparable, V any](m map[K]V) map[K]V
Clone returns a shallow copy of a map.
m := map[int]string{1: "a", 2: "b", 3: "c"}
m2 := gfn.Clone(m)
// m2 is a copy of m
func DeleteBy[K comparable, V any](m map[K]V, deleteFn func(K, V) bool)
DeleteBy deletes keys from a map if the predicate function returns true.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.DeleteBy(m, func(k int, v string) bool {
return k == 1 || v == "c"
})
// map[int]string{2: "b"}
func DifferentKeys[K comparable, V any](ms ...map[K]V) []K
DifferentKeys returns a slice of keys that are in the first map but not in the others, only keys in the map are considered, not values. It usually used to find the difference between two or more sets.
m1 := map[int]string{1: "a", 2: "b", 3: "c", 4: "d"}
m2 := map[int]string{1: "a", 2: "b"}
m3 := map[int]string{2: "b", 3: "c"}
gfn.DifferentKeys(m1, m2, m3) // []int{4}
func EqualKV[K, V comparable](a map[K]V, b map[K]V) bool
EqualKV returns true if two maps/sets are equal.
map1 := map[int]struct{}{1: {}, 2: {}, 3: {}}
map2 := map[int]struct{}{1: {}, 2: {}, 3: {}}
gfn.EqualKV(map1, map2) // true
func EqualKVBy[K comparable, V1, V2 any](a map[K]V1, b map[K]V2, fn func(K, V1, V2) bool) bool
EqualKVBy returns true if two maps/sets are equal by a custom function.
m1 := map[int]string{1: "a", 2: "b", 3: "c"}
m2 := map[int]string{1: "e", 2: "f", 3: "g"}
gfn.EqualKVBy(m1, m2, func(k int, a, b string) bool {
return len(a) == len(b)
}) // true
func ForEachKV[K comparable, V any](m map[K]V, fn func(K, V))
ForEachKV iterates over a map and calls a function for each key/value pair.
m := map[int]string{1: "a", 2: "b", 3: "c"}
array := make([]int, 0, len(m))
gfn.ForEachKV(m, func(k int, v string) {
array = append(array, k)
}
// array is []int{1, 2, 3} or other order
m := map[int]string{1: "a", 2: "b", 3: "c"}
invert := map[string]int{}
gfn.ForEachKV(m, func(k int, v string) {
invert[v] = k
}
// invert is map[string]int{"a": 1, "b": 2, "c": 3}
func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V
GetOrDefault returns the value for a key if it exists, otherwise it returns the default value.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.GetOrDefault(m, 1, "d") // "a"
gfn.GetOrDefault(m, 4, "d") // "d"
func IntersectKeys[K comparable, V any](ms ...map[K]V) []K
IntersectKeys returns a slice of keys that are in all maps. It usually used to find the intersection of two or more sets.
m1 := map[int]string{1: "a", 2: "b", 3: "c", 4: "d"}
m2 := map[int]string{1: "a", 2: "b"}
m3 := map[int]string{2: "b", 3: "c", 4: "d"}
gfn.IntersectKeys(m1, m2, m3) // []int{2}
func Invert[K, V comparable](m map[K]V) map[V]K
Invert returns a map with keys and values swapped.
m := map[string]string{
"Array": "array.go",
"Map": "map.go",
"Set": "set.go",
"Math": "math.go",
}
gfn.Invert(m)
// map[string]string{
// "array.go": "Array",
// "map.go": "Map",
// "set.go": "Set",
// "math.go": "Math",
// }
func IsDisjoint[K comparable, V1 any, V2 any](m1 map[K]V1, m2 map[K]V2) bool
IsDisjoint returns true if the maps have no keys in common. It usually used to check if two sets are disjoint.
m1 := map[int]string{1: "a", 2: "b", 3: "c"}
m2 := map[int]int{4: 4, 5: 5, 6: 6}
IsDisjoint(m1, m2) // true
m3 := map[int]struct{}{1: {}, 2: {}, 3: {}}
m4 := map[int]struct{}{4: {}, 5: {}, 6: {}}
gfn.IsDisjoint(m3, m4) // true
func Items[K comparable, V any](m map[K]V) []Pair[K, V]
Items returns a slice of pairs of keys and values.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.Items(m)
// []gfn.Pair[int, string]{
// {1, "a"},
// {2, "b"},
// {3, "c"},
// }
func Keys[K comparable, V any](m map[K]V) []K
Keys returns the keys of a map.
gfn.Keys(map[int]string{1: "a", 2: "b", 3: "c"})
// []int{1, 2, 3} or []int{3, 2, 1} or []int{2, 1, 3} etc.
func Select[K comparable, V any](m map[K]V, fn func(K, V) bool) map[K]V
Select returns a map with keys and values that satisfy the predicate function.
m := map[int]string{1: "a", 2: "b", 3: "c"}
gfn.Select(m, func(k int, v string) bool {
return k == 1 || v == "c"
})
// map[int]string{1: "a", 3: "c"}
func ToKV[K comparable, V any](n int, fn func(int) (K, V)) map[K]V
ToKV converts a slice to a map using a function to generate the keys and values.
gfn.ToKV(3, func(i int) (int, string) {
return i, strconv.Itoa(i)
})
// map[int]string{0: "0", 1: "1", 2: "2"}
func Update[K comparable, V any](m map[K]V, other ...map[K]V)
Update updates a map with the keys and values from other maps.
// use Update to do union of maps
m1 := map[int]string{1: "a", 2: "b", 3: "c"}
m2 := map[int]string{4: "d", 5: "e"}
union := map[int]string{}
gfn.Update(union, m1, m2)
// union: map[int]string{1: "a", 2: "b", 3: "c", 4: "d", 5: "e"}
m1 := map[int]string{1: "a", 2: "b", 3: "c"}
m2 := map[int]string{1: "d", 2: "e"}
m3 := map[int]string{1: "f"}
gfn.Update(m1, m2, m3)
// map[int]string{1: "f", 2: "e", 3: "c"}
func Values[K comparable, V any](m map[K]V) []V
Values returns the values of a map.
gfn.Values(map[int]string{1: "a", 2: "b", 3: "c"})
// []string{"a", "b", "c"} or []string{"c", "b", "a"} or []string{"b", "a", "c"} etc.
Format:
/* @example MyFunc
// your examples here
MyFunc(...)
// output: ...
*/
// MyFunc is ...
func MyFunc(args ...) (return values...) {
// your code here.
}
then run following command to update README.md
(generated from README.tmpl.md
).
make doc
gfn
is under the MIT license. See the LICENSE file for details.