iters

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

Go Reference License Go Report Card Release

Iters Package

Overview

The iters package provides a collection of iterators designed to enhance the traversal and transformation of data structures in Go. Leveraging the new iterator support introduced in Go 1.23, this package offers a range of utilities to simplify working with collections.

Features

  • Lazy Evaluation: Efficient processing of elements with lazy evaluation. Process elements only when needed.
  • Chainable Operations: Support for chaining multiple iterator operations for complex data transformations.
  • Range-Based Iteration: Simplified range-based loops for common data structures.

Installation

To install the iters package, run:

go get github.com/elordeiro/go/iters

Import the package in your Go code:

import "github.com/elordeiro/go/iters"

or

import it "github.com/elordeiro/go/iters"

Usage

  • Most of the functions in this package have a func() and func2() version.
  • The func() version will accept any iterable that implements the Iterable interface.
    • The Iterable interface requires the implementation of the Len(), Values(), and All() methods.
    • This package implements the Iterable interface for Seq and ItSlice.
  • The func2() version will accept any iterable that implements the Iterable2 interface.
    • The Iterable2 interface requires the implementation of the Len(), Keys(), Values(), and All() methods.
    • This package implements the Iterable2 interface for Seq2 and ItMap.
  • For iterators that need to return more than two values, the Seq2 type is used with a Pair struct.
  • If an iterator is not yet created, you can pass in a slice or a map to Iterable() or Iterable2() respectively to create one.

Iterator Methods:

  • Len(): Len returns the length of the iterator.
  • Keys(): Keys returns a Seq iterator from the keys of a Seq2 or ItMap.
  • Values(): Values returns a Seq iterator from the values of a Seq, Seq2, ItSlice, or ItMap.
  • All():
    • From a Seq or ItSlice, All returns an index-value Seq2 iterator (i starts at 0).
    • From a Seq2 or ItMap, All returns a key-value Seq2 iterator.
  • Sink(): Sink converts a Seq to iter.Seq to be used with the standard library.
  • String(): String returns a string representation of the iterator.

For converting standard library iterators to be used within this package use:

  • Surf(): Surf function is used to convert an iter.Seq to Seq to be used within this package.
  • Surf2(): Surf2 function is used to convert an iter.Seq2 to Seq2 to be used within this package.

Iterator Functions:

The following functions only have a func() version.
  • Range(): Range returns an iterator over a range of values. It can take either one, two or three arguments.
    • 1 arg: value is the end value and the start value is 0.
    • 2 args: values are the start and end values.
    • 3 args: values are the start, end, and step values.
    • Range automatically determines the direction of the range, and panics if it detects an infinite loop.
  • Count(): Count returns an iterator that counts up from a start value.
  • CountDown(): CountDown returns an iterator that counts down from a start value.
The following functions have a func2() version.

Itertools:

  • Enumerate(): Enumerate returns a Seq2 iterator with an index attached to each value starting from a given index.
    • For a Seq or ItSlice, it returns an index-value iterator.
    • For a Seq2 or ItMap, it returns an index-pair iterator.
  • Zip(): Zip returns a Seq2 iterator with elements from two iterators.
    • For 2 Seqs or ItSlices, it returns a value-value iterator.
    • For 2 Seq2s or ItMaps, it returns a pair-pair iterator.
  • ForEach(): ForEach consumes the entire iterator and calls a function with each element.
  • Repeat(): Repeat returns an iterator that repeats a value indefinitely.
  • Cycle(): Cycles returns an iterator that repeats the elements of the given iterator indefinitely.
  • Chain(): Chain returns an iterator that chains multiple iterators together.
  • Backwards(): Backwards returns an iterator that iterates over the elements in reverse order.
  • Take(): Take returns the first n elements of the iterator.
  • Drop(): Drop returns an iterator that skips the first n elements.
  • TakeBetween(): TakeBetween returns an iterator that takes elements between two indices (inclusive).
  • Rotate(): Rotate returns an iterator that rotates the elements of the iterator, shifting them to the left or right.

Functional:

  • Filter(): Filter returns an iterator whose elements satisfy the predicate.
  • Map(): Map returns an iterator that applies a function to each element.
  • Reduce(): Reduce returns a single value by applying a function to each element.
  • TakeWhile(): TakeWhile returns an iterator that takes elements from the iterator while the predicate is true.
  • DropWhile(): DropWhile returns an iterator that drops elements from the iterator while the predicate is true.
  • With(): With returns an iterator that calls a function with each element.
  • OnEmpty(): Else returns an iterator that calls a function if the iterator is exhausted.
Convenience Functions:

Seq2 functions:

  • Split(): Splits takes an Iterable[Pair[K, V]] and returns a Seq2[K, V].
  • SwapKV(): Swaps the key and value of a Seq2 iterator.

Seq functions:

  • Sum(): Sum returns the sum of the elements in the iterator.
  • Product(): Product returns the product of the elements in the iterator.
  • Min(): Min returns the minimum element in the iterator.
  • Max(): Max returns the maximum element in the iterator.
  • All(): All returns true if all elements in the iterator satisfy the predicate.
  • Any(): Any returns true if any element in the iterator satisfies the predicate.
  • None(): None returns true if no element in the iterator satisfies the predicate.

Examples

Basic iterators

Here's are some examples of how to use the iters package:

package main

import (
    "fmt"
    it "github.com/elordeiro/go/iters"
)

func main() {
    for v := range it.Range(1, 6) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    // Output: 1 2 3 4 5

    slice := it.Iterable([]bool{true, false, true, false, true})
    fmt.Println(it.Any(slice.Values()))
    // Output: true

    result := ""
    i := 31
    do := func() { result = fmt.Sprint(i, " is prime") }
    for range it.OnEmpty(it.Range(2, i), do) {
        if i%2 == 0 {
            result = fmt.Sprint(i, " is not prime")
            break
        }
    }
    fmt.Println(result)
    // Output: 31 is prime
}
Chaining operations

You can chain multiple operations using the iterators:

package main

import (
    "fmt"
    it "github.com/elordeiro/go/iters"
)

func main() {
    sqr := func(x int) int { return x * x }
    for v := range it.Map(it.Range(6), sqr) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    // Output: 1 4 9 16 25

    sum := func(a, b int) int { return a + b }
    result := it.Reduce(it.Range(1, 6), sum)
    fmt.Println(result)
    // Output: 15

    for v := range it.Take(9, it.Cycle(it.Range(0, 3))) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    // Output: 0 1 2 0 1 2 0 1 2

    for v := range it.Chain(it.Range(2), it.Range(2, 5), it.Range(5, 10, 2)) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    // Output: 0 1 2 3 4 5 7 9
}
Using other iterable types
package main

import (
    "fmt"
    it "github.com/elordeiro/go/iters"
)


func main() {
    type IntSlice []int

    slice := it.Iterable(IntSlice{1, 2, 3, 4, 5})
    fmt.Println(slice)
    // Output: ItSlice[int][1 2 3 4 5]

    slice2 := it.Iterable([]string{"a", "b", "c"})
    for i, v := range it.Enumerate(1, slice2) {
        fmt.Printf("%d:%s ", i, v)
    }
    fmt.Println()
    // Output: 1:a 2:b 3:c

    seq1 := it.Iterable([]int{1, 2, 3})
    seq2 := it.Iterable([]string{"a", "b", "c"})
    for v1, v2 := range it.Zip(seq1, seq2) {
        fmt.Printf("%d:%s ", v1, v2)
    }
    fmt.Println()
    // Output: 1:a 2:b 3:c

    slice3 := it.Iterable([]int{0, 1, 2, 3})
    slice4 := it.Iterable([]int{4, 5, 6, 7})
    for v := range it.Chain(slice3, slice4) {
        fmt.Print(v, " ")
    }
    fmt.Println()
    // Output: 0 1 2 3 4 5 6 7

    m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
    isEven := func(k string, v int) bool { return v%2 == 0 }
    for k, v := range it.Filter2(m, isEven) {
        fmt.Printf("%s:%d ", k, v)
    }
    fmt.Println()
    // Output: b:2
}

Notes

  • The iters package is still under development and may change in the future.
  • The package is designed to be used with the new iterator support introduced in Go 1.23. It will not work with older versions of Go.
  • The package 'surfaces' the standard library iterators to be used within the package. If you need interact with the standard library, you can use the Sink() methods. If you have a standard library iterator and want to use it within the package, you can use the Surf() and Surf2() functions.

Contributing

Contributions to the iters package are welcome. If you have an idea for a new iterator or an improvement, please open an issue or submit a pull request.

License

This project is licensed under the BSD-3 License. See the LICENSE file for details.


Documentation

Overview

Iters package provides a set of functions that operate on iterators.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](seq Seq[V], predicate ...func(V) bool) bool

All returns true if all values in the iterator satisfy the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]bool{true, true, true, true, true})
	fmt.Println(it.All(slice.Values()))
}
Output:

true

func Any added in v0.1.1

func Any[V any](seq Seq[V], predicate ...func(V) bool) bool

Any returns true if any value in the iterator satisfies the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]bool{true, false, true, false, true})
	fmt.Println(it.Any(slice.Values()))
}
Output:

true

func ForEach added in v0.1.1

func ForEach[V any](seq IIterable[V], do func(V))

ForEach applies a function to each value in the iterator. This function is not lazy and will consume the entire iterator. Note that break, continue, and return statements are not supported in the function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	it.ForEach(slice, func(v int) {
		fmt.Print(2*v, " ")
	})
	fmt.Println()
}
Output:

2 4 6 8 10

func ForEach2 added in v0.1.1

func ForEach2[K, V any](seq IIterable2[K, V], do func(K, V))

ForEach2 applies a function to each key-value pair in the iterator. This function is not lazy and will consume the entire iterator. Note that break, continue, and return statements are not supported in the function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}))
	it.ForEach2(seq2, func(k, v int) {
		fmt.Printf("%d:%d ", k, v*2)
	})
	fmt.Println()
}
Output:

1:4 3:8 5:12

func Max added in v0.1.1

func Max[V Ord](seq Seq[V]) V

Max returns the maximum value in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(it.Max(slice.Values()))
}
Output:

5

func Min added in v0.1.1

func Min[V Ord](seq Seq[V]) V

Min returns the minimum value in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(it.Min(slice.Values()))
}
Output:

1

func None added in v0.1.1

func None[V any](seq Seq[V], predicate ...func(V) bool) bool

None returns true if no value in the iterator satisfies the predicate. If no predicate is provided, it defaults to checking if a value is a zero value for its type.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]bool{true, false, true, false, true})
	fmt.Println(it.None(slice.Values()))
}
Output:

false

func Product added in v0.1.1

func Product[V Integer](seq Seq[V]) V

Product returns the product of all values in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(it.Product(slice.Values()))
}
Output:

120

func Reduce added in v0.1.1

func Reduce[V any](seq IIterable[V], reduce func(V, V) V, start ...V) V

Reduce returns a single value that is the result of applying the reduce function to all values in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	result := it.Reduce(it.Range(5), sum)
	fmt.Println(result)
}
Output:

10
Example (Start)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	result := it.Reduce(it.Range(5), sum, 10)
	fmt.Println(result)
}
Output:

20

func Reduce2 added in v0.1.1

func Reduce2[K, V any](seq IIterable2[K, V], reduce func(V, V) V, start ...V) V

Reduce returns a single value that is the result of applying the reduce function to all values in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	seq2 := it.Range(5).All()
	result := it.Reduce2(seq2, sum)
	fmt.Println(result)
}
Output:

10
Example (Start)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	sum := func(acc, v int) int { return acc + v }
	seq2 := it.Range(5).All()
	result := it.Reduce2(seq2, sum, 10)
	fmt.Println(result)
}
Output:

20

func Sum added in v0.1.1

func Sum[V Ord](seq Seq[V]) V

Sum returns the sum of all values in the iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(it.Sum(slice.Values()))
}
Output:

15

Types

type IIterable added in v0.1.1

type IIterable[V any] interface {
	Len() int
	Values() Seq[V]
	All() Seq2[int, V]
}

IIterable is an interface that defines the methods for a sequence of values or index-value pairs.

type IIterable2 added in v0.1.1

type IIterable2[K, V any] interface {
	Len() int
	Keys() Seq[K]
	Values() Seq[V]
	All() Seq2[K, V]
}

IIterable2 is an interface that defines the methods of Iterable and adds a method to get the keys.

type Integer added in v0.1.1

type Integer constraints.Integer // Integer constraint

type ItMap added in v0.1.1

type ItMap[K comparable, V any] map[K]V // Generic map type

func Iterable2

func Iterable2[M ~map[K]V, K comparable, V any](m M) ItMap[K, V]

Iterable2 returns a new ItMap. ItMaps can be passed to any function that also accepts Seq2[K, V] iterators, while preserving the underlying map, which means they can still be indexed with keys.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	for k, v := range m {
		fmt.Printf("%s:%d\n", k, v)
	}
}
Output:

a:1
b:2
c:3
Example (Custom)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	type StringIntMap map[string]int
	m := it.Iterable2(StringIntMap{"a": 1, "b": 2, "c": 3})
	for k, v := range m {
		fmt.Printf("%s:%d\n", k, v)
	}
	fmt.Println()
}
Output:

a:1
b:2
c:3

func (ItMap[K, V]) All added in v0.1.1

func (m ItMap[K, V]) All() Seq2[K, V]

All returns a Seq2[K, V] iterator over the key-value pairs of the map.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	all := m.All()
	keys := slices.Collect(all.Keys().Sink())
	// map order is not guaranteed
	slices.Sort(keys)
	values := slices.Collect(all.Values().Sink())
	// map order is not guaranteed
	slices.Sort(values)
	fmt.Println(keys, values)
}
Output:

[a b c] [1 2 3]

func (ItMap[K, V]) Keys added in v0.1.1

func (m ItMap[K, V]) Keys() Seq[K]

Keys returns a Seq[K] iterator over the keys of the map.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	slice := slices.Collect(m.Keys().Sink())
	// map order is not guaranteed
	slices.Sort(slice)
	fmt.Println(slice)
}
Output:

[a b c]

func (ItMap[K, V]) Len added in v0.1.1

func (m ItMap[K, V]) Len() int

Len returns the length of the underlying map.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	fmt.Println(m.Len())
}
Output:

3

func (ItMap[K, V]) String added in v0.1.1

func (m ItMap[K, V]) String() string

String returns a string representation of a ItMap[K, V] iterator.

Example
package main

import (
	"fmt"
	"strings"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	mapStr := fmt.Sprint(m)
	typeInfoIdx := strings.Index(mapStr, "]") + 2
	typeInfo := mapStr[:typeInfoIdx]
	fmt.Println(typeInfo)
	parts := strings.Split(mapStr[typeInfoIdx:len(mapStr)-1], " ")
	for _, part := range parts {
		fmt.Println(part)
	}
	fmt.Println("]")
}
Output:

ItMap[string,int][
a:1
b:2
c:3
]

func (ItMap[K, V]) Values added in v0.1.1

func (m ItMap[K, V]) Values() Seq[V]

Values returns a Seq[V] iterator over the values of the map.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	m := it.Iterable2(map[string]int{"a": 1, "b": 2, "c": 3})
	slice := slices.Collect(m.Values().Sink())
	// map order is not guaranteed
	slices.Sort(slice)
	fmt.Println(slice)
}
Output:

[1 2 3]

type ItSlice added in v0.1.1

type ItSlice[V any] []V // Generic slice type

func Iterable

func Iterable[S ~[]V, V any](s S) ItSlice[V]

Iterable returns a new ItSlice. ItSlices can be passed to any function that also accepts Seq[V] iterators, while preserving the underlying slice, which means they can still be indexed and sliced.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(slice)
}
Output:

ItSlice[int][1 2 3 4 5]
Example (Custom)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	type IntSlice []int
	slice := it.Iterable(IntSlice{1, 2, 3, 4, 5})
	fmt.Println(slice)
}
Output:

ItSlice[int][1 2 3 4 5]

func (ItSlice[V]) All added in v0.1.1

func (s ItSlice[V]) All() Seq2[int, V]

All returns a Seq2[int, V] iterator over index-value pairs of the slice.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(slice.All())
}
Output:

Seq2[int,int][0:1 1:2 2:3 3:4 4:5]

func (ItSlice[V]) Len added in v0.1.1

func (s ItSlice[V]) Len() int

Len returns the length of the underlying slice.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(slice.Len())
}
Output:

5

func (ItSlice[V]) String added in v0.1.1

func (s ItSlice[V]) String() string

String returns a string representation of a ItSlice[V] iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(slice)
}
Output:

ItSlice[int][1 2 3 4 5]

func (ItSlice[V]) Values added in v0.1.1

func (s ItSlice[V]) Values() Seq[V]

Values returns a Seq[V] iterator over the values of the slice.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(slice.Values())
}
Output:

Seq[int][1 2 3 4 5]

type Ord added in v0.1.1

type Ord constraints.Ordered // Ordered constraint

type Pair

type Pair[K, V any] struct {
	Key   K
	Value V
}

Pair is a key-value pair for Seq2 iterators that need to return more than 2 values.

func (Pair[K, V]) String

func (p Pair[K, V]) String() string

String returns a string representation of a Pair.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})
	fmt.Println(seq)
}
Output:

ItSlice[iters.Pair[int,int]][1:2 3:4 5:6]

type Seq

type Seq[V any] iter.Seq[V] // Same as iter.Seq[V] but accessible in this package

func Backwards added in v0.1.1

func Backwards[V any](seq IIterable[V]) Seq[V]

Backwards returns a Seq[V] iterator that iterates over the values in reverse order. If seq is an iterator, it takes O(n) time and space to collect all the values.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Backwards(it.Range(5)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

4 3 2 1 0

func Chain

func Chain[V any](seqs ...IIterable[V]) Seq[V]

Chain returns a Seq[V] iterator that chains the values of multiple input iterators.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Chain(it.Range(2), it.Range(2, 5), it.Range(5, 10, 2)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 3 4 5 7 9
Example (Second)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice1 := it.Iterable([]int{0, 1, 2, 3})
	slice2 := it.Iterable([]int{4, 5, 6, 7})
	for v := range it.Chain(slice1, slice2) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 3 4 5 6 7

func Count

func Count[I Integer](i I) Seq[I]

Count returns an iterator that counts up from a given number. The iterator is infinite unless it is stopped by the caller.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	max := 5
	for v := range it.Count(0) {
		if v >= max {
			break
		}
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 3 4

func CountDown added in v0.1.1

func CountDown[I Integer](i I) Seq[I]

CountDown returns an iterator that counts down from a given number. The iterator is infinite unless it is stopped by the caller.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.CountDown(5) {
		if v <= 0 {
			break
		}
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

5 4 3 2 1

func Cycle added in v0.1.1

func Cycle[V any](seq IIterable[V]) Seq[V]

Cycle returns a Seq[V] iterator that cycles through the values of the input iterator. The iterator is infinite unless it is stopped by the caller.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	max := 5
	i := 0
	for v := range it.Cycle(it.Range(3)) {
		if i >= max {
			break
		}
		fmt.Print(v, " ")
		i++
	}
	fmt.Println()
}
Output:

0 1 2 0 1

func Drop added in v0.1.1

func Drop[I Integer, V any](n I, seq IIterable[V]) Seq[V]

Drop returns a Seq[V] iterator that skips the first n values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Drop(3, it.Range(10)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

3 4 5 6 7 8 9
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Drop(10, it.Range(3)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

func DropWhile added in v0.1.1

func DropWhile[V any](seq IIterable[V], predicate func(V) bool) Seq[V]

DropWhile returns a Seq[V] iterator that skips values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isLessThan5 := func(v int) bool { return v < 5 }
	for v := range it.DropWhile(it.Range(10), isLessThan5) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

5 6 7 8 9

func Filter added in v0.1.1

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

Filter returns a Seq[V] iterator over values that satisfy the filter function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isEven := func(v int) bool { return v%2 == 0 }
	for v := range it.Filter(it.Range(5), isEven) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 2 4
Example (Inline)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Filter(it.Range(5), func(v int) bool { return v%2 == 0 }) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 2 4

func Map

func Map[V any](seq IIterable[V], mapf func(V) V) Seq[V]

Map returns a Seq[V] iterator over values that are transformed by the map function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	double := func(v int) int { return v * 2 }
	for v := range it.Map(it.Range(5), double) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 2 4 6 8
Example (Inline)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Map(it.Range(5), func(v int) int { return v * v }) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 4 9 16

func OnEmpty added in v0.1.1

func OnEmpty[V any](seq IIterable[V], callback func()) Seq[V]

OnEmpty returns a Seq[V] iterator that calls an else function only if the iterator is exhausted. Similar to a for ... else block in Python.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	result := ""
	i := 31
	Else := func() { result = fmt.Sprint(i, " is prime") }
	for range it.OnEmpty(it.Range(2, i), Else) {
		if i%2 == 0 {
			result = fmt.Sprint(i, " is not prime")
			break
		}
	}
	fmt.Println(result)
}
Output:

31 is prime
Example (True)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	Else := func() { slice = []int{100} }
	for i, v := range it.Enumerate(0, it.OnEmpty(slice, Else)) {
		if v == 10 {
			break
		}
		slice[i] = v * i
	}
	fmt.Println(slice)
}
Output:

ItSlice[int][100]

func Range

func Range[S Signed](val S, vals ...S) Seq[S]

Range returns an iterator over a range of integers. If only one argument is provided, it is the end of the range. If two arguments are provided, they are the start and end of the range. If three arguments are provided, they are the start, end, and step of the range. If an infinite loop is detected, the function panics.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Range(5) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 3 4
Example (Reverse)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Range(5, 0) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

5 4 3 2 1
Example (StartEnd)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Range(2, 5) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

2 3 4
Example (StartEndStep)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Range(2, 10, 2) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

2 4 6 8

func Repeat

func Repeat[I Integer, V any](val V, n I) Seq[V]

Repeat returns a Seq[V] iterator that yields the same value n times. If n is negative, the iterator is infinite.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Repeat(3, 5) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

3 3 3 3 3

func Rotate added in v0.1.1

func Rotate[I Integer, V any](n I, seq IIterable[V], len ...I) Seq[V]

Rotate returns a Seq[V] iterator that rotates the values of the input iterator by n steps. A positive n rotates the values to the left, and a negative n rotates the values to the right. If seq is an iterator, it is advised to pass the length of it as the third argument to avoid a call to Len() which is an O(n) operation.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Rotate(2, it.Range(5)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

2 3 4 0 1
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Rotate(-2, it.Range(5)) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

3 4 0 1 2

func Surf

func Surf[V any](seq iter.Seq[V]) Seq[V]

Surf Surfaces a Seq[V] iterator. It takes an iter.Seq[V] iterator and returns a Seq[V] iterator.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := []int{1, 2, 3, 4, 5}
	iterSeq := slices.Values(slice)
	seq := it.Surf(iterSeq)
	fmt.Println(seq)
}
Output:

Seq[int][1 2 3 4 5]

func Take added in v0.1.1

func Take[I Integer, V any](n I, seq IIterable[V]) Seq[V]

Take returns a Seq[V] iterator that yields the first n values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Take(9, it.Cycle(it.Range(0, 3))) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 0 1 2 0 1 2
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for v := range it.Take(2, it.Cycle(it.Range(0, 3))) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1

func TakeBetween added in v0.1.1

func TakeBetween[I Integer, V any](start, end I, seq IIterable[V]) Seq[V]

TakeBetween returns a Seq[V] iterator that yields values between start and end. The start and end values are inclusive.

func TakeWhile added in v0.1.1

func TakeWhile[V any](seq IIterable[V], predicate func(V) bool) Seq[V]

TakeWhile returns a Seq[V] iterator that yields values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isLessThan5 := func(v int) bool { return v < 5 }
	for v := range it.TakeWhile(it.Range(10), isLessThan5) {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

0 1 2 3 4

func With added in v0.1.1

func With[V any](seq IIterable[V], process func(V)) Seq[V]

With returns a Seq[V] iterator that calls a function with each element before yielding it.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	result := 0
	process := func(v int) {
		if v%2 != 0 {
			result += v
		}
	}
	for range it.With(slice, process) {
	}
	fmt.Println(result)
}
Output:

9

func (Seq[V]) All

func (s Seq[V]) All() Seq2[int, V]

All returns a Seq2[int, V] iterator over index-value pairs of the Seq.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := []int{1, 2, 3, 4, 5}
	iterSeq2 := slices.All(slice)
	seq2 := it.Surf2(iterSeq2)
	fmt.Println(seq2)
}
Output:

Seq2[int,int][0:1 1:2 2:3 3:4 4:5]

func (Seq[V]) Len added in v0.1.1

func (s Seq[V]) Len() int

Len returns the length of the Seq.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq := it.Range(5)
	fmt.Println(seq.Len())
}
Output:

5

func (Seq[V]) Sink

func (seq Seq[V]) Sink() iter.Seq[V]

Sink sinks a Seq[V] iterator Returns an iter.Seq[V] iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]int{1, 2, 3, 4, 5})
	seq := slice.Values()
	iterSeq := seq.Sink()
	for v := range iterSeq {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

1 2 3 4 5

func (Seq[V]) String

func (s Seq[V]) String() string

String returns a string representation of a Seq[V] iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq := it.Iterable([]int{1, 2, 3, 4, 5})
	fmt.Println(seq.Values())
}
Output:

Seq[int][1 2 3 4 5]

func (Seq[V]) Values

func (s Seq[V]) Values() Seq[V]

Values simply returns the Seq.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := []int{1, 2, 3, 4, 5}
	iterSeq2 := slices.All(slice)
	seq := it.Surf2(iterSeq2).Values()
	fmt.Println(seq)
}
Output:

Seq[int][1 2 3 4 5]

type Seq2

type Seq2[K, V any] iter.Seq2[K, V] // Same as iter.Seq2[K, V] but accessible in this package

func Backwards2 added in v0.1.1

func Backwards2[K, V any](seq IIterable2[K, V]) Seq2[K, V]

Backwards returns a Seq2[K, V] iterator that iterates over the values in reverse order. If seq is an iterator, it takes O(n) time and space to collect all the values. If seq is ItMap, the function return the Seq2 from All() since map ordering is not guaranteed

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	for k, v := range it.Backwards2(seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

4:4 3:3 2:2 1:1 0:0

func Chain2

func Chain2[K, V any](seqs ...IIterable2[K, V]) Seq2[K, V]

Chain2 returns a Seq2[K, V] iterator that chains the values of multiple input iterators.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq1 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}}).Values())
	seq2 := it.Split(it.Iterable([]it.Pair[int, int]{{5, 6}, {7, 8}}).Values())
	for k, v := range it.Chain2(seq1, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

1:2 3:4 5:6 7:8

func Cycle2 added in v0.1.1

func Cycle2[K, V any](seq IIterable2[K, V]) Seq2[K, V]

Cycle returns a Seq2[K, V] iterator that cycles through the values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	max := 5
	i := 0
	for k, v := range it.Cycle2(it.Range(3).All()) {
		if i >= max {
			break
		}
		fmt.Printf("%d:%d ", k, v)
		i++
	}
	fmt.Println()
}
Output:

0:0 1:1 2:2 0:0 1:1

func Drop2 added in v0.1.1

func Drop2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]

Drop returns a Seq2[K, V] iterator that skips the first n values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(10).All()
	for k, v := range it.Drop2(3, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

3:3 4:4 5:5 6:6 7:7 8:8 9:9
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(3).All()
	for k, v := range it.Drop2(10, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

func DropWhile2 added in v0.1.1

func DropWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]

DropWhile returns a Seq2[K, V] iterator that skips values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isLessThan5 := func(k, v int) bool { return v < 5 }
	seq2 := it.Range(10).All()
	for k, v := range it.DropWhile2(seq2, isLessThan5) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

5:5 6:6 7:7 8:8 9:9

func Enumerate

func Enumerate[I Integer, V any](start I, seq IIterable[V]) Seq2[I, V]

Enumerate returns a Seq2[int, V] iterator over index-value pairs in the Iterable. The start argument specifies the starting index.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]string{"a", "b", "c"})
	for i, v := range it.Enumerate(1, slice) {
		fmt.Printf("%d:%s ", i, v)
	}
	fmt.Println()
}
Output:

1:a 2:b 3:c

func Enumerate2

func Enumerate2[I Integer, K, V any](start I, seq IIterable2[K, V]) Seq2[I, Pair[K, V]]

Enumerate2 returns a Seq2[int, Pair] iterator over index-pair pairs in the Iterable. A pair is a key-value pair for Seq2 iterators that need to return more than 2 values. The start argument specifies the starting index.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Split(it.Iterable([]it.Pair[string, string]{{"x", "r"}, {"y", "g"}, {"z", "b"}}))
	for i, v := range it.Enumerate2(1, seq2) {
		fmt.Printf("%d:%s ", i, v)
	}
	fmt.Println()
}
Output:

1:x:r 2:y:g 3:z:b

func Filter2 added in v0.1.1

func Filter2[K, V any](seq IIterable2[K, V], filter func(K, V) bool) Seq2[K, V]

Filter returns a Seq2[K, V] iterator over key-value pairs that satisfy the filter function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isEven := func(k, v int) bool { return v%2 == 0 }
	seq2 := it.Range(5).All()
	for k, v := range it.Filter2(seq2, isEven) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 2:2 4:4
Example (Inline)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	for k, v := range it.Filter2(seq2, func(k, v int) bool { return v%2 == 0 }) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 2:2 4:4

func Map2 added in v0.1.1

func Map2[K, V any](seq IIterable2[K, V], mapf func(K, V) (K, V)) Seq2[K, V]

Map returns a Seq2[K, V] iterator over key-value pairs that are transformed by the map function.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	double := func(k, v int) (int, int) { return k, v * 2 }
	seq2 := it.Range(5).All()
	for k, v := range it.Map2(seq2, double) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:2 2:4 3:6 4:8
Example (Inline)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	for k, v := range it.Map2(seq2, func(k, v int) (int, int) { return k, v * v }) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1 2:4 3:9 4:16

func OnEmpty2 added in v0.1.1

func OnEmpty2[K, V any](seq IIterable2[K, V], callback func()) Seq2[K, V]

OnEmpty returns a Seq2[K, V] iterator that calls an else function only if the iterator is not exhausted. Similar to a for ... else block in Python.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})
	seq2 := it.Split(slice)
	Else := func() { slice = []it.Pair[int, int]{{100, 200}} }
	for i, p := range it.Enumerate2(0, it.OnEmpty2(seq2, Else)) {
		if p.Key == 5 {
			break
		}
		slice[i] = it.Pair[int, int]{p.Key, p.Value * i}
	}
	fmt.Println(slice)
}
Output:

ItSlice[iters.Pair[int,int]][1:0 3:4 5:6]
Example (True)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})
	seq2 := it.Split(slice)
	Else := func() { slice = []it.Pair[int, int]{{100, 200}} }
	for i, p := range it.Enumerate2(0, it.OnEmpty2(seq2, Else)) {
		if p.Value == 10 {
			break
		}
		slice[i] = it.Pair[int, int]{p.Key, p.Value * i}
	}
	fmt.Println(slice)
}
Output:

ItSlice[iters.Pair[int,int]][100:200]

func Repeat2

func Repeat2[I Integer, K, V any](key K, val V, n I) Seq2[K, V]

Reapeat2 returns a Seq2[K, V] iterator that yields the same key-value pair n times. If n is negative, the iterator is infinite.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	for k, v := range it.Repeat2(3, 4, 5) {
		fmt.Print(k, ":", v, " ")
	}
	fmt.Println()
}
Output:

3:4 3:4 3:4 3:4 3:4

func Rotate2 added in v0.1.1

func Rotate2[I Integer, K, V any](n I, seq IIterable2[K, V], len ...I) Seq2[K, V]

Rotate returns a Seq2[K, V] iterator that rotates the values of the input iterator by n steps. A positive n rotates the values to the left, and a negative n rotates the values to the right. If seq is an iterator, it is advised to pass the length of it as the third argument to avoid a call to Len() which is an O(n) operation.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	for k, v := range it.Rotate2(2, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

2:2 3:3 4:4 0:0 1:1
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	for k, v := range it.Rotate2(-2, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

3:3 4:4 0:0 1:1 2:2

func Split added in v0.1.1

func Split[K, V any](seq IIterable[Pair[K, V]]) Seq2[K, V]

Split converts an iterator over pair values to an iterator over key-values.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[int, string]{{1, "a"}, {2, "b"}, {3, "c"}})
	seq2 := it.Split(slice)
	for k, v := range seq2 {
		fmt.Printf("%d:%s ", k, v)
	}
	fmt.Println()
}
Output:

1:a 2:b 3:c

func Surf2

func Surf2[K, V any](seq iter.Seq2[K, V]) Seq2[K, V]

Surf2 Surfaces a Seq2[K, V] iterator. It takes an iter.Seq2[K, V] iterator and returns a Seq2Seq2[K, V] iterator.

Example
package main

import (
	"fmt"

	"slices"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := []int{1, 2, 3, 4, 5}
	iterSeq2 := slices.All(slice)
	seq2 := it.Surf2(iterSeq2)
	fmt.Println(seq2)
}
Output:

Seq2[int,int][0:1 1:2 2:3 3:4 4:5]

func SwapKV added in v0.1.1

func SwapKV[K, V any](seq IIterable2[K, V]) Seq2[V, K]

SwapKV returns a Seq2[V, K] iterator that swaps the keys and values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Split(it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}}).Values())
	for k, v := range it.SwapKV(seq2) {
		fmt.Printf("%d:%s ", k, v)
	}
	fmt.Println()
}
Output:

1:a 2:b 3:c

func Take2 added in v0.1.1

func Take2[I Integer, K, V any](n I, seq IIterable2[K, V]) Seq2[K, V]

Take2 returns a Seq2[K, V] iterator that yields the first n values of the input iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Cycle2(it.Range(0, 3).All())
	for k, v := range it.Take2(9, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1 2:2 0:0 1:1 2:2 0:0 1:1 2:2
Example (Less)
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Cycle2(it.Range(0, 3).All())
	for k, v := range it.Take2(2, seq2) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1

func TakeBetween2 added in v0.1.1

func TakeBetween2[I Integer, K, V any](start, end I, seq IIterable2[K, V]) Seq2[K, V]

TakeBetween returns a Seq2[K, V] iterator that yields values between start and end. The start and end values are inclusive.

func TakeWhile2 added in v0.1.1

func TakeWhile2[K, V any](seq IIterable2[K, V], predicate func(K, V) bool) Seq2[K, V]

TakeWhile returns a Seq2[K, V] iterator that yields values from the input iterator until the predicate is false.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	isLessThan5 := func(k, v int) bool { return v < 5 }
	seq2 := it.Range(10).All()
	for k, v := range it.TakeWhile2(seq2, isLessThan5) {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

0:0 1:1 2:2 3:3 4:4

func With2 added in v0.1.1

func With2[K, V any](seq IIterable2[K, V], process func(K, V)) Seq2[K, V]

With returns a Seq2[K, V] iterator that calls a function on each iteration before yielding it.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[string, int]{{"alice", 1}, {"bob", 2}, {"charlie", 3}, {"david", 4}, {"eve", 5}})
	seq2 := it.Split(slice)
	result := ""
	process := func(k string, v int) {
		if v > 3 {
			result += k + " "
		}
	}
	for range it.With2(seq2, process) {
	}
	fmt.Println(result)
}
Output:

david eve

func Zip

func Zip[V1, V2 any](seq1 IIterable[V1], seq2 IIterable[V2]) Seq2[V1, V2]

Zip returns a Seq2[V1, V2] iterator over values from two sequences. The iteration stops when either of the sequences are exhausted. In other words, the length of the resulting sequence is the minimum of the lengths of the input sequences.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq1 := it.Iterable([]int{1, 2, 3})
	seq2 := it.Iterable([]string{"a", "b", "c"})
	for v1, v2 := range it.Zip(seq1, seq2) {
		fmt.Printf("%d:%s ", v1, v2)
	}
	fmt.Println()
}
Output:

1:a 2:b 3:c

func Zip2

func Zip2[K1, V1, K2, V2 any](seq1 IIterable2[K1, V1], seq2 IIterable2[K2, V2]) Seq2[Pair[K1, V1], Pair[K2, V2]]

Zip2 returns a Seq2[Pair, Pair] iterator over values from two sequences. The iteration stops when either of the sequences are exhausted. In other words, the length of the resulting sequence is the minimum of the lengths of the input sequences.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq1 := it.Split(it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}))
	seq2 := it.Split(it.Iterable([]it.Pair[string, string]{{"a", "b"}, {"c", "d"}, {"e", "f"}}).Values())
	for v1, v2 := range it.Zip2(seq1, seq2) {
		fmt.Printf("{%d %d}:{%s %s} ", v1.Key, v1.Value, v2.Key, v2.Value)
	}
	fmt.Println()
}
Output:

{1 2}:{a b} {3 4}:{c d} {5 6}:{e f}

func (Seq2[K, V]) All

func (s Seq2[K, V]) All() Seq2[K, V]

All returns a Seq2[K, V] iterator over the index-value pairs of the Seq2.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}})
	seq2 := it.Split(slice).All()
	fmt.Println(seq2)
}
Output:

Seq2[string,int][a:1 b:2 c:3]

func (Seq2[K, V]) Keys

func (s Seq2[K, V]) Keys() Seq[K]

Keys returns a Seq iterator over the keys of the Seq2.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}})
	seq2 := it.Split(slice)
	seq := seq2.Keys()
	fmt.Println(seq)
}
Output:

Seq[string][a b c]

func (Seq2[K, V]) Len added in v0.1.1

func (s Seq2[K, V]) Len() int

Len returns the length of the Seq2.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq2 := it.Range(5).All()
	fmt.Println(seq2.Len())
}
Output:

5

func (Seq2[K, V]) Sink added in v0.1.1

func (seq Seq2[K, V]) Sink() iter.Seq2[K, V]

Sink2 sinks a Seq2[K, V] iterator Returns an iter.Seq2[K, V] iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}})
	seq2 := it.Split(slice)
	iterSeq2 := seq2.Sink()
	for k, v := range iterSeq2 {
		fmt.Printf("%d:%d ", k, v)
	}
	fmt.Println()
}
Output:

1:2 3:4 5:6

func (Seq2[K, V]) String

func (s Seq2[K, V]) String() string

String returns a string representation of a Seq2[K, V] iterator.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	seq := it.Iterable([]it.Pair[int, int]{{1, 2}, {3, 4}, {5, 6}}).Values()
	fmt.Println(it.Split(seq))
}
Output:

Seq2[int,int][1:2 3:4 5:6]

func (Seq2[K, V]) Values

func (s Seq2[K, V]) Values() Seq[V]

Values returns a Seq iterator over the values of the Seq2.

Example
package main

import (
	"fmt"

	it "github.com/elordeiro/go/iters"
)

func main() {
	slice := it.Iterable([]it.Pair[string, int]{{"a", 1}, {"b", 2}, {"c", 3}})
	seq2 := it.Split(slice)
	seq := seq2.Values()
	fmt.Println(seq)
}
Output:

Seq[int][1 2 3]

type Signed added in v0.1.1

type Signed constraints.Signed // Signed constraint

Jump to

Keyboard shortcuts

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