seq

package
v1.1.0 Latest Latest
Warning

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

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

README

seq - Sequence Utility Functions for Go

The seq package provides utility functions for sequence manipulation in Go. A sequence is a chainable wrapper around a collection that allows for method chaining, enabling functional programming style operations on collections with a fluent interface.

Installation

go get github.com/gflydev/utils/seq

Usage

import "github.com/gflydev/utils/seq"

Functions

Creating Sequences
New

Creates a new sequence from the given values.

seq := seq.New(1, 2, 3)
// seq.Value() returns []int{1, 2, 3}
FromSlice

Creates a new sequence from the given slice.

slice := []int{1, 2, 3}
seq := seq.FromSlice(slice)
// seq.Value() returns []int{1, 2, 3}
Basic Operations
Value

Returns the underlying collection as a slice.

seq := seq.New(1, 2, 3)
values := seq.Value()
// values is []int{1, 2, 3}
First

Returns the first element of the sequence.

seq := seq.New(1, 2, 3)
first, exists := seq.First()
// first is 1, exists is true

emptySeq := seq.New[int]()
first, exists := emptySeq.First()
// first is 0, exists is false
Last

Returns the last element of the sequence.

seq := seq.New(1, 2, 3)
last, exists := seq.Last()
// last is 3, exists is true

emptySeq := seq.New[int]()
last, exists := emptySeq.Last()
// last is 0, exists is false
Transformation
Map

Applies a function to each element in the sequence and returns a new sequence with the results.

Parameters:

  • fn: The function to apply to each element

Returns:

  • *Sequence[T]: A new sequence containing the transformed elements
seq := seq.New(1, 2, 3)
result := seq.Map(func(n int) int { return n * 2 })
// result.Value() returns []int{2, 4, 6}
MapTo

Applies a function to each element in the sequence and returns a new sequence of a different type.

Parameters:

  • fn: The function to apply to each element, converting it to a different type

Returns:

  • *Sequence[any]: A new sequence containing the transformed elements of the new type
// Convert integers to strings
seq := seq.New(1, 2, 3)
result := seq.MapTo(func(n int) any {
    return strconv.Itoa(n)
}) 
// result.Value() returns []any{"1", "2", "3"}
Filter

Creates a new sequence with all elements that pass the test implemented by the provided function.

Parameters:

  • predicate: Function that tests each element; return true to keep the element, false otherwise

Returns:

  • *Sequence[T]: A new sequence containing only the elements that passed the test
// Get only even numbers
seq := seq.New(1, 2, 3, 4)
result := seq.Filter(func(n int) bool { return n%2 == 0 })
// result.Value() returns []int{2, 4}
Reject

Creates a new sequence with all elements that do not pass the test implemented by the provided function.

Parameters:

  • predicate: Function that tests each element; return true to remove the element, false to keep it

Returns:

  • *Sequence[T]: A new sequence containing only the elements that did not pass the test
// Get only odd numbers by rejecting even numbers
seq := seq.New(1, 2, 3, 4)
result := seq.Reject(func(n int) bool { return n%2 == 0 })
// result.Value() returns []int{1, 3}
Reduce

Applies a function against an accumulator and each element in the sequence to reduce it to a single value.

Parameters:

  • fn: Function to execute on each element, taking the accumulator and current value as arguments
  • initial: The initial value of the accumulator

Returns:

  • any: The final accumulated value
// Sum all numbers in the sequence
seq := seq.New(1, 2, 3, 4)
result := seq.Reduce(func(acc any, n int) any {
    return acc.(int) + n
}, 0)
// result is 10
ForEach

Executes a provided function once for each sequence element.

Parameters:

  • iteratee: Function to execute on each element

Returns:

  • *Sequence[T]: The original sequence (for chaining)
seq := seq.New(1, 2, 3)
sum := 0
result := seq.ForEach(func(n int) {
    sum += n
})
// sum is 6
// result is the original sequence (for chaining)
Searching
Includes

Determines whether the sequence includes a certain value.

Parameters:

  • value: The value to search for

Returns:

  • bool: True if the value is found, false otherwise
seq := seq.New(1, 2, 3)
result := seq.Includes(2)
// result is true

result = seq.Includes(4)
// result is false

emptySeq := seq.New[int]()
result = emptySeq.Includes(1)
// result is false
Find

Returns the first element in the sequence that satisfies the provided testing function.

Parameters:

  • predicate: Function to test each element; return true to indicate a match

Returns:

  • T: The first element that satisfies the predicate
  • bool: True if an element was found, false otherwise
seq := seq.New(1, 2, 3, 4)
value, found := seq.Find(func(n int) bool { return n > 2 })
// value is 3, found is true

seq = seq.New(1, 2)
value, found = seq.Find(func(n int) bool { return n > 2 })
// value is 0, found is false

emptySeq := seq.New[int]()
value, found = emptySeq.Find(func(n int) bool { return n > 2 })
// value is 0, found is false
FindLast

Returns the last element in the sequence that satisfies the provided testing function.

Parameters:

  • predicate: Function to test each element; return true to indicate a match

Returns:

  • T: The last element that satisfies the predicate
  • bool: True if an element was found, false otherwise
seq := seq.New(1, 2, 3, 4, 3)
value, found := seq.FindLast(func(n int) bool { return n > 2 })
// value is 3, found is true

seq = seq.New(1, 2)
value, found = seq.FindLast(func(n int) bool { return n > 2 })
// value is 0, found is false

emptySeq := seq.New[int]()
value, found = emptySeq.FindLast(func(n int) bool { return n > 2 })
// value is 0, found is false
Testing
Every

Tests whether all elements in the sequence pass the test implemented by the provided function.

Parameters:

  • predicate: Function to test each element; should return a boolean

Returns:

  • bool: True if all elements pass the test, false otherwise
seq := seq.New(2, 4, 6)
result := seq.Every(func(n int) bool { return n%2 == 0 })
// result is true

seq = seq.New(2, 3, 6)
result = seq.Every(func(n int) bool { return n%2 == 0 })
// result is false

emptySeq := seq.New[int]()
result = emptySeq.Every(func(n int) bool { return n%2 == 0 })
// result is true (vacuously true for empty sequences)
Some

Tests whether at least one element in the sequence passes the test implemented by the provided function.

Parameters:

  • predicate: Function to test each element; should return a boolean

Returns:

  • bool: True if at least one element passes the test, false otherwise
seq := seq.New(1, 2, 3)
result := seq.Some(func(n int) bool { return n%2 == 0 })
// result is true

seq = seq.New(1, 3, 5)
result = seq.Some(func(n int) bool { return n%2 == 0 })
// result is false

emptySeq := seq.New[int]()
result = emptySeq.Some(func(n int) bool { return n%2 == 0 })
// result is false
Information
Size

Returns the number of elements in the sequence.

Returns:

  • int: The number of elements in the sequence
seq := seq.New(1, 2, 3, 4)
size := seq.Size()
// size is 4

seq = seq.New(1)
size = seq.Size()
// size is 1

emptySeq := seq.New[int]()
size = emptySeq.Size()
// size is 0
IsEmpty

Checks if the sequence contains no elements.

Returns:

  • bool: True if the sequence is empty, false otherwise
seq := seq.New(1, 2, 3)
isEmpty := seq.IsEmpty()
// isEmpty is false

emptySeq := seq.New[int]()
isEmpty = emptySeq.IsEmpty()
// isEmpty is true
Manipulation
Reverse

Creates a new sequence with the elements in reverse order.

Returns:

  • *Sequence[T]: A new sequence with elements in reverse order
seq := seq.New(1, 2, 3)
result := seq.Reverse()
// result.Value() returns []int{3, 2, 1}

seq = seq.New(1)
result = seq.Reverse()
// result.Value() returns []int{1}
Uniq

Creates a new sequence with all duplicate elements removed.

Returns:

  • *Sequence[T]: A new sequence with only unique elements
seq := seq.New(1, 2, 1, 3, 2)
result := seq.Uniq()
// result.Value() returns []int{1, 2, 3}

seq = seq.New(1, 2, 3)
result = seq.Uniq()
// result.Value() returns []int{1, 2, 3}
Chunk

Splits the sequence into groups of the specified size.

Parameters:

  • size: The size of each chunk

Returns:

  • [][]T: A slice of slices, where each inner slice is a chunk of the original sequence
seq := seq.New(1, 2, 3, 4)
chunks := seq.Chunk(2)
// chunks is [][]int{{1, 2}, {3, 4}}

seq = seq.New(1, 2, 3, 4, 5)
chunks = seq.Chunk(2)
// chunks is [][]int{{1, 2}, {3, 4}, {5}}

seq = seq.New(1, 2, 3)
chunks = seq.Chunk(5)
// chunks is [][]int{{1, 2, 3}}

emptySeq := seq.New[int]()
chunks = emptySeq.Chunk(2)
// chunks is [][]int{}
Flatten

Flattens the sequence a single level deep.

Note: This is a simplified version that assumes T is already a slice. In a real implementation, reflection would be needed to handle different types.

Returns:

  • *Sequence[T]: A new sequence with elements flattened one level
// Note: This example is conceptual as the current implementation is simplified
seq := seq.New([]int{1, 2}, []int{3, 4})
result := seq.Flatten()
// Would return sequence with [1, 2, 3, 4]
Concat

Concatenates the current sequence with one or more other sequences.

Parameters:

  • others: One or more sequences to concatenate with the current sequence

Returns:

  • *Sequence[T]: A new sequence containing all elements from the current sequence followed by elements from the other sequences
seq1 := seq.New(1, 2)
seq2 := seq.New(3, 4)
result := seq1.Concat(seq2)
// result.Value() returns []int{1, 2, 3, 4}

result = seq.New(1, 2).Concat(seq.New(3), seq.New(4, 5))
// result.Value() returns []int{1, 2, 3, 4, 5}
Take

Creates a new sequence with n elements taken from the beginning of the current sequence.

seq := seq.New(1, 2, 3, 4)
result := seq.Take(2)
// result.Value() returns []int{1, 2}

seq = seq.New(1, 2, 3)
result = seq.Take(5)
// result.Value() returns []int{1, 2, 3}

seq = seq.New(1, 2, 3)
result = seq.Take(0)
// result.Value() returns []int{}
TakeRight

Creates a new sequence with n elements taken from the end of the current sequence.

seq := seq.New(1, 2, 3, 4)
result := seq.TakeRight(2)
// result.Value() returns []int{3, 4}

seq = seq.New(1, 2, 3)
result = seq.TakeRight(5)
// result.Value() returns []int{1, 2, 3}

seq = seq.New(1, 2, 3)
result = seq.TakeRight(0)
// result.Value() returns []int{}
Drop

Creates a new sequence with n elements removed from the beginning of the current sequence.

seq := seq.New(1, 2, 3, 4)
result := seq.Drop(2)
// result.Value() returns []int{3, 4}

seq = seq.New(1, 2, 3)
result = seq.Drop(5)
// result.Value() returns []int{}

seq = seq.New(1, 2, 3)
result = seq.Drop(0)
// result.Value() returns []int{1, 2, 3}

emptySeq := seq.New[int]()
result = emptySeq.Drop(2)
// result.Value() returns []int{}
DropRight

Creates a new sequence with n elements removed from the end of the current sequence.

seq := seq.New(1, 2, 3, 4)
result := seq.DropRight(2)
// result.Value() returns []int{1, 2}

seq = seq.New(1, 2, 3)
result = seq.DropRight(5)
// result.Value() returns []int{}

seq = seq.New(1, 2, 3)
result = seq.DropRight(0)
// result.Value() returns []int{1, 2, 3}

emptySeq := seq.New[int]()
result = emptySeq.DropRight(2)
// result.Value() returns []int{}
Shuffle

Creates a new sequence with elements randomly reordered.

Returns:

  • *Sequence[T]: A new sequence with the same elements in random order
seq := seq.New(1, 2, 3, 4)
result := seq.Shuffle()
// result.Value() might return []int{3, 1, 4, 2}
Sample

Returns a randomly selected element from the sequence.

Returns:

  • T: A randomly selected element from the sequence
  • bool: True if the sequence is not empty and an element was selected, false otherwise
seq := seq.New(1, 2, 3, 4)
value, exists := seq.Sample()
// value might be 2, exists is true

emptySeq := seq.New[int]()
value, exists = emptySeq.Sample()
// value is 0, exists is false
SampleSize

Returns a new sequence with n randomly selected elements from the current sequence.

Parameters:

  • n: The number of elements to randomly select

Returns:

  • *Sequence[T]: A new sequence containing n randomly selected elements
seq := seq.New(1, 2, 3, 4)
result := seq.SampleSize(2)
// result.Value() might return []int{2, 4}

// If n is greater than the sequence size, returns all elements in random order
seq = seq.New(1, 2)
result = seq.SampleSize(3)
// result.Value() returns all elements in random order
Partition

Divides the sequence into two groups: elements that satisfy the predicate and elements that don't.

Parameters:

  • predicate: Function to test each element; return true to include in first group, false for second group

Returns:

  • [][]T: A slice containing two slices: the first with elements that passed the test, the second with elements that failed
seq := seq.New(1, 2, 3, 4)
result := seq.Partition(func(n int) bool {
    return n % 2 == 0
})
// result is [][]int{{2, 4}, {1, 3}}
GroupBy

Creates a map that groups elements by keys generated from the iteratee function.

Parameters:

  • iteratee: Function that returns a string key for each element

Returns:

  • map[string][]T: A map where keys are the strings returned by iteratee and values are slices of elements that produced each key
// Group numbers by even/odd
seq := seq.New(1, 2, 3, 4)
result := seq.GroupBy(func(n int) string {
    if n % 2 == 0 {
        return "even"
    }
    return "odd"
})
// result is map[string][]int{"odd": {1, 3}, "even": {2, 4}}
CountBy

Creates a map that counts elements by keys generated from the iteratee function.

Parameters:

  • iteratee: Function that returns a string key for each element

Returns:

  • map[string]int: A map where keys are the strings returned by iteratee and values are counts of elements that produced each key
// Count numbers by even/odd
seq := seq.New(1, 2, 3, 4)
result := seq.CountBy(func(n int) string {
    if n % 2 == 0 {
        return "even"
    }
    return "odd"
})
// result is map[string]int{"odd": 2, "even": 2}
KeyBy

Creates a map with keys generated by applying the iteratee function to each element.

Parameters:

  • iteratee: Function that returns an integer key for each element

Returns:

  • map[int]T: A map where keys are integers returned by iteratee and values are the elements that produced each key
// Create a map of users keyed by their ID
type User struct {
    ID   int
    Name string
}
users := seq.New(User{1, "Alice"}, User{2, "Bob"})
result := users.KeyBy(func(u User) int {
    return u.ID
})
// result is map[int]User{1: {1, "Alice"}, 2: {2, "Bob"}}
SortBy

Creates a new sequence sorted by the values returned by the iteratee function.

Parameters:

  • iteratee: Function that returns a comparable value used for sorting

Returns:

  • *Sequence[T]: A new sequence with elements sorted by the iteratee function results
// Sort numbers in ascending order
seq := seq.New(1, 3, 2)
result := seq.SortBy(func(n int) int {
    return n
})
// result.Value() returns []int{1, 2, 3}

// Sort users by ID
type User struct {
    ID   int
    Name string
}
users := seq.New(User{3, "Charlie"}, User{1, "Alice"}, User{2, "Bob"})
result := users.SortBy(func(u User) int {
    return u.ID
})
// result.Value() returns users sorted by ID: [{1 Alice} {2 Bob} {3 Charlie}]
OrderBy

Sorts the sequence by the results of running each element through iteratee.

Parameters:

  • iteratee: Function that returns an integer value used for sorting comparison
  • ascending: If true sort in ascending order, if false sort in descending order

Returns:

  • *Sequence[T]: A new sequence with elements sorted based on iteratee results
// Sort numbers in ascending order
seq := seq.New(1, 3, 2)
result := seq.OrderBy(func(n int) int {
    return n
}, true)
// result.Value() returns []int{1, 2, 3}

// Sort users by age in descending order
type User struct {
    Name string
    Age  int
}
users := seq.New(
    User{"Alice", 25},
    User{"Bob", 30},
    User{"Charlie", 20},
)
result := users.OrderBy(func(u User) int {
    return u.Age
}, false)
// result.Value() returns users sorted by age in descending order: 
// [{Bob 30} {Alice 25} {Charlie 20}]
Join

Joins all elements of the sequence into a string.

seq := seq.New(1, 2, 3)
result := seq.Join(",")
// result is "1,2,3"

seq = seq.New(1)
result = seq.Join(",")
// result is "1"

emptySeq := seq.New[int]()
result = emptySeq.Join(",")
// result is ""

Documentation

Overview

Package seq provides utility functions for sequence manipulation. A sequence is a chainable wrapper around a collection that allows for method chaining. It enables functional programming style operations on collections with a fluent interface.

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. It wraps a slice of comparable values and provides methods for manipulating the collection in a functional programming style with method chaining.

func FromSlice

func FromSlice[T comparable](slice []T) *Sequence[T]

FromSlice creates a new sequence from the given slice.

Parameters:

  • slice: The slice to convert into a sequence

Returns:

  • *Sequence[T]: A new sequence containing the elements from the slice

Example:

seq.FromSlice([]int{1, 2, 3}) // Creates a sequence with values [1, 2, 3]

func New

func New[T comparable](values ...T) *Sequence[T]

New creates a new sequence from the given values.

Parameters:

  • values: The values to include in the sequence

Returns:

  • *Sequence[T]: A new sequence containing the provided values

Example:

seq.New(1, 2, 3) // Creates a sequence with values [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.

Parameters:

  • size: The size of each chunk

Returns:

  • [][]T: A slice of slices, where each inner slice is a chunk of the original sequence

Example:

seq.New(1, 2, 3, 4).Chunk(2) // Returns [][]int{{1, 2}, {3, 4}}
seq.New(1, 2, 3, 4, 5).Chunk(2) // Returns [][]int{{1, 2}, {3, 4}, {5}}

func (*Sequence[T]) Concat

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

Concat creates a new sequence by concatenating the current sequence with other sequences.

Parameters:

  • others: One or more sequences to concatenate with the current sequence

Returns:

  • *Sequence[T]: A new sequence containing all elements from the current sequence followed by elements from the other sequences

Example:

seq1 := seq.New(1, 2)
seq2 := seq.New(3, 4)
seq1.Concat(seq2) // Returns sequence with [1, 2, 3, 4]

seq.New(1, 2).Concat(seq.New(3), seq.New(4, 5)) // Returns sequence with [1, 2, 3, 4, 5]

func (*Sequence[T]) CountBy

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

CountBy creates a map that counts elements by keys generated from the iteratee function.

Parameters:

  • iteratee: Function that returns a string key for each element

Returns:

  • map[string]int: A map where keys are the strings returned by iteratee and values are counts of elements that produced each key

Example:

// Count numbers by even/odd
seq.New(1, 2, 3, 4).CountBy(func(n int) string {
    if n % 2 == 0 {
        return "even"
    }
    return "odd"
}) // Returns map[string]int{"odd": 2, "even": 2}

func (*Sequence[T]) Drop

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

Drop creates a new sequence with n elements removed from the beginning of the current sequence.

Parameters:

  • n: The number of elements to exclude from the beginning

Returns:

  • *Sequence[T]: A new sequence with the first n elements removed

Example:

seq.New(1, 2, 3, 4).Drop(2) // Returns sequence with [3, 4]
seq.New(1, 2).Drop(3) // Returns empty sequence (drops all elements)

func (*Sequence[T]) DropRight

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

DropRight creates a new sequence with n elements removed from the end of the current sequence.

Parameters:

  • n: The number of elements to exclude from the end

Returns:

  • *Sequence[T]: A new sequence with the last n elements removed

Example:

seq.New(1, 2, 3, 4).DropRight(2) // Returns sequence with [1, 2]
seq.New(1, 2).DropRight(3) // Returns empty sequence (drops all elements)

func (*Sequence[T]) Every

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

Every tests whether all elements in the sequence pass the test implemented by the provided function.

Parameters:

  • predicate: Function to test each element; should return a boolean

Returns:

  • bool: True if all elements pass the test, false otherwise

Example:

// Check if all numbers are even
seq.New(2, 4, 6).Every(func(n int) bool {
    return n % 2 == 0
}) // Returns true

seq.New(1, 2, 3).Every(func(n int) bool {
    return n % 2 == 0
}) // Returns false

func (*Sequence[T]) Filter

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

Filter creates a new sequence with all elements that pass the test implemented by the provided function.

Parameters:

  • predicate: Function that tests each element; return true to keep the element, false otherwise

Returns:

  • *Sequence[T]: A new sequence containing only the elements that passed the test

Example:

// Get only even numbers
seq.New(1, 2, 3, 4).Filter(func(n int) bool {
    return n % 2 == 0
}) // Returns sequence with [2, 4]

func (*Sequence[T]) Find

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

Find returns the first element in the sequence that satisfies the provided testing function.

Parameters:

  • predicate: Function to test each element; return true to indicate a match

Returns:

  • T: The first element that satisfies the predicate
  • bool: True if an element was found, false otherwise

Example:

// Find the first number greater than 2
value, found := seq.New(1, 2, 3, 4).Find(func(n int) bool {
    return n > 2
}) // Returns 3, true

func (*Sequence[T]) FindLast

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

FindLast returns the last element in the sequence that satisfies the provided testing function.

Parameters:

  • predicate: Function to test each element; return true to indicate a match

Returns:

  • T: The last element that satisfies the predicate
  • bool: True if an element was found, false otherwise

Example:

// Find the last number greater than 2
value, found := seq.New(1, 2, 3, 4).FindLast(func(n int) bool {
    return n > 2
}) // Returns 4, true

func (*Sequence[T]) First

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

First returns the first element of the sequence.

Returns:

  • T: The first element in the sequence
  • bool: True if the sequence is not empty, false otherwise

Example:

value, exists := seq.New(1, 2, 3).First() // Returns 1, true
value, exists := seq.New().First() // Returns zero value, false

func (*Sequence[T]) Flatten

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

Flatten flattens the sequence a single level deep.

Note: This is a simplified version that assumes T is already a slice. In a real implementation, reflection would be needed to handle different types.

Returns:

  • *Sequence[T]: A new sequence with elements flattened one level

Example:

// Note: This example is conceptual as the current implementation is simplified
seq.New([]int{1, 2}, []int{3, 4}).Flatten() // Would return sequence with [1, 2, 3, 4]

func (*Sequence[T]) ForEach

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

ForEach executes a provided function once for each sequence element.

Parameters:

  • iteratee: Function to execute on each element

Returns:

  • *Sequence[T]: The original sequence (for chaining)

Example:

// Print each number in the sequence
seq.New(1, 2, 3).ForEach(func(n int) {
    fmt.Println(n)
}) // Prints 1, 2, 3 and returns the original sequence

func (*Sequence[T]) GroupBy

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

GroupBy creates a map that groups sequence elements by keys generated from the iteratee function.

Parameters:

  • iteratee: Function that returns a string key for each element

Returns:

  • map[string][]T: A map where keys are the strings returned by iteratee and values are slices of elements that produced each key

Example:

// Group numbers by even/odd
seq.New(1, 2, 3, 4).GroupBy(func(n int) string {
    if n % 2 == 0 {
        return "even"
    }
    return "odd"
}) // Returns map[string][]int{"odd": {1, 3}, "even": {2, 4}}

func (*Sequence[T]) Includes

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

Includes determines whether the sequence includes a certain value.

Parameters:

  • value: The value to search for

Returns:

  • bool: True if the value is found, false otherwise

Example:

seq.New(1, 2, 3).Includes(2) // Returns true
seq.New(1, 2, 3).Includes(4) // Returns false

func (*Sequence[T]) IsEmpty

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

IsEmpty checks if the sequence contains no elements.

Returns:

  • bool: True if the sequence is empty, false otherwise

Example:

seq.New().IsEmpty() // Returns true
seq.New(1, 2, 3).IsEmpty() // Returns false

func (*Sequence[T]) Join

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

Join joins all elements of the sequence into a string.

Parameters:

  • separator: The string to insert between joined elements

Returns:

  • string: A string containing all elements joined together with the separator between them

Example:

seq.New(1, 2, 3).Join(",") // Returns "1,2,3"
seq.New("a", "b", "c").Join("-") // Returns "a-b-c"
seq.New(true, false, true).Join(" and ") // Returns "true and false and true"
seq.New[int]().Join(",") // Returns "" (empty string for empty sequence)

func (*Sequence[T]) KeyBy

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

KeyBy creates a map with keys generated by applying the iteratee function to each element.

Parameters:

  • iteratee: Function that returns an integer key for each element

Returns:

  • map[int]T: A map where keys are integers returned by iteratee and values are the elements that produced each key

Example:

// Create a map of users keyed by their ID
type User struct {
    ID   int
    Name string
}
users := seq.New(User{1, "Alice"}, User{2, "Bob"})
users.KeyBy(func(u User) int {
    return u.ID
}) // Returns map[int]User{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.

Returns:

  • T: The last element in the sequence
  • bool: True if the sequence is not empty, false otherwise

Example:

value, exists := seq.New(1, 2, 3).Last() // Returns 3, true
value, exists := seq.New().Last() // Returns zero value, false

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 and returns a new sequence with the results.

Parameters:

  • fn: The function to apply to each element

Returns:

  • *Sequence[T]: A new sequence containing the transformed elements

Example:

seq.New(1, 2, 3).Map(func(n int) int { return n * 2 }) // Returns sequence with [2, 4, 6]

func (*Sequence[T]) MapTo

func (s *Sequence[T]) MapTo(fn func(T) any) *Sequence[any]

MapTo applies a function to each element in the sequence and returns a new sequence of a different type.

Parameters:

  • fn: The function to apply to each element, converting it to a different type

Returns:

  • *Sequence[any]: A new sequence containing the transformed elements of the new type

Example:

// Convert integers to strings
seq.New(1, 2, 3).MapTo(func(n int) any {
    return strconv.Itoa(n)
}) // Returns sequence with ["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.

Parameters:

  • iteratee: Function that returns an integer value used for sorting comparison
  • ascending: If true sort in ascending order, if false sort in descending order

Returns:

  • *Sequence[T]: A new sequence with elements sorted based on iteratee results

Example:

seq.New(1, 3, 2).OrderBy(func(n int) int {
	return n
}, true) // Returns sequence with [1, 2, 3]

// Sort users by age in descending order
type User struct {
	Name string
	Age  int
}
seq.New(
	User{"Alice", 25},
	User{"Bob", 30},
	User{"Charlie", 20},
).OrderBy(func(u User) int {
	return u.Age
}, false) // Returns sequence with [Bob(30), Alice(25), Charlie(20)]

func (*Sequence[T]) Partition

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

Partition divides the sequence into two groups: elements that satisfy the predicate and elements that don't.

Parameters:

  • predicate: Function to test each element; return true to include in first group, false for second group

Returns:

  • [][]T: A slice containing two slices: the first with elements that passed the test, the second with elements that failed

Example:

// Partition into even and odd numbers
seq.New(1, 2, 3, 4).Partition(func(n int) bool {
    return n % 2 == 0
}) // Returns [][]int{{2, 4}, {1, 3}}

func (*Sequence[T]) Reduce

func (s *Sequence[T]) Reduce(fn func(any, T) any, initial any) any

Reduce applies a function against an accumulator and each element in the sequence to reduce it to a single value.

Parameters:

  • fn: Function to execute on each element, taking the accumulator and current value as arguments
  • initial: The initial value of the accumulator

Returns:

  • any: The final accumulated value

Example:

// Sum all numbers in the sequence
seq.New(1, 2, 3).Reduce(func(sum any, n int) any {
    return sum.(int) + n
}, 0) // Returns 6

func (*Sequence[T]) Reject

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

Reject is the opposite of Filter; it creates a new sequence with all elements that do not pass the test.

Parameters:

  • predicate: Function that tests each element; return true to remove the element, false to keep it

Returns:

  • *Sequence[T]: A new sequence containing only the elements that did not pass the test

Example:

// Get only odd numbers by rejecting even numbers
seq.New(1, 2, 3, 4).Reject(func(n int) bool {
    return n % 2 == 0
}) // Returns sequence with [1, 3]

func (*Sequence[T]) Reverse

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

Reverse creates a new sequence with the elements in reverse order.

Returns:

  • *Sequence[T]: A new sequence with elements in reverse order

Example:

seq.New(1, 2, 3).Reverse() // Returns sequence with [3, 2, 1]

func (*Sequence[T]) Sample

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

Sample returns a randomly selected element from the sequence.

Returns:

  • T: A randomly selected element from the sequence
  • bool: True if the sequence is not empty and an element was selected, false otherwise

Example:

// Get a random element
value, exists := seq.New(1, 2, 3, 4).Sample() // Might return 2, true
value, exists := seq.New().Sample() // Returns zero value, false

func (*Sequence[T]) SampleSize

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

SampleSize returns a new sequence with n randomly selected elements from the current sequence.

Parameters:

  • n: The number of elements to randomly select

Returns:

  • *Sequence[T]: A new sequence containing n randomly selected elements

Example:

// Get 2 random elements
seq.New(1, 2, 3, 4).SampleSize(2) // Might return sequence with [2, 4]

// If n is greater than the sequence size, returns all elements in random order
seq.New(1, 2).SampleSize(3) // Returns all elements in random order

func (*Sequence[T]) Shuffle

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

Shuffle creates a new sequence with elements randomly reordered.

Returns:

  • *Sequence[T]: A new sequence with the same elements in random order

Example:

// Result will have the same elements in random order
seq.New(1, 2, 3, 4).Shuffle() // Might return sequence with [3, 1, 4, 2]

func (*Sequence[T]) Size

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

Size returns the number of elements in the sequence.

Returns:

  • int: The number of elements in the sequence

Example:

seq.New(1, 2, 3).Size() // Returns 3
seq.New().Size() // Returns 0

func (*Sequence[T]) Some

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

Some tests whether at least one element in the sequence passes the test implemented by the provided function.

Parameters:

  • predicate: Function to test each element; should return a boolean

Returns:

  • bool: True if at least one element passes the test, false otherwise

Example:

// Check if any number is greater than 3
seq.New(1, 2, 3, 4).Some(func(n int) bool {
    return n > 3
}) // Returns true

seq.New(1, 2, 3).Some(func(n int) bool {
    return n > 3
}) // Returns false

func (*Sequence[T]) SortBy

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

SortBy creates a new sequence sorted by the values returned by the iteratee function.

Parameters:

  • iteratee: Function that returns a comparable value used for sorting

Returns:

  • *Sequence[T]: A new sequence with elements sorted by the iteratee function results

Example:

// Sort numbers in ascending order
seq.New(1, 3, 2).SortBy(func(n int) int {
    return n
}) // Returns sequence with [1, 2, 3]

// Sort users by ID
type User struct {
    ID   int
    Name string
}
seq.New(User{3, "Charlie"}, User{1, "Alice"}, User{2, "Bob"}).SortBy(func(u User) int {
    return u.ID
}) // Returns sequence with users sorted by ID

func (*Sequence[T]) Take

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

Take creates a new sequence with n elements taken from the beginning of the current sequence.

Parameters:

  • n: The number of elements to take

Returns:

  • *Sequence[T]: A new sequence with the first n elements

Example:

seq.New(1, 2, 3, 4).Take(2) // Returns sequence with [1, 2]
seq.New(1, 2).Take(3) // Returns sequence with [1, 2] (takes all available elements)

func (*Sequence[T]) TakeRight

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

TakeRight creates a new sequence with n elements taken from the end of the current sequence.

Parameters:

  • n: The number of elements to take from the end

Returns:

  • *Sequence[T]: A new sequence with the last n elements

Example:

seq.New(1, 2, 3, 4).TakeRight(2) // Returns sequence with [3, 4]
seq.New(1, 2).TakeRight(3) // Returns sequence with [1, 2] (takes all available elements)

func (*Sequence[T]) Uniq

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

Uniq creates a new sequence with all duplicate elements removed.

Returns:

  • *Sequence[T]: A new sequence with only unique elements

Example:

seq.New(1, 2, 1, 3).Uniq() // Returns sequence with [1, 2, 3]

func (*Sequence[T]) Value

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

Value returns the underlying collection as a slice.

Returns:

  • []T: The slice containing all elements in the sequence

Example:

seq.New(1, 2, 3).Value() // Returns []int{1, 2, 3}

Jump to

Keyboard shortcuts

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