Documentation
¶
Overview ¶
Package seq provides utility functions for sequence manipulation. A sequence is a chainable wrapper around a collection that allows for method chaining.
Index ¶
- type Sequence
- func (s *Sequence[T]) Chunk(size int) [][]T
- func (s *Sequence[T]) Concat(others ...*Sequence[T]) *Sequence[T]
- func (s *Sequence[T]) CountBy(iteratee func(T) string) map[string]int
- func (s *Sequence[T]) Drop(n int) *Sequence[T]
- func (s *Sequence[T]) DropRight(n int) *Sequence[T]
- func (s *Sequence[T]) Every(predicate func(T) bool) bool
- func (s *Sequence[T]) Filter(predicate func(T) bool) *Sequence[T]
- func (s *Sequence[T]) Find(predicate func(T) bool) (T, bool)
- func (s *Sequence[T]) FindLast(predicate func(T) bool) (T, bool)
- func (s *Sequence[T]) First() (T, bool)
- func (s *Sequence[T]) Flatten() *Sequence[T]
- func (s *Sequence[T]) ForEach(iteratee func(T)) *Sequence[T]
- func (s *Sequence[T]) GroupBy(iteratee func(T) string) map[string][]T
- func (s *Sequence[T]) Includes(value T) bool
- func (s *Sequence[T]) IsEmpty() bool
- func (s *Sequence[T]) Join(separator string) string
- func (s *Sequence[T]) KeyBy(iteratee func(T) int) map[int]T
- func (s *Sequence[T]) Last() (T, bool)
- func (s *Sequence[T]) Map(fn func(T) T) *Sequence[T]
- func (s *Sequence[T]) MapTo(fn func(T) interface{}) *Sequence[interface{}]
- func (s *Sequence[T]) OrderBy(iteratee func(T) int, ascending bool) *Sequence[T]
- func (s *Sequence[T]) Partition(predicate func(T) bool) [][]T
- func (s *Sequence[T]) Reduce(fn func(interface{}, T) interface{}, initial interface{}) interface{}
- func (s *Sequence[T]) Reject(predicate func(T) bool) *Sequence[T]
- func (s *Sequence[T]) Reverse() *Sequence[T]
- func (s *Sequence[T]) Sample() (T, bool)
- func (s *Sequence[T]) SampleSize(n int) *Sequence[T]
- func (s *Sequence[T]) Shuffle() *Sequence[T]
- func (s *Sequence[T]) Size() int
- func (s *Sequence[T]) Some(predicate func(T) bool) bool
- func (s *Sequence[T]) SortBy(iteratee func(T) int) *Sequence[T]
- func (s *Sequence[T]) Take(n int) *Sequence[T]
- func (s *Sequence[T]) TakeRight(n int) *Sequence[T]
- func (s *Sequence[T]) Uniq() *Sequence[T]
- func (s *Sequence[T]) Value() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Sequence ¶
type Sequence[T comparable] struct { // contains filtered or unexported fields }
Sequence represents a chainable sequence of operations on a collection.
func FromSlice ¶
func FromSlice[T comparable](slice []T) *Sequence[T]
FromSlice creates a new sequence from the given slice. Example: seq.FromSlice([]int{1, 2, 3})
func New ¶
func New[T comparable](values ...T) *Sequence[T]
New creates a new sequence from the given values. Example: seq.New(1, 2, 3)
func (*Sequence[T]) Chunk ¶
Chunk splits the sequence into groups of the specified size. Example: seq.New(1, 2, 3, 4).Chunk(2) -> [][]int{{1, 2}, {3, 4}}
func (*Sequence[T]) Concat ¶
Concat concatenates the sequence with other sequences. Example: seq.New(1, 2).Concat(seq.New(3, 4)) -> seq.New(1, 2, 3, 4)
func (*Sequence[T]) CountBy ¶
CountBy counts the occurrences of elements in the sequence based on the iteratee function. Example: seq.New(1, 2, 3, 4).CountBy(func(n int) string { return "even" if n % 2 == 0 else "odd" }) -> map[string]int{"odd": 2, "even": 2}
func (*Sequence[T]) Drop ¶
Drop creates a slice of the sequence with n elements dropped from the beginning. Example: seq.New(1, 2, 3, 4).Drop(2) -> seq.New(3, 4)
func (*Sequence[T]) DropRight ¶
DropRight creates a slice of the sequence with n elements dropped from the end. Example: seq.New(1, 2, 3, 4).DropRight(2) -> seq.New(1, 2)
func (*Sequence[T]) Every ¶
Every checks if all elements in the sequence satisfy the predicate. Example: seq.New(2, 4, 6).Every(func(n int) bool { return n % 2 == 0 }) -> true
func (*Sequence[T]) Filter ¶
Filter filters the sequence based on a predicate. Example: seq.New(1, 2, 3, 4).Filter(func(n int) bool { return n % 2 == 0 }) -> seq.New(2, 4)
func (*Sequence[T]) Find ¶
Find finds the first element in the sequence that satisfies the predicate. Example: seq.New(1, 2, 3, 4).Find(func(n int) bool { return n > 2 }) -> 3, true
func (*Sequence[T]) FindLast ¶
FindLast finds the last element in the sequence that satisfies the predicate. Example: seq.New(1, 2, 3, 4).FindLast(func(n int) bool { return n > 2 }) -> 4, true
func (*Sequence[T]) First ¶
First returns the first element of the sequence. Example: seq.New(1, 2, 3).First() -> 1, true
func (*Sequence[T]) Flatten ¶
Flatten flattens the sequence a single level deep. Example: seq.New([]int{1, 2}, []int{3, 4}).Flatten() -> seq.New(1, 2, 3, 4)
func (*Sequence[T]) ForEach ¶
ForEach iterates over elements of the sequence and invokes iteratee for each element. Example: seq.New(1, 2, 3).ForEach(func(n int) { fmt.Println(n) })
func (*Sequence[T]) GroupBy ¶
GroupBy creates an object composed of keys generated from the results of running each element of the sequence through iteratee. Example: seq.New(1, 2, 3, 4).GroupBy(func(n int) string { return "even" if n % 2 == 0 else "odd" }) -> map[string][]int{"odd": {1, 3}, "even": {2, 4}}
func (*Sequence[T]) Includes ¶
Includes checks if a value is in the sequence. Example: seq.New(1, 2, 3).Includes(2) -> true
func (*Sequence[T]) IsEmpty ¶
IsEmpty checks if the sequence is empty. Example: seq.New().IsEmpty() -> true
func (*Sequence[T]) Join ¶
Join joins all elements of the sequence into a string. Example: seq.New(1, 2, 3).Join(",") -> "1,2,3"
func (*Sequence[T]) KeyBy ¶
KeyBy creates an object composed of keys generated from the results of running each element of the sequence through iteratee. Example: seq.New(struct{ID int, Name string}{{1, "Alice"}, {2, "Bob"}}).KeyBy(func(u User) int { return u.ID }) -> map[int]struct{ID int, Name string}{1: {1, "Alice"}, 2: {2, "Bob"}}
func (*Sequence[T]) Last ¶
Last returns the last element of the sequence. Example: seq.New(1, 2, 3).Last() -> 3, true
func (*Sequence[T]) Map ¶
Map applies a function to each element in the sequence. Example: seq.New(1, 2, 3).Map(func(n int) int { return n * 2 }) -> seq.New(2, 4, 6)
func (*Sequence[T]) MapTo ¶
MapTo applies a function to each element in the sequence and returns a new sequence of a different type. Example: seq.New(1, 2, 3).MapTo(func(n int) string { return strconv.Itoa(n) }) -> seq.New("1", "2", "3")
func (*Sequence[T]) OrderBy ¶
OrderBy sorts the sequence by the results of running each element through iteratee. Example: seq.New(1, 3, 2).OrderBy(func(n int) int { return n }, true) -> seq.New(1, 2, 3)
func (*Sequence[T]) Partition ¶
Partition splits the sequence into two groups, the first of which contains elements that satisfy the predicate. Example: seq.New(1, 2, 3, 4).Partition(func(n int) bool { return n % 2 == 0 }) -> [][]int{{2, 4}, {1, 3}}
func (*Sequence[T]) Reduce ¶
func (s *Sequence[T]) Reduce(fn func(interface{}, T) interface{}, initial interface{}) interface{}
Reduce reduces the sequence to a single value. Example: seq.New(1, 2, 3).Reduce(func(sum, n int) int { return sum + n }, 0) -> 6
func (*Sequence[T]) Reject ¶
Reject is the opposite of Filter; it returns elements that don't satisfy the predicate. Example: seq.New(1, 2, 3, 4).Reject(func(n int) bool { return n % 2 == 0 }) -> seq.New(1, 3)
func (*Sequence[T]) Reverse ¶
Reverse reverses the order of elements in the sequence. Example: seq.New(1, 2, 3).Reverse() -> seq.New(3, 2, 1)
func (*Sequence[T]) Sample ¶
Sample gets a random element from the sequence. Example: seq.New(1, 2, 3, 4).Sample() -> a random element from the sequence
func (*Sequence[T]) SampleSize ¶
SampleSize gets n random elements from the sequence. Example: seq.New(1, 2, 3, 4).SampleSize(2) -> seq.New(2, 4) (random elements)
func (*Sequence[T]) Shuffle ¶
Shuffle creates a shuffled version of the sequence. Example: seq.New(1, 2, 3, 4).Shuffle() -> seq.New(3, 1, 4, 2) (random order)
func (*Sequence[T]) Size ¶
Size returns the size of the sequence. Example: seq.New(1, 2, 3).Size() -> 3
func (*Sequence[T]) Some ¶
Some checks if any element in the sequence satisfies the predicate. Example: seq.New(1, 2, 3, 4).Some(func(n int) bool { return n > 3 }) -> true
func (*Sequence[T]) SortBy ¶
SortBy sorts the sequence by the results of running each element through iteratee. Example: seq.New(1, 3, 2).SortBy(func(n int) int { return n }) -> seq.New(1, 2, 3)
func (*Sequence[T]) Take ¶
Take creates a slice of the sequence with n elements taken from the beginning. Example: seq.New(1, 2, 3, 4).Take(2) -> seq.New(1, 2)
func (*Sequence[T]) TakeRight ¶
TakeRight creates a slice of the sequence with n elements taken from the end. Example: seq.New(1, 2, 3, 4).TakeRight(2) -> seq.New(3, 4)