iterator

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package iterator implements composable iterator functions

The package is at the moment mostly build for trying out iterators and their characteristics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Combine

func Combine[L, R any](seq iter.Seq2[L, R]) iter.Seq[tuple.Pair[L, R]]

Combine takes a iter.Seq2 and returns a iter.Seq combining both return values of the given sequence into a [Pair].

Example
package main

import (
	"fmt"
	"slices"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := slices.All([]string{"a", "b", "c"})

	for i := range iterator.Combine(i) {
		fmt.Println(i)
	}

}
Output:

(Pair[int, string]: [0; a])
(Pair[int, string]: [1; b])
(Pair[int, string]: [2; c])

func Concat

func Concat[T any](inputs ...iter.Seq[T]) iter.Seq[T]

Concat creates an iterator which yields the values of all given iterators in order.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	a := iterator.Of(1, 2, 3)
	b := iterator.Of(4)
	c := iterator.Of(5, 6)

	for v := range iterator.Concat(a, b, c) {
		fmt.Println(v)
	}

}
Output:

1
2
3
4
5
6

func Enumerate

func Enumerate[T any](input iter.Seq[T]) iter.Seq2[int, T]

Enumerate takes an iter.Seq and creates an iter.Seq2 which yields an index starting at 0 together with the values yielded by the given iter.Seq.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	it := iterator.Of("a", "b", "c")

	for i, v := range iterator.Enumerate(it) {
		fmt.Printf("%d: %s\n", i, v)
	}

}
Output:

0: a
1: b
2: c

func Filter

func Filter[T any](s iter.Seq[T], condition func(T) bool) iter.Seq[T]

Filter creates a iter.Seq that only yields values where conditions returns true.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func isEven(i int) bool {
	return i%2 == 0
}

func main() {
	s := iterator.Of(1, 2, 3, 4, 5, 6)

	for e := range iterator.Filter(s, isEven) {
		fmt.Println(e)
	}

}
Output:

2
4
6

func FixedWindow

func FixedWindow[T any](values iter.Seq[T], windowsSize int) iter.Seq[[]T]

FixedWindow creates a iter.Seq which yields slices of non overlapping windows of the given values and windowSize.

The yielded slices reuse the same slice, so if you plan to store them, you must copy them first.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := iterator.Of(1, 2, 3, 4, 5)

	for group := range iterator.FixedWindow(i, 2) {
		fmt.Println(group)
	}

}
Output:

[1 2]
[3 4]
[5]

func FromStepTo

func FromStepTo[T constraints.RealNumber](start, step, endExcluded T) iter.Seq[T]

FromStepTo creates an iterator returning the values from start to end, where the value is increased by step every call and end will not be included, even if it is met exactly.

If step is 0, it panics.

If step is the wrong sign (never reaching end), the sign is inverted.

Example
for n := range FromStepTo(1.0, 1.5, 7.0) {
	fmt.Println(n)
}
Output:

1
2.5
4
5.5
Example (Backwards)
for n := range FromStepTo(7.0, -1.5, 1.0) {
	fmt.Println(n)
}
Output:

7
5.5
4
2.5

func FromTo

func FromTo(start, endExcluded int) iter.Seq[int]

FromTo creates an iterator returning the values from start to end exclusive (Half open range).

Example
for n := range FromTo(3, 7) {
	fmt.Println(n)
}
Output:

3
4
5
6

func FromToInclusive

func FromToInclusive(start, endIncluded int) iter.Seq[int]

FromToInclusive creates an iterator returning the values from start to end inclusive (Closed range).

Example
for n := range FromToInclusive(3, 7) {
	fmt.Println(n)
}
Output:

3
4
5
6
7

func Map

func Map[IN, OUT any](in iter.Seq[IN], fn func(IN) OUT) iter.Seq[OUT]

Map applies the given fn to each value from the given iter.Seq and yields the result.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := iterator.Of(2, 4, 6)

	for squared := range iterator.Map(i, func(i int) int { return i * i }) {
		fmt.Println(squared)
	}

}
Output:

4
16
36

func Of

func Of[T any](values ...T) iter.Seq[T]

Of creates a iter.Seq of the given values.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	s := iterator.Of(1, 2, 3, 4, 5)

	for v := range s {
		fmt.Println(v)
	}

}
Output:

1
2
3
4
5

func PickLeft

func PickLeft[L, UNUSED any](seq iter.Seq2[L, UNUSED]) iter.Seq[L]

PickLeft takes a iter.Seq2 and returns a iter.Seq of the left/first elements of the given sequence.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := slices.All([]string{"a", "b", "c"})

	for i := range iterator.PickLeft(i) {
		fmt.Println(i)
	}

}
Output:

0
1
2

func PickRight

func PickRight[R, UNUSED any](seq iter.Seq2[UNUSED, R]) iter.Seq[R]

PickRight takes a iter.Seq2 and returns a iter.Seq of the right/second elements of the given sequence.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := slices.All([]string{"a", "b", "c"})

	for i := range iterator.PickRight(i) {
		fmt.Println(i)
	}

}
Output:

a
b
c

func Reduce

func Reduce[IN, ACC any](input iter.Seq[IN], accumulator *ACC, fn Reducer[ACC, IN])

Reduce takes an iter.Seq and applies fn on all yielded values consecutively. The result is collected in the given accumulator.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
	"github.com/KrischanCS/go-toolbox/iterator/reducer"
)

func main() {
	i := iterator.Of(1, 2, 3, 4, 5)

	var sum int

	iterator.Reduce(i, &sum, reducer.Sum)

	fmt.Println(sum)

}
Output:

15

func SlidingWindow

func SlidingWindow[T any](values iter.Seq[T], windowSize int) iter.Seq[[]T]

SlidingWindow creates a iter.Seq which yields slices with overlapping windows of the given values and windowSize.

The yielded slices reuse the same slice, so if you plan to store them, you must copy them first.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := iterator.Of(1, 2, 3, 4, 5)

	for group := range iterator.SlidingWindow(i, 2) {
		fmt.Println(group)
	}

}
Output:

[1 2]
[2 3]
[3 4]
[4 5]

func Unique

func Unique[T comparable](input iter.Seq[T]) iter.Seq[T]

Unique yields each unique value of input once.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	i := iterator.Of(1, 1, 2, 1, 2, 3, 3, 1)

	for v := range iterator.Unique(i) {
		fmt.Println(v)
	}

}
Output:

1
2
3

func Zip

func Zip[L, R any](left iter.Seq[L], right iter.Seq[R]) iter.Seq[tuple.Pair[L, R]]

Zip create a new iter.Seq from the left and right iterators, which yields pairs of values from both.

The resulting iterator will stop when the shorter of the two iterators stops.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/iterator"
)

func main() {
	numbers := iterator.Of(1, 2, 3, 4)
	letters := iterator.Of("a", "b", "c")

	for pair := range iterator.Zip[int, string](numbers, letters) {
		fmt.Println(pair.First(), pair.Second())
	}

}
Output:

1 a
2 b
3 c

Types

type Reducer added in v0.0.3

type Reducer[ACC, IN any] func(accumulator *ACC, value IN)

Reducer is the function signature for the reducer function. Each call must take in and apply its operation to the accumulator.

Directories

Path Synopsis
Package reducer provides a few often used Reducers for iterator.Reduce
Package reducer provides a few often used Reducers for iterator.Reduce
statistics
Package statistics provides reducers for gathering statistics from an iterator.
Package statistics provides reducers for gathering statistics from an iterator.

Jump to

Keyboard shortcuts

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