seqs

package
v0.1.4 Latest Latest
Warning

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

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

Documentation

Overview

Seqs package provides a set of functions that operate on iterators. Some of the functions in this package also have a counterpart in the seq2s package. For functions that transform the input iterator, check out the seqs/transform package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

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

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

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

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

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

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

0 1 2 3 4 5 6 7

func Collect

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

Collect returns a slice of type V with all the values of a Seq[V].

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq := seqs.Range(5)
	fmt.Println(seqs.Collect(seq))
}
Output:

[0 1 2 3 4]

func Count

func Count[I constraints.Integer](start I, step ...I) iter.Seq[I]

Count returns Seq[Integer] that counts up from a given number. The step argument is optional and defaults to 1. For a decreasing count, provide a negative step. If the step is 0, the function yields the same value indefinitely. The iterator is infinite unless it is stopped by the caller.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

0 1 2 3 4
Example (Down)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	for v := range seqs.Count(5, -1) {
		if v <= 0 {
			break
		}
		fmt.Print(v, " ")
	}
	fmt.Println()
}
Output:

5 4 3 2 1

func Cycle

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

Cycle returns a Seq[V] 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"

	"github.com/elordeiro/goext/seqs"
)

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

0 1 2 0 1

func Enumerate

func Enumerate[I constraints.Integer, V any](start I, seq iter.Seq[V]) iter.Seq2[I, V]

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

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

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

1:a 2:b 3:c

func Equal

func Equal[V comparable](seq1, seq2 iter.Seq[V]) bool

Equal returns true if two Seq[V] are equal. The values must be of comparable type. For non-comparable types, use EqualFunc().

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq1 := slices.Values([]int{1, 2, 3})
	seq2 := slices.Values([]int{1, 2, 3})
	fmt.Println(seqs.Equal(seq1, seq2))
}
Output:

true

func EqualFunc

func EqualFunc[V1, V2 any](seq1 iter.Seq[V1], seq2 iter.Seq[V2], eq func(V1, V2) bool) bool

EqualFunc returns true if two Seq[V] are equal. The values are compared using the provided function.

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq1 := slices.Values([]int{1, 2, 3})
	seq2 := slices.Values([]int{1, 2, 3})
	fmt.Println(seqs.EqualFunc(seq1, seq2, func(a, b int) bool { return a == b }))
}
Output:

true

func EqualUnordered

func EqualUnordered[V comparable](seq1, seq2 iter.Seq[V]) bool

EqualUnordered returns true if two Seq[V] are equal. The values must be of comparable type. The values are compared in an unordered fashion.

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq1 := slices.Values([]int{1, 2, 3})
	seq2 := slices.Values([]int{3, 2, 1})
	fmt.Println(seqs.EqualUnordered(seq1, seq2))
}
Output:

true

func IsEmpty

func IsEmpty[V any](seq iter.Seq[V]) bool

func Len

func Len[V any](seq iter.Seq[V]) int

Len returns the length of a Seq[V]. Note that the function consumes the sequence, so if the seq passed is an one-time iterator, it will be consumed and cannot be used again.

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

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

5

func MultiUse

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

MultiUse takes a single use Seq[V] and returns a multi-use Seq[V].

func NilSeq

func NilSeq[V any](yield func(V) bool)

func Range

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

Range returns a Seq[Signed]. It is similar to:

for i := start; i < end; i += step.

If only one argument is provided, range is [0, end).
If two arguments are provided, range is [start, end).
If three arguments are provided, range is [start, end) with the given step.
If an infinite loop is detected, the function logs a message and returns an empty seq.
Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

0 1 2 3 4
Example (Reverse)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

5 4 3 2 1
Example (StartEnd)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

2 3 4
Example (StartEndStep)
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

2 4 6 8

func Repeat

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

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

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/seqs"
)

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

3 3 3 3 3

func SeqRange

func SeqRange[I constraints.Integer, V any](start, end I, seq iter.Seq[V]) iter.Seq[V]

SeqRange returns a Seq[V] that yields the values of a Seq[V] between [start, end).

If start is 0, the function is equivalent to a Take(n, seq), where n = end.
If start > end, the function is equivalent to a Drop(n, seq), where n = start.
Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

func main() {
	seq := slices.Values([]int{0, 1, 2, 3, 4, 5})
	fmt.Println(seqs.String(seqs.SeqRange(0, 3, seq)))
}
Output:

=>[0 1 2]

func String

func String[V any](seq iter.Seq[V]) string

String returns a string representation of a Seq[V]. Note that the function consumes the iterator.

Example
package main

import (
	"fmt"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

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

=>[1 2 3 4 5]

func Zip

func Zip[V1, V2 any](seq1 iter.Seq[V1], seq2 iter.Seq[V2]) iter.Seq2[V1, V2]

Zip returns a Seq2[V1, V2] over values from a Seq[V1] and a Seq[V2]. 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"

	"slices"

	"github.com/elordeiro/goext/seqs"
)

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

1:a 2:b 3:c

Types

This section is empty.

Directories

Path Synopsis
Package transform provides functions that transform seq[V] iterators in various ways.
Package transform provides functions that transform seq[V] iterators in various ways.

Jump to

Keyboard shortcuts

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