Documentation ¶
Index ¶
- Variables
- func AppendIfMissing(list interface{}, v ...interface{}) interface{}
- func Filter(list interface{}, fun interface{}) interface{}
- func MustCompare(v1 Comparable, v2 interface{}) int
- func Parallel(list interface{}, option *ParallelOption, fun interface{}) error
- func SplitByLength(list interface{}, eachSize int) interface{}
- func ToAddr(v interface{}) interface{}
- func ToElem(v interface{}) interface{}
- func ToInterface(v interface{}) []interface{}
- type Comparable
- type ParallelOption
- type SliceError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultParallelOption = &ParallelOption{
MaxConcurrency: 0,
}
DefaultParallelOption is a default ParallelOption value
var ErrInvalidComparison = fmt.Errorf("invalid comparison")
ErrInvalidComparison is an error object returend by Compare if not comparable.
Functions ¶
func AppendIfMissing ¶
func AppendIfMissing(list interface{}, v ...interface{}) interface{}
AppendIfMissing returns a new list of `list` if v doesn't present.
Example ¶
var a = []int{0, 1, 2, 3, 4} var b = AppendIfMissing(a, 0) var c = AppendIfMissing(a, 5) fmt.Println(b) fmt.Println(c)
Output: [0 1 2 3 4] [0 1 2 3 4 5]
func Filter ¶
func Filter(list interface{}, fun interface{}) interface{}
Filter returns a new filtered list
Example ¶
var a = []int{0, 1, 2, 3, 4} var b = Filter(a, func(i int, v int) bool { return v == 2 }).([]int) fmt.Println(b)
Output: [0 1 3 4]
func MustCompare ¶
func MustCompare(v1 Comparable, v2 interface{}) int
MustCompare returns the compared result with v1 and v2. 0: v1 == v2, 1: v1 > v2, -1: v1 < v2.
func Parallel ¶
func Parallel(list interface{}, option *ParallelOption, fun interface{}) error
Parallel is spawn `fun` in parallel. `fun` must be type of `func(int, *T) error`, where list is []T or []*T).
Example ¶
var a = []int{0, 1, 2, 3, 4} Parallel(a, DefaultParallelOption, func(i int, v int) error { a[i] = a[i] + 1 return nil }) fmt.Println(a)
Output: [1 2 3 4 5]
func SplitByLength ¶
func SplitByLength(list interface{}, eachSize int) interface{}
SplitByLength splits a list into multiple lists. Each lengths of lists must be up to `each` You can use the returned value as `[][]T` when you pass `[]T` for list.
Example ¶
var a = []int{0, 1, 2, 3, 4} var b = SplitByLength(a, 2).([][]int) fmt.Println(b)
Output: [[0 1] [2 3] [4]]
func ToInterface ¶
func ToInterface(v interface{}) []interface{}
ToInterface converts []*T to []interface{}
Types ¶
type Comparable ¶
Comparable is an interface to compare a slice element with x
type ParallelOption ¶
type ParallelOption struct {
MaxConcurrency int // # of goroutines to invoke at max. 0 means the same # of slice length
}
ParallelOption is an option for Parallel* methods
type SliceError ¶
type SliceError []error
SliceError is an error collection as a single error. error[i] might be nil if there is no error.
func NewSliceError ¶
func NewSliceError(size int) SliceError
NewSliceError creates SliceError instance with the given size.