Documentation ¶
Overview ¶
slice is a package of Slice.
Example (AllAny) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 2, 3, 4, 5) b := s.All(func(i int) bool { return i <= 5 }) fmt.Println(b) b = s.Any(func(i int) bool { return i == 3 }) fmt.Println(b) }
Output: true true
Example (BinarySearch) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 3, 5, 7, 9, 11, 13, 15, 17, 19) found := s.BinarySearch(3, func(i1, i2 int) int { if i1 < i2 { return -1 } else if i1 > i2 { return 1 } return 0 }) fmt.Println(found) notFound := s.BinarySearch(8, func(i1, i2 int) int { if i1 < i2 { return -1 } else if i1 > i2 { return 1 } return 0 }) fmt.Println(notFound) }
Output: 1 -1
Example (Chunk) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 3, 5, 7, 9, 11, 13, 15, 17, 19) result := s.Chunk(2) slice.Of(result...).Foreach(func(s slice.Slice[int]) { fmt.Println(s.JoinToString(", ")) }) }
Output: 1, 3 5, 7 9, 11 13, 15 17, 19
Example (Count) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) evens := s.Count(func(i int) bool { return i%2 == 0 }) fmt.Println(evens) }
Output: 5
Example (Filter) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) r := s.Filter(func(i int) bool { return i > 5 }).JoinToString(", ") fmt.Println(r) }
Output: 6, 7, 8, 9, 10, 11
Example (FirstOf) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) r := s.FirstOf(func(i int) bool { return i > 5 }) fmt.Println(r) }
Output: 6
Example (Map) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) r := s.Map(func(index, value int) int { return value * 2 }) fmt.Println(r.JoinToString(", ")) }
Output: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22
Example (Max) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6) r := s.Max(func(i1, i2 int) int { if i1 > i2 { return -1 } else if i1 < i2 { return 1 } return 0 }) fmt.Println(r) }
Output: 11
Example (Min) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6) r := s.Min(func(i1, i2 int) int { if i1 > i2 { return -1 } else if i1 < i2 { return 1 } return 0 }) fmt.Println(r) }
Output: 1
Example (Random) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6) r := s.Random() fmt.Println(r) }
Output: random value of 1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6
Example (Reduce) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6) sum := s.Reduce(func(i1, i2 int) int { return i1 + i2 }, 0) fmt.Println(sum) }
Output: 66
Example (Sort) ¶
package main import ( "fmt" "github.com/snowmerak/generics-for-go/v2/collections/slice" ) func main() { s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6) r := s.Sort(func(i1, i2 int) int { if i1 < i2 { return -1 } else if i1 > i2 { return 1 } return 0 }) fmt.Println(r.JoinToString(", ")) }
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Index ¶
- func GroupBy[T any, K comparable](slice Slice[T], f func(T) K) map[K]Slice[T]
- func Map[T, R any](slice Slice[T], f func(T) R) []R
- type Slice
- func (slice Slice[T]) All(f func(T) bool) bool
- func (slice Slice[T]) Any(f func(T) bool) bool
- func (slice Slice[T]) BinarySearch(value T, f func(T, T) int) int
- func (slice Slice[T]) Chunk(size int) []Slice[T]
- func (slice Slice[T]) Clone() Slice[T]
- func (slice Slice[T]) Count(f func(T) bool) int
- func (slice Slice[T]) Filter(f func(T) bool) Slice[T]
- func (slice Slice[T]) FilterIndex(f func(int) bool) Slice[T]
- func (slice Slice[T]) FirstIndexOf(f func(T) bool) int
- func (slice Slice[T]) FirstOf(f func(T) bool) (r T)
- func (slice Slice[T]) Foreach(f func(T))
- func (slice Slice[T]) JoinToString(sep string) string
- func (slice Slice[T]) LastIndexOf(f func(T) bool) int
- func (slice Slice[T]) LastOf(f func(T) bool) (r T)
- func (slice Slice[T]) Map(f func(int, T) T) Slice[T]
- func (slice Slice[T]) Max(f func(T, T) int) (r T)
- func (slice Slice[T]) Min(f func(T, T) int) (r T)
- func (slice Slice[T]) Random() T
- func (slice Slice[T]) Reduce(f func(T, T) T, initial T) T
- func (slice Slice[T]) Reverse() Slice[T]
- func (slice Slice[T]) Shuffle() Slice[T]
- func (slice Slice[T]) Sort(f func(T, T) int) Slice[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Slice ¶
type Slice[T any] []T
Slice is a []T.
func (Slice[T]) BinarySearch ¶
BinarySearch returns the index of the element in the slice, or -1 if not found.
func (Slice[T]) Filter ¶
Filter returns a new slice containing all elements satisfy the predicate in the slice.
func (Slice[T]) FilterIndex ¶
FilterIndex returns a new slice containing all elements satisfy the predicate with index in the slice.
func (Slice[T]) FirstIndexOf ¶
FirstIndexOf returns the index of the first element in the slice that satisfies the predicate.
func (Slice[T]) FirstOf ¶
FirstOf returns the first element in the slice that satisfies the predicate.
func (Slice[T]) Foreach ¶
func (slice Slice[T]) Foreach(f func(T))
Foreach calls the function f for each element in the slice.
func (Slice[T]) JoinToString ¶
JoinToString returns a string with all elements joined by sep.
func (Slice[T]) LastIndexOf ¶
LastIndexOf returns the index of the last element in the slice that satisfies the predicate.
func (Slice[T]) Random ¶
func (slice Slice[T]) Random() T
Random returns a random element in the slice.
func (Slice[T]) Reduce ¶
func (slice Slice[T]) Reduce(f func(T, T) T, initial T) T
Reduce applies a function against an initial and each element in the slice.