Documentation
¶
Overview ¶
Package sutil
Sutil is slice util. It's spatula in Indonesian/Javanese language.
A personal collection of slice snippet.
Index ¶
- Constants
- func Content[T any](ss []T, page, limit int) []T
- func Equal[T comparable](ss, compared []T) bool
- func Filter[T any](ss []T, fn func(int, T) bool) []T
- func Map[T, U any](ss []T, fn func(int, T) U) []U
- func Reduce[T, U any](ss []T, initializer U, f func(U, T) U) U
- func Split[T any](ss []T, limit int) ([][]T, error)
- func TotalPage[T any](ss []T, limit int) int
- func Unique[T comparable](ss []T) []T
- type Error
Constants ¶
View Source
const ( // ErrInvalidSlice is invalid string slice ErrInvalidSlice = Error("invalid slice") // ErrInvalidLimit is invalid limit ErrInvalidLimit = Error("invalid limit value") )
Variables ¶
This section is empty.
Functions ¶
func Reduce ¶
func Reduce[T, U any](ss []T, initializer U, f func(U, T) U) U
Reduce reduces a []T to a single value using a reduction function.
func Split ¶
Split splits []T into []T with maximum member of[]T is limit.
Can be used for pagination or break the parameters used in SQL IN statements. Manipulating slice is faster than append via iteration.
Example:
input := []string{"7892141641500", "7892141600279", "7892141600422", "7892141640145", "7892141650236", "7892141650274", "7892141650311"}
limit := 2
pages, err := sutil.Split(input, limit)
if err != nil {
fmt.Println(err)
}
fmt.Println(pages)
Will return:
[[7892141641500 7892141600279] [7892141600422 7892141640145] [7892141650236 7892141650274] [7892141650311]]
Types ¶
Click to show internal directories.
Click to hide internal directories.
