This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Iterator - Sequence helpersโ
This page lists all operations on sequences, available in the it
lo sub-package.
Lengthโ
Returns the length of a collection by iterating through the entire sequence.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
}
length := it.Length(seq)
// length == 3seq := func(yield func(string) bool) {
// empty sequence
}
length := it.Length(seq)
// length == 0Similar:Prototype:func Length[T any](collection iter.Seq[T]) int
Filterโ
Returns a sequence of all elements for which the predicate function returns true.
Filter
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
_ = yield(5)
}
filtered := it.Filter(seq, func(x int) bool {
return x%2 == 0
})
var result []int
for v := range filtered {
result = append(result, v)
}
// result contains 2, 4 (even numbers)FilterI
FilterI iterates over elements of collection, returning a sequence of all elements predicate returns true for. The predicate function includes the index.
result := it.FilterI(it.Range(1, 6), func(item int, index int) bool {
return item%2 == 0 && index > 1
})
var filtered []int
for v := range result {
filtered = append(filtered, v)
}
// filtered contains [4, 6]Variant:Prototypes:func Filter[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I
func FilterI[T any, I ~func(func(T) bool)](collection I, predicate func(item T, index int) bool) IKeyByโ
Transforms a sequence into a map using a transform function to generate keys.
result := it.KeyBy(it.Range(1, 5), func(item int) string {
return fmt.Sprintf("key-%d", item)
})
// map[string]int{"key-1": 1, "key-2": 2, "key-3": 3, "key-4": 4}Variant:Similar:Prototype:func KeyBy[K comparable, V any](collection iter.Seq[V], transform func(item V) K) map[K]V
Mapโ
Transforms a sequence to another type by applying a transform function to each element.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
}
mapped := it.Map(seq, func(x int) string {
return fmt.Sprintf("item-%d", x)
})
var result []string
for v := range mapped {
result = append(result, v)
}
// result contains "item-1", "item-2", "item-3"MapI
Transforms a sequence to another type by applying a transform function to each element and its index.
seq := func(yield func(int) bool) {
_ = yield(10)
_ = yield(20)
_ = yield(30)
}
mapped := it.MapI(seq, func(x int, index int) string {
return fmt.Sprintf("item-%d-%d", x, index)
})
var result []string
for v := range mapped {
result = append(result, v)
}
// result contains "item-10-0", "item-20-1", "item-30-2"Variant:Similar:Prototypes:func Map[T, R any](collection iter.Seq[T], transform func(item T) R) iter.Seq[R]
func MapI[T, R any](collection iter.Seq[T], transform func(item T, index int) R) iter.Seq[R]Reduceโ
Reduces a collection to a single accumulated value by applying an accumulator function to each element starting with an initial value.
Reduce
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
}
sum := it.Reduce(seq, func(acc int, item int) int {
return acc + item
}, 0)
// sum == 10seq := func(yield func(string) bool) {
_ = yield("hello")
_ = yield("world")
}
concat := it.Reduce(seq, func(acc string, item string) string {
return acc + " " + item
}, "")
// concat == " hello world"ReduceI
Reduces a collection to a single value by iterating through elements and applying an accumulator function that includes the index.
result := it.ReduceI(it.Range(1, 5), func(agg int, item int, index int) int {
return agg + item*index
}, 0)
// 20 (0*0 + 1*1 + 2*2 + 3*3)Variant:Prototypes:func Reduce[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R
func ReduceI[T, R any](collection iter.Seq[T], accumulator func(agg R, item T, index int) R, initial R) RFilterMapโ
Maps elements of a sequence to new values and filters out elements where the callback returns false. Only elements where the second return value is true are included in the result.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
result := lo.FilterMap(seq, func(x int) (string, bool) {
if x%2 == 0 {
return fmt.Sprintf("even-%d", x), true
}
return "", false
})
// iter.Seq[string] yielding "even-2", "even-4"
seq = func(yield func(string) bool) {
yield("a")
yield("")
yield("c")
yield("d")
}
result = lo.FilterMap(seq, func(s string) (int, bool) {
if s != "" {
return len(s), true
}
return 0, false
})
// iter.Seq[int] yielding 1, 1, 1 (length of "a", "c", "d")FilterMapI
Maps elements of a sequence to new values and filters out elements where the callback returns false. The callback receives both the item and its index.
seq := func(yield func(string) bool) {
yield("apple")
yield("banana")
yield("cherry")
}
result := lo.FilterMapI(seq, func(s string, index int) (string, bool) {
if index%2 == 0 {
return fmt.Sprintf("%s-%d", s, index), true
}
return "", false
})
// iter.Seq[string] yielding "apple-0", "cherry-2"Variant:Prototypes:func FilterMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]
func FilterMapI[T, R any](collection iter.Seq[T], callback func(item T, index int) (R, bool)) iter.Seq[R]ForEachโ
Iterates over elements and invokes a transform function for each element.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
}
var result []int
it.ForEach(seq, func(item int) {
result = append(result, item*2)
})
// result contains 2, 4, 6seq := func(yield func(string) bool) {
_ = yield("hello")
_ = yield("world")
}
it.ForEach(seq, func(item string) {
fmt.Println("Item:", item)
})
// Prints: Item: hello
// Item: worldVariant:Similar:Prototype:func ForEach[T any](collection iter.Seq[T], transform func(item T))
RejectMapโ
Maps elements of a sequence to new values and rejects elements where the callback returns true. Only elements where the second return value is false are included in the result.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
result := lo.RejectMap(seq, func(x int) (string, bool) {
if x%2 == 0 {
return fmt.Sprintf("even-%d", x), true // reject even numbers
}
return fmt.Sprintf("odd-%d", x), false
})
// iter.Seq[string] yielding "odd-1", "odd-3"
seq = func(yield func(string) bool) {
yield("a")
yield("")
yield("c")
yield("d")
}
result = lo.RejectMap(seq, func(s string) (int, bool) {
if s == "" {
return 0, true // reject empty strings
}
return len(s), false
})
// iter.Seq[int] yielding 1, 1, 1 (length of "a", "c", "d")Prototype:func RejectMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]
UniqByโ
Returns a sequence with duplicate elements removed based on a transform function.
result := it.UniqBy(it.Range(1, 7), func(item int) int {
return item % 3
})
// [1, 2, 3]Variant:Similar:Prototype:func UniqBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I
Uniqโ
Returns a duplicate-free version of a sequence, removing consecutive duplicates.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(2)
_ = yield(3)
_ = yield(2)
_ = yield(2)
}
uniqueSeq := it.Uniq(seq)
var result []int
for v := range uniqueSeq {
result = append(result, v)
}
// result contains 1, 2, 3, 2 (consecutive duplicates removed)Similar:Prototype:func Uniq[T comparable, I ~func(func(T) bool)](collection I) I
ReduceLastโ
Reduces a collection from right to left, returning a single value.
ReduceLast
result := it.ReduceLast(it.Range(1, 5), func(agg int, item int) int {
return agg - item
}, 0)
// -10 (0 - 4 - 3 - 2 - 1)ReduceLastI
Reduces a collection from right to left, returning a single value. The accumulator function includes the index.
result := it.ReduceLastI(it.Range(1, 5), func(agg int, item int, index int) int {
return agg - item*index
}, 0)
// -20 (0 - 4*3 - 3*2 - 2*1 - 1*0)Variant:Prototypes:func ReduceLast[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R
func ReduceLastI[T, R any](collection iter.Seq[T], accumulator func(agg R, item T, index int) R, initial R) RDropByIndexโ
Removes elements from a collection at the specified indexes.
result := it.DropByIndex(it.Range(1, 6), 1, 3)
// [1, 3, 5]Variant:Prototype:func DropByIndex[T any, I ~func(func(T) bool)](collection I, indexes ...int) I
Chunkโ
Returns a sequence of elements split into groups of length size. The last chunk may be smaller than size.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
_ = yield(5)
}
chunks := it.Chunk(seq, 2)
var result [][]int
for chunk := range chunks {
result = append(result, chunk)
}
// result contains [1, 2], [3, 4], [5]Similar:Prototype:func Chunk[T any](collection iter.Seq[T], size int) iter.Seq[[]T]
Timesโ
Invokes a transform function n times and returns a sequence of the results.
Examples:
seq := it.Times(5, func(index int) int {
return index * 2
})
var result []int
for v := range seq {
result = append(result, v)
}
// result contains 0, 2, 4, 6, 8seq := it.Times(3, func(index int) string {
return fmt.Sprintf("item-%d", index+1)
})
var result []string
for v := range seq {
result = append(result, v)
}
// result contains "item-1", "item-2", "item-3"Similar:Prototype:func Times[T any](count int, transform func(index int) T) iter.Seq[T]
Repeatโ
Creates a sequence that repeats the initial value count times.
result := it.Repeat(3, "hello")
// ["hello", "hello", "hello"]Variant:Similar:Prototype:func Repeat[T lo.Clonable[T]](count int, initial T) iter.Seq[T]
DropLastโ
Drops the last n elements from a sequence. Returns a new sequence without the specified number of trailing elements.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
result := lo.DropLast(seq, 2)
// iter.Seq[int] yielding 1, 2, 3
result = lo.DropLast(seq, 0)
// iter.Seq[int] yielding 1, 2, 3, 4, 5 (unchanged)
result = lo.DropLast(seq, 10)
// iter.Seq[int] yielding nothing (all elements dropped)
seq = func(yield func(string) bool) {
yield("a")
yield("b")
yield("c")
}
result = lo.DropLast(seq, 1)
// iter.Seq[string] yielding "a", "b"Prototype:func DropLast[T any, I ~func(func(T) bool)](collection I, n int) I
GroupByโ
Returns an object composed of keys generated from running each element of collection through a transform function. The value of each key is an array of elements responsible for generating the key.
Examples:
seq := func(yield func(string) bool) {
_ = yield("apple")
_ = yield("banana")
_ = yield("apricot")
_ = yield("blueberry")
}
grouped := it.GroupBy(seq, func(s string) string {
return string(s[0]) // group by first letter
})
// grouped contains map with keys: "a": ["apple", "apricot"], "b": ["banana", "blueberry"]Prototype:func GroupBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U][]T
Sliceโ
Returns a sub-sequence from start index to end index (exclusive).
result := it.Slice(it.Range(1, 10), 2, 5)
// [3, 4, 5]Variant:Similar:Prototype:func Slice[T any, I ~func(func(T) bool)](collection I, start, end int) I
Reverseโ
Reverses a sequence so the first element becomes the last and the last element becomes the first.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
}
reversed := it.Reverse(seq)
var result []int
for v := range reversed {
result = append(result, v)
}
// result contains 4, 3, 2, 1Similar:Prototype:func Reverse[T any, I ~func(func(T) bool)](collection I) I
Dropโ
Drops n elements from the beginning of a sequence.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
_ = yield(5)
}
dropped := it.Drop(seq, 2)
var result []int
for v := range dropped {
result = append(result, v)
}
// result contains 3, 4, 5Similar:Prototype:func Drop[T any, I ~func(func(T) bool)](collection I, n int) I
Count / CountByโ
Counts elements in a collection. Count counts elements equal to a value, CountBy counts elements matching a predicate.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(2)
_ = yield(3)
_ = yield(2)
}
cnt := it.Count(seq, 2)
// cnt == 3seq := func(yield func(string) bool) {
_ = yield("apple")
_ = yield("banana")
_ = yield("apricot")
}
cnt := it.CountBy(seq, func(s string) bool {
return len(s) > 5
})
// cnt == 2 (banana, apricot)Similar:Prototypes:func Count[T comparable](collection iter.Seq[T], value T) int
func CountBy[T any](collection iter.Seq[T], predicate func(item T) bool) intFlatMapโ
Transforms and flattens a sequence to another type. Each element is transformed into a sequence, then all sequences are concatenated.
Examples:
seq := func(yield func([]int) bool) {
_ = yield([]int{1, 2})
_ = yield([]int{3, 4})
_ = yield([]int{5})
}
flattened := it.FlatMap(seq, func(arr []int) iter.Seq[int] {
return func(yield func(int) bool) {
for _, v := range arr {
if !yield(v * 2) {
return
}
}
}
})
var result []int
for v := range flattened {
result = append(result, v)
}
// result contains 2, 4, 6, 8, 10FlatMapI
Transforms and flattens a sequence to another type. Each element is transformed into a sequence with access to the element's index, then all sequences are concatenated.
seq := func(yield func(string) bool) {
_ = yield("a")
_ = yield("b")
_ = yield("c")
}
flattened := it.FlatMapI(seq, func(s string, index int) iter.Seq[string] {
return func(yield func(string) bool) {
for i := 0; i <= index; i++ {
if !yield(fmt.Sprintf("%s-%d", s, i)) {
return
}
}
}
})
var result []string
for v := range flattened {
result = append(result, v)
}
// result contains "a-0", "b-0", "b-1", "c-0", "c-1", "c-2"Variant:Similar:Prototypes:func FlatMap[T, R any](collection iter.Seq[T], transform func(item T) iter.Seq[R]) iter.Seq[R]
func FlatMapI[T, R any](collection iter.Seq[T], transform func(item T, index int) iter.Seq[R]) iter.Seq[R]Subsetโ
Returns a subset of a sequence starting from the specified offset with the given length.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
result := lo.Subset(seq, 1, 3)
// iter.Seq[int] yielding 2, 3, 4
result = lo.Subset(seq, 0, 2)
// iter.Seq[int] yielding 1, 2
result = lo.Subset(seq, 3, 10)
// iter.Seq[int] yielding 4, 5 (returns available elements)
result = lo.Subset(seq, 10, 5)
// iter.Seq[int] yielding nothing (offset beyond sequence)
seq = func(yield func(string) bool) {
yield("a")
yield("b")
yield("c")
yield("d")
}
result = lo.Subset(seq, 1, 2)
// iter.Seq[string] yielding "b", "c"Similar:Prototype:func Subset[T any, I ~func(func(T) bool)](collection I, offset, length int) I
Spliceโ
Inserts elements into a sequence at the specified index. Returns a new sequence with the elements inserted.
seq := func(yield func(int) bool) {
yield(1)
yield(2)
yield(5)
}
result := lo.Splice(seq, 2, 3, 4)
// iter.Seq[int] yielding 1, 2, 3, 4, 5
result = lo.Splice(seq, 0, 0)
// iter.Seq[int] yielding 0, 1, 2, 5 (insert at beginning)
result = lo.Splice(seq, 3, 6, 7)
// iter.Seq[int] yielding 1, 2, 5, 6, 7 (insert at end)
seq = func(yield func(string) bool) {
yield("a")
yield("c")
}
result = lo.Splice(seq, 1, "b")
// iter.Seq[string] yielding "a", "b", "c"
result = lo.Splice(seq, 1, "x", "y")
// iter.Seq[string] yielding "a", "x", "y", "c"Prototype:func Splice[T any, I ~func(func(T) bool)](collection I, index int, elements ...T) I
RepeatByโ
Builds a sequence with values returned by N calls of transform.
result := it.RepeatBy(3, func(index int) string {
return fmt.Sprintf("item-%d", index+1)
})
var output []string
for item := range result {
output = append(output, item)
}
// output contains ["item-1", "item-2", "item-3"]
result2 := it.RepeatBy(5, func(index int) int {
return index * 2
})
var output2 []int
for item := range result2 {
output2 = append(output2, item)
}
// output2 contains [0, 2, 4, 6, 8]Similar:Prototype:func RepeatBy[T any](count int, transform func(index int) T) iter.Seq[T]
Shuffleโ
Returns a sequence of shuffled values using Fisher-Yates algorithm. Note: this requires collecting all elements in memory.
Examples:
seq := func(yield func(int) bool) {
_ = yield(1)
_ = yield(2)
_ = yield(3)
_ = yield(4)
_ = yield(5)
}
shuffled := it.Shuffle(seq)
var result []int
for v := range shuffled {
result = append(result, v)
}
// result contains the same elements in random orderSimilar:Prototype:func Shuffle[T any, I ~func(func(T) bool)](collection I) I
ForEachWhileโ
ForEachWhile iterates over elements of collection and invokes predicate for each element.
The predicate return value decides to continue or break, like do while().collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
called := 0
it.ForEachWhile(collection, func(item int) bool {
called++
return item < 3
})
// called is 3 (elements 1, 2, 3 were processed)ForEachWhileI iterates over elements of collection and invokes predicate for each element with index.
The predicate return value decides to continue or break, like do while().collection := func(yield func(string) bool) {
yield("a")
yield("b")
yield("c")
yield("d")
}
called := 0
it.ForEachWhileI(collection, func(item string, index int) bool {
called++
return index < 2
})
// called is 3 (elements at indices 0, 1, 2 were processed)Variant:Similar:Prototypes:func ForEachWhile[T any](collection iter.Seq[T], predicate func(item T) bool)
func ForEachWhileI[T any](collection iter.Seq[T], predicate func(item T, index int) bool)DropWhileโ
DropWhile drops elements from the beginning of a sequence while the predicate returns true.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
filtered := it.DropWhile(collection, func(x int) bool {
return x < 3
})
var result []int
for item := range filtered {
result = append(result, item)
}
// result contains [3, 4, 5]Similar:Prototype:func DropWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I
DropLastWhileโ
DropLastWhile drops elements from the end of a sequence while the predicate returns true.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
filtered := it.DropLastWhile(collection, func(x int) bool {
return x > 3
})
var result []int
for item := range filtered {
result = append(result, item)
}
// result contains [1, 2, 3]Similar:Prototype:func DropLastWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I
Drainโ
Drain consumes an entire sequence.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
fmt.Println("yielding")
}
it.Drain(collection)
// prints "yielding" three times, sequence is consumedPrototype:func Drain[T any](collection iter.Seq[T])
PartitionByโ
PartitionBy returns a sequence of elements split into groups. The order of grouped values is
determined by the order they occur in collection. The grouping is generated from the results
of running each element of collection through transform.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
yield(6)
}
result := it.PartitionBy(collection, func(x int) int {
return x % 3
})
// result contains [[1, 4], [2, 5], [3, 6]]Similar:Prototype:func PartitionBy[T any, K comparable](collection iter.Seq[T], transform func(item T) K) [][]T
Flattenโ
Flatten returns a sequence a single level deep.
seq1 := func(yield func(int) bool) {
yield(1)
yield(2)
}
seq2 := func(yield func(int) bool) {
yield(3)
yield(4)
}
flattened := it.Flatten([]iter.Seq[int]{seq1, seq2})
var result []int
for item := range flattened {
result = append(result, item)
}
// result contains [1, 2, 3, 4]Similar:Prototype:func Flatten[T any, I ~func(func(T) bool)](collection []I) I
Interleaveโ
Interleave round-robin alternating input sequences and sequentially appending value at index into result.
seq1 := func(yield func(int) bool) {
yield(1)
yield(3)
}
seq2 := func(yield func(int) bool) {
yield(2)
yield(4)
}
seq3 := func(yield func(int) bool) {
yield(5)
yield(6)
}
interleaved := it.Interleave(seq1, seq2, seq3)
var result []int
for item := range interleaved {
result = append(result, item)
}
// result contains [1, 2, 5, 3, 4, 6]Similar:Prototype:func Interleave[T any](collections ...iter.Seq[T]) iter.Seq[T]
Fillโ
Fill replaces elements of a sequence with
initial
value.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
}
filled := it.Fill(collection, 99)
var result []int
for item := range filled {
result = append(result, item)
}
// result contains [99, 99, 99]Similar:Prototype:func Fill[T lo.Clonable[T], I ~func(func(T) bool)](collection I, initial T) I
Rejectโ
Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return true for.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
filtered := it.Reject(collection, func(x int) bool {
return x%2 == 0
})
var result []int
for item := range filtered {
result = append(result, item)
}
// result contains [1, 3]RejectI is the opposite of Filter, this method returns the elements of collection that predicate does not return true for, with index.
collection := func(yield func(string) bool) {
yield("a")
yield("b")
yield("c")
}
filtered := it.RejectI(collection, func(item string, index int) bool {
return index == 1
})
var result []string
for item := range filtered {
result = append(result, item)
}
// result contains ["a", "c"]RejectMap returns a sequence obtained after both filtering and mapping using the given callback function.
The callback function should return two values: the result of the mapping operation and whether the result element should be included or not.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
filtered := it.RejectMap(collection, func(x int) (string, bool) {
return fmt.Sprintf("item-%d", x), x%2 == 0
})
var result []string
for item := range filtered {
result = append(result, item)
}
// result contains ["item-1", "item-3"]Variant:Prototypes:func Reject[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I
func RejectI[T any, I ~func(func(T) bool)](collection I, predicate func(item T, index int) bool) I
func RejectMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]
func RejectMapI[T, R any](collection iter.Seq[T], callback func(item T, index int) (R, bool)) iter.Seq[R]