iterx

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 1 Imported by: 2

Documentation

Overview

Example
package main

import (
	"fmt"
	"reflect"
	"strconv"

	"github.com/xoctopus/x/iterx"
)

func main() {
	slices := []int{-1, 0, 1, 2, -1}

	seq := iterx.Map(
		// filter negative numbers
		iterx.FilterSlice(slices, func(x int) bool { return x >= 0 }),
		// add 1 and convert int to string
		func(x int) string { return strconv.Itoa(x + 1) },
	)

	for v := range seq {
		fmt.Println(reflect.TypeOf(v), v)
	}

	fmt.Println(iterx.Values(seq))

}
Output:

string 1
string 2
string 3
[1 2 3]
Example (Iter_break)
package main

import (
	"fmt"
	"iter"
)

// pushes 1..5 to downstream until all numbers pushed or them don't need more
func Push() iter.Seq[int] {
	return func(yield func(int) bool) {
		for i := 1; i <= 5; i++ {
			fmt.Printf("push-iter: yield(%d)\n", i)
			if !yield(i) {
				fmt.Println("push-iter: yield returned false, stop pushing")
				return
			}
		}
	}
}

func main() {
	// pulls values from upstream until meet 3
	pull := func() {
		for v := range Push() {
			fmt.Printf("pull-range: got %d\n", v)
			if v == 3 {
				fmt.Println("pull-range: break. no more is needed")
				break
			}
		}
	}
	pull()

}
Output:

push-iter: yield(1)
pull-range: got 1
push-iter: yield(2)
pull-range: got 2
push-iter: yield(3)
pull-range: got 3
pull-range: break. no more is needed
push-iter: yield returned false, stop pushing

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[V any](seq iter.Seq[V], filter func(V) bool) iter.Seq[V]

Filter returns a filtered iteration from seq by filter

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.Filter(iterx.Slice([]int{0, 1, 2}), func(x int) bool { return x > 0 })
	for v := range seq {
		fmt.Println(v)
		if v == 1 {
			break
		}
	}
}
Output:

1

func FilterSlice

func FilterSlice[V any, E ~[]V](values E, filter func(V) bool) iter.Seq[V]

FilterSlice returns a filtered iteration from values by filter

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.FilterSlice([]int{0, 1, 2}, func(x int) bool { return x > 0 })
	for v := range seq {
		fmt.Println(v)
		if v == 1 {
			break
		}
	}
}
Output:

1

func Map

func Map[I any, O any](seq iter.Seq[I], m func(I) O) iter.Seq[O]

Map returns a mapped iteration from seq by m

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.Map(iterx.Slice([]int{0, 1, 2}), func(x int) int { return x + 2 })
	for v := range seq {
		fmt.Println(v)
		if v == 3 {
			break
		}
	}
}
Output:

2
3

func MapSeq

func MapSeq[K comparable, V any, M ~map[K]V](m M) iter.Seq2[K, V]

MapSeq converts a map to iteration with key and value

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.MapSeq(map[int]string{1: "a"})
	for k, v := range seq {
		fmt.Printf("%d:%s\n", k, v)
		if k == 1 {
			break
		}
	}
}
Output:

1:a

func MapSlice

func MapSlice[I any, E ~[]I, O any](values E, m func(I) O) iter.Seq[O]

MapSlice returns a mapped iteration from values by m

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.MapSlice([]int{0, 1, 2}, func(x int) int { return x + 1 })
	for v := range seq {
		fmt.Println(v)
		if v == 2 {
			break
		}
	}
}
Output:

1
2

func Slice

func Slice[T any, E ~[]T](values E) iter.Seq[T]

Slice converts a slice to iteration

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.Slice([]int{0, 1, 2})
	for v := range seq {
		fmt.Println(v)
		if v == 1 {
			break
		}
	}
}
Output:

0
1

func SliceSeq

func SliceSeq[T any, E ~[]T](values E) iter.Seq2[int, T]

SliceSeq converts a slice to iteration with index yielded

Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/iterx"
)

func main() {
	seq := iterx.SliceSeq([]int{0, 1, 2})
	for k, v := range seq {
		fmt.Printf("%d:%d\n", k, v)
		if k == 0 {
			break
		}
	}
}
Output:

0:0

func Values

func Values[T any](seq iter.Seq[T]) []T

Types

This section is empty.

Jump to

Keyboard shortcuts

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