Documentation
¶
Index ¶
- func All[T any](slice []T, f func(T) bool) bool
- func Any[T any](slice []T, f func(T) bool) bool
- func Chunk[T any](slice []T, chunk int) [][]T
- func Clone[T any](slice []T) []T
- func Contains[T comparable](slice []T, v T) bool
- func ContainsBy[T any](slice []T, v T, cmp func(T, T) bool) bool
- func DeepClone[T clone.Cloneable[T]](slice []T) []T
- func DeepCloneBy[T any](slice []T, clone func(T) T) []T
- func DifferenceBy[T any](lhs []T, rhs []T, eq func(T, T) bool) []T
- func Distinct[T comparable](slice []T) []T
- func DistinctBy[T any, K comparable](slice []T, key func(T) K) []T
- func Equal[T comparable](lhs []T, rhs []T) bool
- func EqualBy[T any](lhs []T, rhs []T, eq func(T, T) bool) bool
- func Filter[T any](slice []T, f func(T) bool) []T
- func FilterIndexed[T any](slice []T, f func(T, int) bool) []T
- func FindIndex[T comparable](slice []T, v T) int
- func FindIndexBy[T any](slice []T, v T, eq func(T, T) bool) int
- func Flatten[T any](slice [][]T) []T
- func FlattenBy[T, S any](slice []T, f func(T) []S) []S
- func Fold[T, A any](slice []T, initial A, accumulator func(A, T) A) A
- func FoldRight[T, A any](slice []T, initial A, accumulator func(A, T) A) A
- func ForEach[T any](slice []T, f func(T))
- func ForEachIndexed[T any](slice []T, f func(T, int))
- func GroupBy[T any, TKey comparable, TValue any](slice []T, group func(T) (TKey, TValue)) map[TKey][]TValue
- func Index[T comparable](slice []T, v T) optional.Optional[int]
- func IndexBy[T any](slice []T, v T, eq func(T, T) bool) optional.Optional[int]
- func IntersectionBy[T any](lhs []T, rhs []T, eq func(T, T) bool) []T
- func IsSorted[T core.Ordered](slice []T) bool
- func IsSortedBy[T any](slice []T, less func(lhs, rhs T) bool) bool
- func LastIndex[T comparable](slice []T, v T) optional.Optional[int]
- func LastIndexBy[T any](slice []T, v T, eq func(T, T) bool) optional.Optional[int]
- func Map[T, U any](slice []T, f func(T) U) []U
- func MapIndexed[T, U any](slice []T, f func(T, int) U) []U
- func Max[T core.Ordered](slice []T) optional.Optional[T]
- func MaxBy[T any](slice []T, less func(T, T) bool) optional.Optional[T]
- func Min[T core.Ordered](slice []T) optional.Optional[T]
- func MinBy[T any](slice []T, less func(T, T) bool) optional.Optional[T]
- func None[T any](slice []T, f func(T) bool) bool
- func Nth[T any](slice []T, n int) optional.Optional[T]
- func Reduce[T any](slice []T, f func(T, T) T) optional.Optional[T]
- func ReduceRight[T any](slice []T, f func(T, T) T) optional.Optional[T]
- func Reverse[T any](slice []T)
- func Shuffle[T any](slice []T)
- func Single[T any](slice []T) result.Result[T]
- func Sort[T core.Ordered](slice []T) []T
- func SortBy[T any](slice []T, less func(lhs, rhs T) bool) []T
- func ToHashMap[T any, TKey comparable, TValue any, F func(T, int) (TKey, TValue)](slice []T, f F) map[TKey]TValue
- func ToHashSet[T comparable](slice []T) map[T]struct{}
- func ToIndexedMap[T any](slice []T) map[int]T
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)
- FindIndex
- FindIndexBy
- 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)
- 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 ¶
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 ¶
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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 Chunk ¶
Chunk returns a new slice with the given slice split into smaller slices of the given size.
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](slice []T) []T
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(slice) fmt.Println(result) }
Output: [1 2 3]
func Contains ¶
func Contains[T comparable](slice []T, 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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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, user{Id: 2}, func(t, u user) bool { return u.Id == t.Id }) 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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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](slice []T, clone func(T) T) []T
DeepCloneBy returns a new slice with the cloned elements as the given slice.
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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) bool { return a.Id == b.Id && a.Name == b.Name }) fmt.Println(result) }
Output: [{3 Jack} {4 Bob}]
func Distinct ¶ added in v0.2.0
func Distinct[T comparable](slice []T) []T
Distinct returns a new slice with the given slice without duplicates.
func DistinctBy ¶ added in v0.2.0
func DistinctBy[T any, K comparable](slice []T, key func(T) K) []T
DistinctBy returns a new slice with the distinct elements of the given slice by the given function.
func Equal ¶
func Equal[T comparable](lhs []T, rhs []T) bool
Equal returns true if the given slices are equal.
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 ¶
EqualBy returns true if the given slices are equal by the given function.
Example (Full) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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 ¶
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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 FindIndex ¶ added in v0.1.1
func FindIndex[T comparable](slice []T, v T) int
FindIndex returns the index of the first element in the given slice that satisfies the given predicate. Deprecated: use Index instead.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{1, 2, 3} result := slices.FindIndex(slice, 2) fmt.Println(result) }
Output: 1
func FindIndexBy ¶
FindIndexBy returns the index of the first element in the given slice that satisfies the given predicate. Deprecated: use IndexBy instead.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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.FindIndexBy(slice, user{Id: 2}, func(t, u user) bool { return u.Id == t.Id }) fmt.Println(result) }
Output: 1
func Flatten ¶
func Flatten[T any](slice [][]T) []T
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(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(slice) fmt.Println(result) }
Output: []
func FlattenBy ¶ added in v0.1.1
func FlattenBy[T, S any](slice []T, f func(T) []S) []S
FlattenBy returns a new slice with all elements in the given slice and all elements in the given slices.
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](slice []T, 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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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](slice []T, 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](slice []T, 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 GroupBy ¶
func GroupBy[T any, TKey comparable, TValue any](slice []T, group func(T) (TKey, TValue)) map[TKey][]TValue
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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, user) { return u.Id, u }) fmt.Println(result) }
Output: map[1:[{1 John}] 2:[{2 Jane} {2 Jack}] 4:[{4 Bob}]]
func Index ¶ added in v0.2.0
func Index[T comparable](slice []T, v T) optional.Optional[int]
Index returns the index of the first element in the given slice that same with the given element.
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 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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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) bool { return a.Id == b.Id && a.Name == b.Name }) fmt.Println(result) }
Output: [{1 John} {2 Jane}]
func IsSorted ¶ added in v0.1.1
IsSorted returns true if the given slice is sorted.
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
IsSortedBy returns true if the given slice is sorted by the given less function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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, func(a, b user) bool { return a.Id < b.Id }) fmt.Println(isSortedBy) }
Output: true
Example (Not) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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, func(a, b user) bool { return a.Id < b.Id }) fmt.Println(isSortedBy) }
Output: false
func LastIndex ¶ added in v0.2.0
func LastIndex[T comparable](slice []T, 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](slice []T, 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 ¶
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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 ¶
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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) bool { return a.Id < b.Id }) maxName := slices.MaxBy(slice, func(a, b user) bool { return 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 ¶
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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) bool { return a.Id < b.Id }) minName := slices.MinBy(slice, func(a, b user) bool { return 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.
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 Reduce ¶
Reduce returns the result of applying the given function to each element in the given slice.
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/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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](slice []T)
Reverse returns a new slice with the elements in the given slice in reverse order.
func Shuffle ¶ added in v0.2.0
func Shuffle[T any](slice []T)
Shuffle returns a new slice with the given slice shuffled.
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
Sort sorts the given slice in-place.
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 ¶
SortBy sorts the given slice in-place by the given less function.
Example ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) func main() { slice := []int{3, 1, 2} slices.SortBy(slice, func(a, b int) bool { return a < b }) fmt.Println(slice) }
Output: [1 2 3]
Example (Key) ¶
package main import ( "fmt" "github.com/go-board/std/slices" ) type user struct { Id int64 `json:"id"` Name string `json:"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, func(a, b user) bool { return a.Id < b.Id }) fmt.Println(slice) }
Output: [{1 John} {2 Jane} {3 Jack}]
func ToHashMap ¶ added in v0.2.0
func ToHashMap[ T any, TKey comparable, TValue any, F func(T, int) (TKey, TValue), ]( slice []T, 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](slice []T) 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.
Types ¶
This section is empty.