slice

package
v0.0.0-...-ed9fe1d Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2018 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultParallelOption = &ParallelOption{
	MaxConcurrency: 0,
}

DefaultParallelOption is a default ParallelOption value

View Source
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 ToAddr

func ToAddr(v interface{}) interface{}

ToAddr converts []T to []*T

func ToElem

func ToElem(v interface{}) interface{}

ToElem converts []*T to []T

func ToInterface

func ToInterface(v interface{}) []interface{}

ToInterface converts []*T to []interface{}

Types

type Comparable

type Comparable interface {
	Compare(interface{}) (int, error)
}

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.

func (SliceError) Error

func (se SliceError) Error() string

Error implemnts error.Error()

Jump to

Keyboard shortcuts

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