Documentation
¶
Overview ¶
Package seq provides helpful iterator functions.
Index ¶
- func BatchSlice[T any](items []T, eqFunc func(a, b T) bool, maxSize int) iter.Seq[[]T]
- func BatchSliceFast[T any](items []T, eqFunc func(items []T, i, j int) bool, maxSize int) iter.Seq[[]T]
- func CollectCap[T any](src iter.Seq[T], cap int) []T
- func Indexed[T any](src iter.Seq[T]) iter.Seq2[int, T]
- func Map[T any, S any](src iter.Seq[S], fn func(S) T) iter.Seq[T]
- func None[T any]() iter.Seq[T]
- func Range(from, to int) iter.Seq[int]
- func Reduce[T any, S any](src iter.Seq[S], initialValue T, fn func(accum T, value S) T) T
- func Select[T any](src iter.Seq[T], pred func(T) bool) iter.Seq[T]
- func Sum[T constr.Numeric](src iter.Seq[T]) T
- func Times(count int) iter.Seq[int]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BatchSlice ¶
BatchSlice groups the elements of the source sequence into batches with the same key, as determined by the key function. In order for this function to work best, it is assumed that items are already sorted according to the key function.
The maxSize parameter can be used to limit the size of the batches. If the maxSize is 0 or negative, then the batches will be of max possible size.
Example ¶
package main
import (
"fmt"
"github.com/mokiat/gog/seq"
)
func main() {
source := []string{
"1Hello", "1World",
"2This", "2Is", "2Longer",
"3Yes",
}
eqFn := func(a, b string) bool {
return a[0] == b[0]
}
for batch := range seq.BatchSlice(source, eqFn, 0) {
fmt.Printf("%#v\n", batch)
}
}
Output: []string{"1Hello", "1World"} []string{"2This", "2Is", "2Longer"} []string{"3Yes"}
func BatchSliceFast ¶
func BatchSliceFast[T any](items []T, eqFunc func(items []T, i, j int) bool, maxSize int) iter.Seq[[]T]
BatchSliceFast is the same as BatchSlice, but it uses a much more performant equality function, which allows one to work with references to items in the slice instead of copies. This can have a huge impact when the items are large.
func CollectCap ¶
CollectCap collects values from src into a new slice with the given capacity preallocated and returns it.
Example ¶
package main
import (
"fmt"
"github.com/mokiat/gog/seq"
)
func main() {
source := seq.Times(4)
target := seq.CollectCap(source, 12)
for _, v := range target {
fmt.Println(v)
}
fmt.Println()
fmt.Println(cap(target))
}
Output: 0 1 2 3 12
func Indexed ¶ added in v0.19.0
Indexed returns a new key-value pair iterator from a value iterator, where it assigns indices as keys to each value.
func Map ¶
Map applies the given transformation function to each element of the source sequence and returns a new sequence with the results.
Example ¶
package main
import (
"fmt"
"github.com/mokiat/gog/seq"
)
func main() {
source := seq.Times(4)
target := seq.Map(source, func(v int) string {
return fmt.Sprintf("item %d", v)
})
for v := range target {
fmt.Println(v)
}
}
Output: item 0 item 1 item 2 item 3
func Range ¶
Range returns a sequence of integers from from (inclusive) to to (inclusive).
If from is greater than to, the sequence will be in descending order.
Example ¶
package main
import (
"fmt"
"github.com/mokiat/gog/seq"
)
func main() {
for v := range seq.Range(1, 3) {
fmt.Println(v)
}
}
Output: 1 2 3
func Reduce ¶ added in v0.15.0
Reduce compacts a sequence into a single value. The provided function is used to perform the reduction starting with the initialValue.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/mokiat/gog/seq"
)
func main() {
source := slices.Values([]int{1, 2, 3})
result := seq.Reduce(source, 10, func(acc, v int) int {
return acc + v
})
fmt.Println(result)
}
Output: 16
func Select ¶ added in v0.15.0
Select applies the given predicate function to each element of the source sequence and returns a new sequence with the elements for which the predicate returned true.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/mokiat/gog/seq"
)
func main() {
source := slices.Values([]int{1, 2, 3, 4, 5})
target := seq.Select(source, func(v int) bool {
return v%2 == 0
})
for v := range target {
fmt.Println(v)
}
}
Output: 2 4
func Sum ¶ added in v0.15.0
Sum is a convenience function that calculates the sum of all elements in the source sequence.
The same can normally be achieved with the Reduce function, but this function is simpler to use and faster.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/mokiat/gog/seq"
)
func main() {
source := slices.Values([]int{1, 2, 3})
result := seq.Sum(source)
fmt.Println(result)
}
Output: 6
Types ¶
This section is empty.