seq

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 2 Imported by: 0

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

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

func (s *Sequence[T]) Chunk(size int) [][]T

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

func (s *Sequence[T]) Concat(others ...*Sequence[T]) *Sequence[T]

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

func (s *Sequence[T]) CountBy(iteratee func(T) string) map[string]int

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

func (s *Sequence[T]) Drop(n int) *Sequence[T]

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

func (s *Sequence[T]) DropRight(n int) *Sequence[T]

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

func (s *Sequence[T]) Every(predicate func(T) bool) bool

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

func (s *Sequence[T]) Filter(predicate func(T) bool) *Sequence[T]

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

func (s *Sequence[T]) Find(predicate func(T) bool) (T, bool)

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

func (s *Sequence[T]) FindLast(predicate func(T) bool) (T, bool)

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

func (s *Sequence[T]) First() (T, bool)

First returns the first element of the sequence. Example: seq.New(1, 2, 3).First() -> 1, true

func (*Sequence[T]) Flatten

func (s *Sequence[T]) Flatten() *Sequence[T]

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

func (s *Sequence[T]) ForEach(iteratee func(T)) *Sequence[T]

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

func (s *Sequence[T]) GroupBy(iteratee func(T) string) map[string][]T

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

func (s *Sequence[T]) Includes(value T) bool

Includes checks if a value is in the sequence. Example: seq.New(1, 2, 3).Includes(2) -> true

func (*Sequence[T]) IsEmpty

func (s *Sequence[T]) IsEmpty() bool

IsEmpty checks if the sequence is empty. Example: seq.New().IsEmpty() -> true

func (*Sequence[T]) Join

func (s *Sequence[T]) Join(separator string) string

Join joins all elements of the sequence into a string. Example: seq.New(1, 2, 3).Join(",") -> "1,2,3"

func (*Sequence[T]) KeyBy

func (s *Sequence[T]) KeyBy(iteratee func(T) int) map[int]T

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

func (s *Sequence[T]) Last() (T, bool)

Last returns the last element of the sequence. Example: seq.New(1, 2, 3).Last() -> 3, true

func (*Sequence[T]) Map

func (s *Sequence[T]) Map(fn func(T) T) *Sequence[T]

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

func (s *Sequence[T]) MapTo(fn func(T) interface{}) *Sequence[interface{}]

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

func (s *Sequence[T]) OrderBy(iteratee func(T) int, ascending bool) *Sequence[T]

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

func (s *Sequence[T]) Partition(predicate func(T) bool) [][]T

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

func (s *Sequence[T]) Reject(predicate func(T) bool) *Sequence[T]

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

func (s *Sequence[T]) Reverse() *Sequence[T]

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

func (s *Sequence[T]) Sample() (T, bool)

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

func (s *Sequence[T]) SampleSize(n int) *Sequence[T]

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

func (s *Sequence[T]) Shuffle() *Sequence[T]

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

func (s *Sequence[T]) Size() int

Size returns the size of the sequence. Example: seq.New(1, 2, 3).Size() -> 3

func (*Sequence[T]) Some

func (s *Sequence[T]) Some(predicate func(T) bool) bool

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

func (s *Sequence[T]) SortBy(iteratee func(T) int) *Sequence[T]

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

func (s *Sequence[T]) Take(n int) *Sequence[T]

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

func (s *Sequence[T]) TakeRight(n int) *Sequence[T]

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)

func (*Sequence[T]) Uniq

func (s *Sequence[T]) Uniq() *Sequence[T]

Uniq creates a duplicate-free version of the sequence. Example: seq.New(1, 2, 1, 3).Uniq() -> seq.New(1, 2, 3)

func (*Sequence[T]) Value

func (s *Sequence[T]) Value() []T

Value returns the underlying collection. Example: seq.New(1, 2, 3).Value() -> []int{1, 2, 3}

Jump to

Keyboard shortcuts

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