Documentation
¶
Overview ¶
Seq2s package provides a set of functions that operate on iterators. Some of the functions in this package also have a counterpart in the seqs package. For functions that transform the input iterator, check out the seq2s/transform2 package.
Index ¶
- func Chain[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func Collect[V any](seq iter.Seq2[V, V]) [][2]V
- func CollectPairs[K, V any](seq iter.Seq2[K, V]) []tuples.Pair[K, V]
- func Cycle[K, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func Enumerate[I constraints.Integer, K, V any](start I, seq iter.Seq2[K, V]) iter.Seq2[I, tuples.Pair[K, V]]
- func Equal[K, V comparable](seq1, seq2 iter.Seq2[K, V]) bool
- func EqualFunc[K1, K2, V1, V2 any](seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2], ...) bool
- func EqualUnordered[K, V comparable](seq1, seq2 iter.Seq2[K, V]) bool
- func FromSlice[V any](slice [][2]V) iter.Seq2[V, V]
- func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]
- func Len[K, V any](seq iter.Seq2[K, V]) int
- func MultiUse[K, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func NilSeq2[K, V any](yield func(K, V) bool)
- func Repeat[I constraints.Integer, K, V any](key K, val V, n I) iter.Seq2[K, V]
- func SeqRange[I constraints.Integer, K, V any](start, end I, seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func String[V, K any](seq iter.Seq2[K, V]) string
- func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]
- func Zip[K1, V1, K2, V2 any](seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2]) iter.Seq2[tuples.Pair[K1, V1], tuples.Pair[K2, V2]]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
Chain returns a Seq2[K, V] that chains the values of multiple input iterators into a single iterator.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
seq1 := transform.Unpair(
slices.Values(
tuples.Pairs(
tuples.NewPair(1, 2),
tuples.NewPair(3, 4),
),
),
)
seq2 := transform.Unpair(
slices.Values(
tuples.Pairs(
tuples.NewPair(5, 6),
tuples.NewPair(7, 8),
),
),
)
for k, v := range seq2s.Chain(seq1, seq2) {
fmt.Printf("%d:%d ", k, v)
}
fmt.Println()
}
Output: 1:2 3:4 5:6 7:8
func Collect ¶
Collect returns a slice of 2-element slices of type V with all the values of a Seq2[K, V]. Note that both the keys and values in the sequence must be of the same type. For different types, use CollectPairs.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq2 := seqs.Enumerate(0, seqs.Range(5))
fmt.Println(seq2s.Collect(seq2))
}
Output: [[0 0] [1 1] [2 2] [3 3] [4 4]]
func CollectPairs ¶
CollectPairs returns a slice of type Pair[K, V] with all the values of a Seq2[K, V].
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq2 := seqs.Enumerate(0, seqs.Range(5))
fmt.Println(seq2s.CollectPairs(seq2))
}
Output: [(0 0) (1 1) (2 2) (3 3) (4 4)]
func Cycle ¶
Cycle returns a Seq2[K, 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/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
max := 5
i := 0
for k, v := range seq2s.Cycle(seqs.Enumerate(0, seqs.Range(3))) {
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 Enumerate ¶
func Enumerate[I constraints.Integer, K, V any]( start I, seq iter.Seq2[K, V], ) iter.Seq2[I, tuples.Pair[K, V]]
Enumerate returns a Seq2[Integer, Pair] over index-pair pairs in the Iterable. The Pair will contain the key-value with the key as the left and the value as the right. The start argument specifies the starting index.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
seq2 := transform.Unpair(
slices.Values(
tuples.Pairs(
tuples.NewPair("x", "r"),
tuples.NewPair("y", "g"),
tuples.NewPair("z", "b"),
),
),
)
for i, v := range seq2s.Enumerate(1, seq2) {
fmt.Printf("%d:%s ", i, v)
}
fmt.Println()
}
Output: 1:(x r) 2:(y g) 3:(z b)
func Equal ¶
func Equal[K, V comparable](seq1, seq2 iter.Seq2[K, V]) bool
Equal returns true if two Seq2[K, V] are equal. The values must be of comparable type. For non-comparable types, use EqualFunc2().
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq1 := seqs.Enumerate(0, slices.Values([]int{1, 2, 3}))
seq2 := seqs.Enumerate(0, slices.Values([]int{1, 2, 3}))
fmt.Println(seq2s.Equal(seq1, seq2))
}
Output: true
func EqualFunc ¶
func EqualFunc[K1, K2, V1, V2 any]( seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2], eq func(p1 tuples.Pair[K1, V1], p2 tuples.Pair[K2, V2]) bool) bool
EqualFunc returns true if two Seq2[K, V] are equal. The values are compared using the provided function.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq1 := seqs.Enumerate(0, slices.Values([]int{1, 2, 3}))
seq2 := seqs.Enumerate(0, slices.Values([]int{1, 2, 3}))
fmt.Println(seq2s.EqualFunc(seq1, seq2, func(a, b tuples.Pair[int, int]) bool { return a == b }))
}
Output: true
func EqualUnordered ¶
func EqualUnordered[K, V comparable](seq1, seq2 iter.Seq2[K, V]) bool
EqualUnordered returns true if two Seq2[K, 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/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
pairs1 := tuples.Pairs(tuples.NewPair(1, 2), tuples.NewPair(2, 3), tuples.NewPair(3, 4))
paris2 := tuples.Pairs(tuples.NewPair(3, 4), tuples.NewPair(2, 3), tuples.NewPair(1, 2))
seq1 := transform.Unpair(slices.Values(pairs1))
seq2 := transform.Unpair(slices.Values(paris2))
fmt.Println(seq2s.EqualUnordered(seq1, seq2))
}
Output: true
func FromSlice ¶
FromSlice returns a Seq2[V, V] from a slice of 2-element arrays.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
want := seqs.Enumerate(0, seqs.Range(10, 60, 10))
slice := [][2]int{{0, 10}, {1, 20}, {2, 30}, {3, 40}, {4, 50}}
got := seq2s.FromSlice(slice)
fmt.Println(seq2s.Equal(got, want))
}
Output: true
func Keys ¶
Keys returns a Seq[K] over the keys of a Seq2[K, V].
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
slice := tuples.Pairs(
tuples.NewPair("a", 1),
tuples.NewPair("b", 2),
tuples.NewPair("c", 3),
)
seq2 := transform.Unpair(slices.Values(slice))
seq := seq2s.Keys(seq2)
fmt.Println(seqs.String(seq))
}
Output: =>[a b c]
func Len ¶
Len returns the length of a Seq2[K, 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/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq2 := seqs.Enumerate(0,
slices.Values(
[]int{1, 2, 3},
),
)
fmt.Println(seq2s.Len(seq2))
}
Output: 3
func Repeat ¶
func Repeat[I constraints.Integer, K, V any](key K, val V, n I) iter.Seq2[K, V]
Repeat returns a Seq2[K, V] that yields the same key-value pair n times. If n is negative, the iterator is infinite.
Example ¶
package main
import (
"fmt"
"github.com/elordeiro/goext/seq2s"
)
func main() {
for k, v := range seq2s.Repeat(3, 4, 5) {
fmt.Print(k, ":", v, " ")
}
fmt.Println()
}
Output: 3:4 3:4 3:4 3:4 3:4
func SeqRange ¶
SeqRange returns a Seq2[K, V] that yields the values of a Seq2[K, V] between [start, end).
If start is 0, the function is equivalent to a Take2(n, seq), where n is end. If start > end, the function is equivalent to a Drop2(n, seq), where n is start.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
)
func main() {
seq2 := seqs.Enumerate(0, slices.Values([]int{0, 1, 2, 3, 4, 5}))
fmt.Println(seq2s.String(seq2s.SeqRange(0, 3, seq2)))
}
Output: =>[(0 0) (1 1) (2 2)]
func String ¶
String returns a string representation of a Seq2[K, V]. Note that the function consumes the iterator.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
seq := slices.Values(
tuples.Pairs(
tuples.NewPair(1, 2),
tuples.NewPair(3, 4),
tuples.NewPair(5, 6),
),
)
fmt.Println(seq2s.String(transform.Unpair(seq)))
}
Output: =>[(1 2) (3 4) (5 6)]
func Values ¶
Values returns a Seq[V] over the values of a Seq2[K, V].
Example ¶
package main
import (
"fmt"
"slices"
"github.com/elordeiro/goext/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
slice := tuples.Pairs(
tuples.NewPair("a", 1),
tuples.NewPair("b", 2),
tuples.NewPair("c", 3),
)
seq2 := transform.Unpair(slices.Values(slice))
seq := seq2s.Values(seq2)
fmt.Println(seqs.String(seq))
}
Output: =>[1 2 3]
func Zip ¶
func Zip[K1, V1, K2, V2 any]( seq1 iter.Seq2[K1, V1], seq2 iter.Seq2[K2, V2], ) iter.Seq2[tuples.Pair[K1, V1], tuples.Pair[K2, V2]]
Zip returns a Seq2[Pair, Pair] over values from a Seq2[K1, V1] and a Seq2[K2, 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/containers/tuples"
"github.com/elordeiro/goext/seq2s"
"github.com/elordeiro/goext/seqs/transform"
)
func main() {
seq1 := transform.Unpair(
slices.Values(
tuples.Pairs(
tuples.NewPair(1, 2),
tuples.NewPair(3, 4),
tuples.NewPair(5, 6),
),
),
)
seq2 := transform.Unpair(
slices.Values(
tuples.Pairs(
tuples.NewPair("a", "b"),
tuples.NewPair("c", "d"),
tuples.NewPair("e", "f"),
),
),
)
for v1, v2 := range seq2s.Zip(seq1, seq2) {
fmt.Printf("{%d %d}:{%s %s} ", v1.Left(), v1.Right(), v2.Left(), v2.Right())
}
fmt.Println()
}
Output: {1 2}:{a b} {3 4}:{c d} {5 6}:{e f}
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package transform2 provides functions that transform seq2[K, V] iterators in various ways.
|
Package transform2 provides functions that transform seq2[K, V] iterators in various ways. |