slice

package
v0.0.0-...-0494513 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: AGPL-3.0 Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Batch

func Batch[T any](s []T, batchSize int, f func(batch []T) error) error
Example
package main

import (
	"fmt"

	"gitlab.com/picnic-app/backend/app-service/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

func Convert[A, B any](f func(A) B, a ...A) []B
Example
package main

import (
	"fmt"
	"strconv"

	"gitlab.com/picnic-app/backend/app-service/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

func ConvertPointers[A, B any](f func(A) B, a ...A) []*B
Example
package main

import (
	"fmt"
	"strconv"

	"gitlab.com/picnic-app/backend/app-service/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

func Filter[T any](s []T, ok func(v T) bool) []T
Example
package main

import (
	"fmt"

	"gitlab.com/picnic-app/backend/app-service/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/app-service/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/app-service/internal/util/slice"
)

func main() {
	s := slice.Uniq([]int{3, 2, 3, 2, 0})
	fmt.Println(s)

}
Output:

[3 2 0]

func ValuesByKey

func ValuesByKey[T, K any](s []T, key func(obj T) K) []K

Types

This section is empty.

Jump to

Keyboard shortcuts

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