seq

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package seq provides helpful iterator functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchSlice

func BatchSlice[T any](items []T, eqFunc func(a, b T) bool, maxSize int) iter.Seq[[]T]

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

func CollectCap[T any](src iter.Seq[T], cap int) []T

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

func Indexed[T any](src iter.Seq[T]) iter.Seq2[int, T]

Indexed returns a new key-value pair iterator from a value iterator, where it assigns indices as keys to each value.

func Map

func Map[T any, S any](src iter.Seq[S], fn func(S) T) iter.Seq[T]

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 None

func None[T any]() iter.Seq[T]

None returns an empty sequence.

func Range

func Range(from, to int) iter.Seq[int]

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

func Reduce[T any, S any](src iter.Seq[S], initialValue T, fn func(accum T, value S) T) T

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

func Select[T any](src iter.Seq[T], pred func(T) bool) iter.Seq[T]

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

func Sum[T constr.Numeric](src iter.Seq[T]) T

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

func Times

func Times(count int) iter.Seq[int]

Times returns a sequence of integers from 0 to count.

Example
package main

import (
	"fmt"

	"github.com/mokiat/gog/seq"
)

func main() {
	for v := range seq.Times(3) {
		fmt.Println(v)
	}

}
Output:

0
1
2

Types

This section is empty.

Jump to

Keyboard shortcuts

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