Documentation
¶
Index ¶
- func After[T any](ss []T, n int) []T
- func Chunk[T any](items []T, size int) (chunks [][]T, err error)
- func ContainsItem[T comparable](s []T, o T) bool
- func EqualsNoOrder[T any](a, b []T, less func(x, y T) bool) bool
- func Exclude[T comparable](a []T, b []T) []T
- func ExcludeFunc[T any, K comparable](a []T, b []T, keyFn func(element T) K) []T
- func First[T any](param []T) T
- func FirstMaxN[T any](ss []T, maxN int) []T
- func FirstOr[T any](param []T, def T) T
- func FromSingleton[T any](e T) []T
- func Range[T any](ss []T, from int, to int) []T
- func Set[T comparable](a []T) []T
- func ToMap[K comparable, V any, F MapFunc[K, V]](l []V, getKeyFn F) map[K]V
- func ToMapWithDuplicateCheck[K comparable, V any, F MapFunc[K, V], D MapFuncOnDuplicate[K, V]](l []V, getKeyFn F, onDuplicateFn D) (map[K]V, error)
- func ToSetMap[K comparable](l []K) map[K]Null
- func Union[T comparable](a []T, b []T) []T
- func UnionFunc[T any, K comparable](a []T, b []T, keyFn func(element T) K) []T
- type MapFunc
- type MapFuncOnDuplicate
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After is an alternative of arr[N:] to get all element after N, to avoid slice's behaviour on arr[N:] will panic if N is out or range.
func ContainsItem ¶
func ContainsItem[T comparable](s []T, o T) bool
func EqualsNoOrder ¶
EqualsNoOrder compares two slices without order. It is a wrapper of github.com/google/go-cmp/cmp.Equal with cmpopts.SortSlices(less).
func Exclude ¶
func Exclude[T comparable](a []T, b []T) []T
Exclude takes two slices and returns a slice with elements in a but not in b. For slices which elements are not comparable, use ExcludeFunc instead. Same elements in a will be kept.
func ExcludeFunc ¶
func ExcludeFunc[T any, K comparable](a []T, b []T, keyFn func(element T) K) []T
func FirstMaxN ¶
FirstMaxN is an alternative of arr[:N] to get first maximum N element of given slice, to avoid slice's behaviour on arr[:N] will panic if N is out or range. If length of slice is less than N, will get the original slice.
func FromSingleton ¶
func FromSingleton[T any](e T) []T
FromSingleton returns a slice of given element.
func Range ¶
Range is an alternative of arr[from:to] to get all element between from and to, to avoid slice's behaviour on arr[from:to] will panic if from or to is out or range.
func Set ¶
func Set[T comparable](a []T) []T
Set takes a slice and returns a slice remove following appeared duplicate elements.
func ToMap ¶
func ToMap[K comparable, V any, F MapFunc[K, V]](l []V, getKeyFn F) map[K]V
ToMap providers a function to convert slice to map, along with a function set instruction how to get key from slice item. If there are duplicate key, the last one will be kept. If you want to handle duplicate key, use ToMapWithDuplicateCheck.instead.
Example ¶
package main
import (
"fmt"
"github.com/sixleaveakkm/go-utils/slice"
)
func main() {
arr := []string{"1", "2", "3"}
// convert slice to map with key as index, value os value.
m1 := slice.ToMap(arr, func(idx int, v string) int {
return idx
})
fmt.Println(m1)
type Complex struct {
ID int
Name string
}
arr2 := []Complex{
{ID: 1, Name: "foo"},
{ID: 2, Name: "bar"},
}
// convert slice to map with some value in slice item.
m2 := slice.ToMap(arr2, func(_ int, v Complex) int {
return v.ID
})
fmt.Println(m2)
}
Output: map[0:1 1:2 2:3] map[1:{1 foo} 2:{2 bar}]
func ToMapWithDuplicateCheck ¶
func ToMapWithDuplicateCheck[K comparable, V any, F MapFunc[K, V], D MapFuncOnDuplicate[K, V]](l []V, getKeyFn F, onDuplicateFn D) (map[K]V, error)
ToMapWithDuplicateCheck providers a function to convert slice to map, along with a function set instruction how to get key from slice item, and a function to handle duplicate key. If the onDuplicateFn returns error, it will stop and return immediately with nil map and the error.
Example ¶
package main
import (
"fmt"
"github.com/sixleaveakkm/go-utils/slice"
)
func main() {
type Complex struct {
ID int
Name string
}
arr := []Complex{
{ID: 1, Name: "foo"},
{ID: 2, Name: "bar"},
{ID: 2, Name: "foo bar"},
}
getKey := func(_ int, v Complex) int {
return v.ID
}
onDupKeepExist := func(key int, existValue Complex, newValue Complex) (Complex, error) {
return existValue, nil
}
onDupDoErr := func(key int, existValue Complex, newValue Complex) (Complex, error) {
return Complex{}, fmt.Errorf("duplicate on key %d, existValue: %v, newValue: %v", key, existValue, newValue)
}
m1, err := slice.ToMapWithDuplicateCheck(arr, getKey, onDupKeepExist)
fmt.Println(m1, ", ", err)
m2, err := slice.ToMapWithDuplicateCheck(arr, getKey, onDupDoErr)
fmt.Println(m2, ", ", err)
}
Output: map[1:{1 foo} 2:{2 bar}] , <nil> map[] , duplicate on key 2, existValue: {2 bar}, newValue: {2 foo bar}
func ToSetMap ¶
func ToSetMap[K comparable](l []K) map[K]Null
ToSetMap takes a slice of comparable type and returns a map with key as slice item and value as None.
Example ¶
package main
import (
"fmt"
"github.com/sixleaveakkm/go-utils/slice"
)
func main() {
arr := []string{"1", "2", "3", "10", "1", "2"}
setMap := slice.ToSetMap(arr)
fmt.Println(setMap)
for v := range setMap {
// do something with unique elements in arr
_ = v
}
}
Output:
func Union ¶
func Union[T comparable](a []T, b []T) []T
Union joins two slices into one slice removing duplicate elements (both duplicate inside one slice and duplicate cross two slices). For slices which elements are not comparable, use UnionFunc instead.
func UnionFunc ¶
func UnionFunc[T any, K comparable](a []T, b []T, keyFn func(element T) K) []T
UnionFunc join two slice to one slice, with a function parameter to identify the key of each element. Order will not be guaranteed. For slices which elements are comparable, use Union instead.
Types ¶
type MapFunc ¶
type MapFunc[K comparable, V any] interface { ~func(idx int, v V) K }
MapFunc is the function type for ToMap and ToMapWithDuplicateCheck to get key from slice item.
type MapFuncOnDuplicate ¶
type MapFuncOnDuplicate[K comparable, V any] interface { ~func(key K, existValue V, newValue V) (V, error) }
MapFuncOnDuplicate is the function type for ToMapWithDuplicateCheck to handle duplicate key.