Documentation ¶
Index ¶
- func Batch[T any](s []T, batchSize int, f func(batch []T) error) error
- func Convert[A any, B any](f func(A) B, a ...A) []B
- func ConvertPointers[A any, B any](f func(A) B, a ...A) []*B
- func Filter[T any](s []T, ok func(v T) bool) []T
- func Reorder[T any, K comparable](keys []K, s []T, key func(obj T) K) []T
- func Uniq[T comparable](s []T) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Batch ¶
Example ¶
package main import ( "fmt" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { s := []int{1, 2, 3, 4, 5} _ = slice.Batch(s, 2, func(batch []int) error { fmt.Println(batch) return nil }) }
Output: [1 2] [3 4] [5]
func Convert ¶
Example ¶
package main import ( "fmt" "strconv" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { ints := []int{1, 2, 3} strings := slice.Convert(strconv.Itoa, ints...) fmt.Println(strings) }
Output: [1 2 3]
func ConvertPointers ¶
Example ¶
package main import ( "fmt" "strconv" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { ints := []int{1, 2, 3} strings := slice.ConvertPointers(strconv.Itoa, ints...) for _, p := range strings { fmt.Println(*p) } }
Output: 1 2 3
func Filter ¶
Example ¶
package main import ( "fmt" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { s := []int{1, 2, 3, 4, 5} s = slice.Filter(s, func(v int) bool { return v&1 == 1 }) fmt.Println(s) }
Output: [1 3 5]
func Reorder ¶
func Reorder[T any, K comparable](keys []K, s []T, key func(obj T) K) []T
Reorder sorts s in the order of keys. The values in keys must be unique, but the function is tolerant of non-unique values in s. Values from s not present in keys will be skipped.
Example ¶
package main import ( "fmt" "strconv" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { keys := []string{"1", "2", "3"} vals := []int{3, 2, 3, 2, 0} s := slice.Reorder(keys, vals, strconv.Itoa) fmt.Println(s) }
Output: [2 2 3 3]
func Uniq ¶
func Uniq[T comparable](s []T) []T
Uniq returns unique values from s. The function reuses s to store values, so s should not be used afterward. The order of the values is preserved.
Example ¶
package main import ( "fmt" "gitlab.com/picnic-app/backend/streak-api/internal/util/slice" ) func main() { s := slice.Uniq([]int{3, 2, 3, 2, 0}) fmt.Println(s) }
Output: [3 2 0]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.