Documentation ¶
Index ¶
- func All[T any, S ~[]T](slice S, f func(T) bool) bool
- func Any[T any, S ~[]T](slice S, f func(T) bool) bool
- func Backward[E any, S ~[]E](s S) iter.Seq[E]
- func Chunk[T any, S ~[]T](slice S, chunk int) []S
- func Clone[T any, S ~[]T](slice S) S
- func Collect[E any](s iter.Seq[E]) []E
- func Compare[T cmp.Ordered, S1 ~[]T, S2 ~[]T](lhs S1, rhs S2) int
- func CompareBy[T, U any, S1 ~[]T, S2 ~[]U](lhs S1, rhs S2, cmp func(T, U) int) int
- func Contains[T comparable, S ~[]T](slice S, v T) bool
- func ContainsBy[T any, S ~[]T](slice S, predicate func(T) bool) bool
- func DeepClone[T clone.Cloneable[T], S ~[]T](slice S) S
- func DeepCloneBy[T any, S ~[]T](slice S, clone func(T) T) S
- func Difference[T comparable, S ~[]T](lhs S, rhs S) S
- func DifferenceBy[T any, S ~[]T](lhs S, rhs S, cmp func(T, T) int) S
- func Distinct[T comparable, S ~[]T](slice S) S
- func DistinctBy[T any, K comparable, S ~[]T](slice S, key func(T) K) S
- func Equal[T comparable, S1 ~[]T, S2 ~[]T](lhs S1, rhs S2) bool
- func EqualBy[T, U any, S1 ~[]T, S2 ~[]U](lhs S1, rhs S2, eq func(T, U) bool) bool
- func Filter[T any, S ~[]T](slice S, f func(T) bool) S
- func FilterIndexed[T any, S ~[]T](slice S, f func(T, int) bool) S
- func First[T any, S ~[]T](slice S) optional.Optional[T]
- func FirstFunc[E any, S ~[]E](slice S, f func(E) bool) optional.Optional[E]
- func FirstNonZero[T comparable, S ~[]T](slice S) T
- func FlatMap[T, E any, S ~[]T](slice S, f func(T) []E) []E
- func Flatten[T any, S ~[]T, X ~[]S](slice X) S
- func FlattenBy[T, E any, S ~[]T](slice S, f func(T) []E) []E
- func Fold[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) A) A
- func FoldRight[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) A) A
- func ForEach[T any, S ~[]T](slice S, f func(T))
- func ForEachIndexed[T any, S ~[]T](slice S, f func(T, int))
- func Forward[E any, S ~[]E](s S) iter.Seq[E]
- func GroupBy[T any, TKey comparable, S ~[]T](slice S, f func(T) TKey) map[TKey]S
- func Index[T comparable, S ~[]T](slice S, v T) optional.Optional[int]
- func IndexBy[T any, S ~[]T](slice S, predicate func(T) bool) optional.Optional[int]
- func Intersection[T comparable, S ~[]T](lhs S, rhs S) S
- func IntersectionBy[T any, S ~[]T](lhs S, rhs S, cmp func(T, T) int) S
- func IsSorted[T cmp.Ordered, S ~[]T](slice S) bool
- func IsSortedBy[T any, S ~[]T](slice S, cmp func(T, T) int) bool
- func Last[T any, S ~[]T](slice S) optional.Optional[T]
- func LastFunc[E any, S ~[]E](slice S, f func(E) bool) optional.Optional[E]
- func LastIndex[T comparable, S ~[]T](slice S, v T) optional.Optional[int]
- func LastIndexBy[T any, S ~[]T](slice S, f func(T) bool) optional.Optional[int]
- func Map[T, U any, S ~[]T](slice S, f func(T) U) []U
- func MapIndexed[T, U any, S ~[]T](slice S, f func(T, int) U) []U
- func Max[T cmp.Ordered, S ~[]T](slice S) optional.Optional[T]
- func MaxBy[T any, S ~[]T](slice S, f func(T, T) int) optional.Optional[T]
- func MaxByKey[T any, K cmp.Ordered, S ~[]T](slice S, keyFn func(T) K) optional.Optional[T]
- func Min[T core.Ordered, S ~[]T](slice S) optional.Optional[T]
- func MinBy[T any, S ~[]T](slice S, f func(T, T) int) optional.Optional[T]
- func MinByKey[T any, K cmp.Ordered, S ~[]T](slice S, keyFn func(T) K) optional.Optional[T]
- func None[T any, S ~[]T](slice S, f func(T) bool) bool
- func Nth[T any, S ~[]T](slice S, n int) optional.Optional[T]
- func Partition[T any, S ~[]T](slice S, f func(T) bool) (S, S)
- func Reduce[T any, S ~[]T](slice S, f func(T, T) T) optional.Optional[T]
- func ReduceRight[T any, S ~[]T](slice S, f func(T, T) T) optional.Optional[T]
- func Reverse[T any, S ~[]T](slice S) S
- func Shuffle[T any, S ~[]T](slice S) S
- func Single[T any, S ~[]T](slice S) result.Result[T]
- func Sort[T cmp.Ordered, S ~[]T](slice S) S
- func SortBy[T any, S ~[]T](slice S, cmp func(T, T) int) S
- func SpliceFirst[T any, S ~[]T](slice S) (optional.Optional[T], S)
- func SpliceLast[T any, S ~[]T](slice S) (optional.Optional[T], S)
- func ToHashMap[T any, TKey comparable, TValue any, F ~func(T, int) (TKey, TValue), S ~[]T](slice S, f F) map[TKey]TValue
- func ToHashSet[T comparable, S ~[]T](slice S) map[T]struct{}
- func ToIndexedMap[T any, S ~[]T](slice S) map[int]T
- func TryFold[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) (A, error)) (res A, err error)
- func TryFoldRight[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) (A, error)) (res A, err error)
- func TryMap[T, U any, S ~[]T](slice S, f func(T) (U, error)) ([]U, error)
- func TryMapIndexed[T, U any, S ~[]T](slice S, f func(T, int) (U, error)) ([]U, error)
Examples ¶
- All
- Any
- Any (False)
- Any (User)
- Chunk
- Chunk (Empty)
- Clone
- Contains
- ContainsBy
- DeepClone
- DeepClone (User)
- DifferenceBy
- Equal
- EqualBy (Full)
- EqualBy (Partial)
- Filter
- Filter (User)
- Flatten
- Flatten (Empty)
- FlattenBy
- Fold
- Fold (Diff)
- Fold (User)
- ForEach
- ForEachIndexed
- GroupBy
- IntersectionBy
- IsSorted
- IsSorted (Not)
- IsSortedBy
- IsSortedBy (Not)
- Map
- Map (User)
- Max
- MaxBy
- Min
- MinBy
- None
- Nth (In)
- Nth (Not_in)
- Partition
- Reduce
- Reduce (None)
- Reduce (User)
- Sort
- SortBy
- SortBy (Key)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all elements in the given slice satisfy the given predicate.
Example:
slices.All([]int{1,2,3}, func(x int) bool { return x > 0 }) => true slices.All([]int{1,2,3}, func(x int) bool { return x > 2 }) => false
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.All(slice, func(i int) bool { return i > 0 }) fmt.Println(result) }
Output: true
func Any ¶
Any returns true if any element in the given slice satisfies the given predicate.
Example:
slices.Any([]int{1,2,3}, func(x int) bool { return x > 2 }) => true slices.Any([]int{1,2,3}, func(x int) bool { return x > 6 }) => false
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Any(slice, func(i int) bool { return i > 1 }) fmt.Println(result) }
Output: true
Example (False) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, } result := slices.Any(slice, func(u user) bool { return u.Id == 3 }) fmt.Println(result) }
Output: false
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, } result := slices.Any(slice, func(u user) bool { return u.Id == 2 }) fmt.Println(result) }
Output: true
func Backward ¶ added in v0.6.0
Backward create a iter.Seq in backward order.
Example:
slices.Backward([]int{1,2,3}) => seq: 3,2,1
func Chunk ¶
Chunk returns a new slice with the given slice split into smaller slices of the given size.
Example:
slices.Chunk(seq(1,2,3,4,5,6,7,8,9), 3) => [][]int{{1,2,3}, {4,5,6}, {7,8,9}} slices.Chunk(seq(1,2,3,4,5,6,7), 3) => [][]int{{1,2,3}, {4,5,6}, {7}}
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} result := slices.Chunk(slice, 3) fmt.Println(result) }
Output: [[1 2 3] [4 5 6] [7 8 9] [10]]
Example (Empty) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{} result := slices.Chunk(slice, 3) fmt.Println(result) }
Output: []
func Clone ¶ added in v0.1.1
func Clone[T any, S ~[]T](slice S) S
Clone returns a new slice with the same elements as the given slice.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Clone[int, []int](slice) fmt.Println(result) }
Output: [1 2 3]
func Collect ¶ added in v0.5.0
Collect all elements in [Seq] and return a slice.
Example:
slices.Collect(seq(1,2,3)) => []int{1,2,3}
func Contains ¶
func Contains[T comparable, S ~[]T](slice S, v T) bool
Contains returns true if the given slice contains the given element.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Contains(slice, 6) fmt.Println(result) }
Output: false
func ContainsBy ¶
ContainsBy returns true if the given slice contains an element that satisfies the given predicate.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, } result := slices.ContainsBy(slice, func(u user) bool { return u.Id == 2 }) fmt.Println(result) }
Output: true
func DeepClone ¶
DeepClone returns a new slice with the cloned elements.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.DeepCloneBy(slice, func(i int) int { return i }) fmt.Println(result) }
Output: [1 2 3]
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, } result := slices.DeepClone(slice) fmt.Println(result) }
Output: [{1 John} {2 Jane} {2 Jack}]
func DeepCloneBy ¶ added in v0.1.1
func DeepCloneBy[T any, S ~[]T](slice S, clone func(T) T) S
DeepCloneBy returns a new slice with the cloned elements as the given slice.
func Difference ¶ added in v0.5.0
func Difference[T comparable, S ~[]T](lhs S, rhs S) S
Difference returns a new slice with the elements that are in the first slice but not in the second.
Example:
slices.Difference([]int{1,2,3}, []int{3,4,5}) => []int{1,2} slices.Difference([]int{1,2,3}, []int{1,2,3,4,5}) => []int{}
func DifferenceBy ¶ added in v0.1.1
DifferenceBy returns a new slice with the elements that are in the first slice but not in the second by the given function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, {Id: 4, Name: "Bob"}, } slice2 := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 5, Name: "Bob"}, {Id: 6, Name: "Jack"}, } result := slices.DifferenceBy(slice, slice2, func(a, b user) int { return user.Compare(a, b) }) fmt.Println(result) }
Output: [{3 Jack} {4 Bob}]
func Distinct ¶ added in v0.2.0
func Distinct[T comparable, S ~[]T](slice S) S
Distinct returns a new slice with the given slice without duplicates.
Example:
slices.Distinct([]int{1,2,3,2,1}) => []int{1,2,3}
func DistinctBy ¶ added in v0.2.0
func DistinctBy[T any, K comparable, S ~[]T](slice S, key func(T) K) S
DistinctBy returns a new slice with the distinct elements of the given slice by the given function.
func Equal ¶
func Equal[T comparable, S1 ~[]T, S2 ~[]T](lhs S1, rhs S2) bool
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} slice2 := []int{1, 2, 3} result := slices.Equal(slice, slice2) fmt.Println(result) slice3 := []int{1, 2, 3, 4} result2 := slices.Equal(slice, slice3) fmt.Println(result2) slice4 := []int{1, 2, 4} result3 := slices.Equal(slice, slice4) fmt.Println(result3) }
Output: true false false
func EqualBy ¶
Example (Full) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, {Id: 4, Name: "Bob"}, } slice2 := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, {Id: 4, Name: "Bob"}, } result := slices.EqualBy(slice, slice2, func(a, b user) bool { return a.Id == b.Id && a.Name == b.Name }) fmt.Println(result) }
Output: true
Example (Partial) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, {Id: 4, Name: "Bob"}, } slice2 := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, {Id: 4, Name: "Bob"}, } result := slices.EqualBy(slice, slice2, func(a, b user) bool { return a.Id == b.Id }) fmt.Println(result) }
Output: true
func Filter ¶
Filter returns a new slice with all elements that satisfy the given predicate.
Example:
slices.Filter([]int{1,2,3,4,5,6}, func(x int) bool {return x%2 == 0}) => []int{2,4,6}
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Filter(slice, func(i int) bool { return i > 1 }) fmt.Println(result) }
Output: [2 3]
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, } result := slices.Filter(slice, func(u user) bool { return u.Id&1 == 1 }) fmt.Println(result) }
Output: [{1 John} {3 Jack}]
func FilterIndexed ¶ added in v0.2.0
FilterIndexed returns a new slice with all elements that satisfy the given predicate.
func FirstNonZero ¶ added in v0.4.0
func FirstNonZero[T comparable, S ~[]T](slice S) T
FirstNonZero returns first non-zero value
zero value are:
integer and float: 0 bool: false string: empty string, aka: "" pointer: nil pointer struct with all field is zero value interface: nil chan/map/slice: nil
func FlatMap ¶ added in v0.5.0
func FlatMap[T, E any, S ~[]T](slice S, f func(T) []E) []E
FlatMap returns a new slice with all elements in the given slice and all elements in the given slices.
func Flatten ¶
func Flatten[T any, S ~[]T, X ~[]S](slice X) S
Flatten returns a new slice with all elements in the given slice and all elements in all sub-slices.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := [][]int{{1, 2, 3}, {4, 5, 6}} result := slices.Flatten[int](slice) fmt.Println(result) }
Output: [1 2 3 4 5 6]
Example (Empty) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := [][]int{} result := slices.Flatten[int](slice) fmt.Println(result) }
Output: []
func FlattenBy ¶ added in v0.1.1
func FlattenBy[T, E any, S ~[]T](slice S, f func(T) []E) []E
FlattenBy returns a new slice with all elements in the given slice and all elements in the given slices. Deprecated: use FlatMap
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.FlattenBy(slice, func(i int) []int { s := make([]int, i) for j := 0; j < i; j++ { s[j] = j } return s }) fmt.Println(result) }
Output: [0 0 1 0 1 2]
func Fold ¶
func Fold[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) A) A
Fold accumulates value starting with initial value and applying accumulator from left to right to current accum value and each element. Returns the final accum value or initial value if the slice is empty.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Fold(slice, 0, func(acc, i int) int { return acc + i }) fmt.Println(result) }
Output: 6
Example (Diff) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Fold(slice, "", func(acc string, i int) string { return acc + fmt.Sprintf("%d", i) }) fmt.Println(result) }
Output: 123
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, } result := slices.Fold(slice, "", func(acc string, u user) string { return acc + u.Name }) fmt.Println(result) }
Output: JohnJaneJack
func FoldRight ¶ added in v0.2.0
func FoldRight[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) A) A
FoldRight accumulates value starting with initial value and applying accumulator from right to left to current accum value and each element. Returns the final accum value or initial value if the slice is empty.
func ForEach ¶
func ForEach[T any, S ~[]T](slice S, f func(T))
ForEach iterates over the given slice and calls the given function for each element.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} slices.ForEach(slice, func(i int) { fmt.Println(i) }) }
Output: 1 2 3
func ForEachIndexed ¶ added in v0.2.0
ForEachIndexed iterates over the given slice and calls the given function for each element.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} slices.ForEachIndexed(slice, func(i int, v int) { fmt.Println(i, v) }) }
Output: 1 0 2 1 3 2
func Forward ¶ added in v0.6.0
Forward create a iter.Seq in forward order.
Example:
slices.Forward([]int{1,2,3}) => seq: 1,2,3
func GroupBy ¶
func GroupBy[T any, TKey comparable, S ~[]T](slice S, f func(T) TKey) map[TKey]S
GroupBy returns a new map with the given slice split into smaller slices of the given size.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 2, Name: "Jack"}, {Id: 4, Name: "Bob"}, } result := slices.GroupBy(slice, func(u user) int64 { return u.Id }) fmt.Println(result) }
Output: map[1:[{1 John}] 2:[{2 Jane} {2 Jack}] 4:[{4 Bob}]]
func IndexBy ¶ added in v0.2.0
IndexBy returns the index of the first element in the given slice that satisfies the given predicate.
func Intersection ¶ added in v0.3.0
func Intersection[T comparable, S ~[]T](lhs S, rhs S) S
Intersection returns a new slice with the elements that are in both give slices.
func IntersectionBy ¶ added in v0.1.1
IntersectionBy returns a new slice with the elements that are in both given slices by the given function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, {Id: 4, Name: "Bob"}, } slice2 := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 5, Name: "Bob"}, {Id: 6, Name: "Jack"}, } result := slices.IntersectionBy(slice, slice2, func(a, b user) int { if x := cmp.Compare(a.Id, b.Id); x != 0 { return x } return cmp.Compare(a.Name, b.Name) }) fmt.Println(result) }
Output: [{1 John} {2 Jane}]
func IsSorted ¶ added in v0.1.1
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} isSorted := slices.IsSorted(slice) fmt.Println(isSorted) }
Output: true
Example (Not) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{3, 2, 1} isSorted := slices.IsSorted(slice) fmt.Println(isSorted) }
Output: false
func IsSortedBy ¶ added in v0.1.1
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, } isSortedBy := slices.IsSortedBy(slice, user.Compare) fmt.Println(isSortedBy) }
Output: true
Example (Not) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, {Id: 1, Name: "John"}, } isSortedBy := slices.IsSortedBy(slice, user.Compare) fmt.Println(isSortedBy) }
Output: false
func LastIndex ¶ added in v0.2.0
func LastIndex[T comparable, S ~[]T](slice S, v T) optional.Optional[int]
LastIndex returns the index of the last element in the given slice that same with the given element.
func LastIndexBy ¶ added in v0.2.0
LastIndexBy returns the index of the last element in the given slice that satisfies the given predicate.
func Map ¶
func Map[T, U any, S ~[]T](slice S, f func(T) U) []U
Map returns a new slice with the results of applying the given function to each element in the given slice.
Example:
slices.Map([]int{1,2,3}, strconv.Itoa) => []string{"1", "2", "3"}
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Map(slice, func(i int) int { return i * 2 }) fmt.Println(result) }
Output: [2 4 6]
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, } ids := slices.Map(slice, func(u user) int64 { return u.Id }) names := slices.Map(slice, func(u user) string { return u.Name }) fmt.Println(ids) fmt.Println(names) }
Output: [1 2 3] [John Jane Jack]
func MapIndexed ¶ added in v0.2.0
MapIndexed returns a new slice with the results of applying the given function to each element in the given slice.
func Max ¶ added in v0.1.1
Max returns the maximum element in the given slice.
Example:
slices.Max([]int{1,2,1}) => 2
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Max(slice) fmt.Println(result) }
Output: Some(3)
func MaxBy ¶
MaxBy returns the maximum element in the given slice that satisfies the given function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, {Id: 4, Name: "Bob"}, } maxId := slices.MaxBy(slice, func(a, b user) int { return cmp.Compare(a.Id, b.Id) }) maxName := slices.MaxBy(slice, func(a, b user) int { return cmp.Compare(a.Name, b.Name) }) fmt.Println(maxId, maxName) }
Output: Some({Id:4 Name:Bob}) Some({Id:1 Name:John})
func Min ¶ added in v0.1.1
Min returns the minimum element in the given slice.
Example:
slices.Min([]int{1,2,1}) => 1
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Min(slice) fmt.Println(result) }
Output: Some(1)
func MinBy ¶
MinBy returns the minimum element in the given slice that satisfies the given function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, {Id: 4, Name: "Bob"}, } minId := slices.MinBy(slice, func(a, b user) int { return cmp.Compare(a.Id, b.Id) }) minName := slices.MinBy(slice, func(a, b user) int { return cmp.Compare(a.Name, b.Name) }) fmt.Println(minId, minName) }
Output: Some({Id:1 Name:John}) Some({Id:4 Name:Bob})
func None ¶
None returns true if no element in the given slice satisfies the given predicate.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.None(slice, func(i int) bool { return i > 5 }) fmt.Println(result) }
Output: true
func Nth ¶
Nth returns the nth element in the given slice.
If n is negative, it returns the last element plus one. If n is greater than the length of the slice, it returns optional.None.
Example (In) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Nth(slice, 1) fmt.Println(result) }
Output: Some(2)
Example (Not_in) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Nth(slice, 4) fmt.Println(result) }
Output: None
func Partition ¶ added in v0.4.0
Partition split slice into two slices according to a predicate.
The first slice will contain items for which the predicate returned true, and the second slice will contain items for which the predicate returned false.
For Example:
Partition([]int{1, 2, 3}, func(s int) bool { return s % 2 == 0 }) returns: ([2], [1, 3])
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} lhs, rhs := slices.Partition(slice, func(x int) bool { return x%2 == 0 }) fmt.Println(lhs) fmt.Println(rhs) }
Output: [2 4 6 8 10] [1 3 5 7 9]
func Reduce ¶
Reduce returns the result of applying the given function to each element in the given slice.
Example:
slices.Reduce([]int{1,2,3}, func(x, y int) int {return x+y}) => 6
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.Reduce(slice, func(acc, i int) int { return acc + i }) fmt.Println(result) }
Output: Some(6)
Example (None) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{} result := slices.Reduce(slice, func(acc, i int) int { return acc + i }) fmt.Println(result) }
Output: None
Example (User) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, {Id: 3, Name: "Jack"}, } result := slices.Reduce(slice, func(acc, u user) user { return u }) fmt.Println(result) }
Output: Some({Id:3 Name:Jack})
func ReduceRight ¶ added in v0.2.0
ReduceRight returns the result of applying the given function to each element in the given slice.
func Reverse ¶ added in v0.2.0
func Reverse[T any, S ~[]T](slice S) S
Reverse returns a new slice with the elements in the given slice in reverse order.
Example:
slices.Reverse([]int{1,2,3}) => []int{3,2,1}
func Shuffle ¶ added in v0.2.0
func Shuffle[T any, S ~[]T](slice S) S
Shuffle the given slice in-place.
func Single ¶ added in v0.2.0
Single returns the single element, or return an error if the collection is empty or has more than one element.
func Sort ¶ added in v0.1.1
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{3, 1, 2} slices.Sort(slice) fmt.Println(slice) }
Output: [1 2 3]
func SortBy ¶
Example ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) func main() { slice := []int{3, 1, 2} slices.SortBy(slice, cmp.Compare[int]) fmt.Println(slice) }
Output: [1 2 3]
Example (Key) ¶
package main import ( "fmt" "github.com/go-board/std/cmp" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"Name"` } func (u user) Compare(o user) int { if x := cmp.Compare(u.Id, o.Id); x != 0 { return x } return cmp.Compare(u.Name, o.Name) } func (u user) Clone() user { return user{Id: u.Id, Name: u.Name} } func main() { slice := []user{ {Id: 3, Name: "Jack"}, {Id: 1, Name: "John"}, {Id: 2, Name: "Jane"}, } slices.SortBy(slice, user.Compare) fmt.Println(slice) }
Output: [{1 John} {2 Jane} {3 Jack}]
func SpliceFirst ¶ added in v0.3.0
SpliceFirst return first element and rest if len > 0, else return (None, []T)
Example:
slices.SpliceFirst([]int{1,2,3}) => Some(1), []int{2,3} slices.SpliceFirst([]int{}) => None, []int{}
func SpliceLast ¶ added in v0.3.0
SpliceLast return last element and rest if len > 0, else return (None, []T)
Example:
slices.SpliceLast([]int{1,2,3}) => Some(1), []int{1,2} slices.SpliceLast([]int{}) => None, []int{}
func ToHashMap ¶ added in v0.2.0
func ToHashMap[ T any, TKey comparable, TValue any, F ~func(T, int) (TKey, TValue), S ~[]T, ]( slice S, f F, ) map[TKey]TValue
ToHashMap converts the given slice to a map by the given key function.
func ToHashSet ¶ added in v0.2.0
func ToHashSet[T comparable, S ~[]T](slice S) map[T]struct{}
ToHashSet returns a new set with the given slice.
func ToIndexedMap ¶ added in v0.2.0
ToIndexedMap converts the given slice to a map from index to element.
func TryFold ¶ added in v0.5.0
func TryFold[T, A any, S ~[]T](slice S, initial A, accumulator func(A, T) (A, error)) (res A, err error)
TryFold accumulates value starting with initial value and applying accumulator from left to right to current accum value and each element.
Returns the final accum value or initial value if the slice is empty. If error occurred, return error early.
func TryFoldRight ¶ added in v0.5.0
Types ¶
This section is empty.