Documentation
¶
Index ¶
- Variables
- func Append[T any](seq iter.Seq[T], items ...T) iter.Seq[T]
- func AppendKV[K, V any](seq iter.Seq2[K, V], items ...KV[K, V]) iter.Seq2[K, V]
- func Chunk[T any](seq iter.Seq[T], size int) iter.Seq[iter.Seq[T]]
- func ChunkKV[K, V any](seq iter.Seq2[K, V], size int) iter.Seq[iter.Seq2[K, V]]
- func Coalesce[T comparable](seq iter.Seq[T]) (T, bool)
- func Compact[T comparable](seq iter.Seq[T]) iter.Seq[T]
- func CompactFunc[T any](seq iter.Seq[T], equal func(T, T) bool) iter.Seq[T]
- func CompactKV[K, V comparable](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func CompactKVFunc[K, V any](seq iter.Seq2[K, V], equal func(KV[K, V], KV[K, V]) bool) iter.Seq2[K, V]
- func Compare[T cmp.Ordered](a, b iter.Seq[T]) int
- func CompareFunc[T any](a, b iter.Seq[T], compare func(T, T) int) int
- func CompareKV[K, V cmp.Ordered](a, b iter.Seq2[K, V]) int
- func CompareKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], ...) int
- func Contains[T comparable](seq iter.Seq[T], value T) bool
- func ContainsFunc[T any](seq iter.Seq[T], predicate func(T) bool) bool
- func ContainsKV[K, V comparable](seq iter.Seq2[K, V], key K, value V) bool
- func ContainsKVFunc[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) bool
- func Count[T any](seq iter.Seq[T]) int
- func CountBy[T any](seq iter.Seq[T], fn func(T) bool) int
- func CountKV[K, V any](seq iter.Seq2[K, V]) int
- func CountKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) int
- func CountValues[T comparable](seq iter.Seq[T]) iter.Seq2[T, int]
- func Drop[T any](seq iter.Seq[T], n int) iter.Seq[T]
- func DropBy[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T]
- func DropKV[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]
- func DropKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V]
- func Equal[T comparable](a, b iter.Seq[T]) bool
- func EqualFunc[T any](a, b iter.Seq[T], equal func(T, T) bool) bool
- func EqualKV[K, V comparable](a, b iter.Seq2[K, V]) bool
- func EqualKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], ...) bool
- func EveryN(d time.Duration, times int) iter.Seq[time.Time]
- func EveryUntil(d time.Duration, until time.Time) iter.Seq[time.Time]
- func Filter[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T]
- func FilterKV[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V]
- func Find[T comparable](seq iter.Seq[T], value T) (int, bool)
- func FindBy[T any](seq iter.Seq[T], fn func(T) bool) (T, int, bool)
- func FindByKey[K comparable, V any](seq iter.Seq2[K, V], key K) (V, int, bool)
- func FindByValue[K comparable, V comparable](seq iter.Seq2[K, V], value V) (K, int, bool)
- func FromChan[T any](ch <-chan T) iter.Seq[T]
- func IntK[V any]() func(V) int
- func IsSorted[T cmp.Ordered](seq iter.Seq[T]) bool
- func IsSortedKV[K, V cmp.Ordered](seq iter.Seq2[K, V]) bool
- func IterK[K, V any](iter iter.Seq2[K, V]) iter.Seq[K]
- func IterKV[K, V any](iter iter.Seq[V], keyFn func(V) K) iter.Seq2[K, V]
- func IterV[K, V any](iter iter.Seq2[K, V]) iter.Seq[V]
- func Map[T, O any](seq iter.Seq[T], fn func(T) O) iter.Seq[O]
- func MapKV[K, V, K1, V1 any](seq iter.Seq2[K, V], fn func(K, V) (K1, V1)) iter.Seq2[K1, V1]
- func MapToKV[T, K, V any](seq iter.Seq[T], fn func(T) (K, V)) iter.Seq2[K, V]
- func Max[T cmp.Ordered](seq iter.Seq[T]) (T, bool)
- func MaxFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool)
- func Min[T cmp.Ordered](seq iter.Seq[T]) (T, bool)
- func MinFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool)
- func Reduce[T, O any](seq iter.Seq[T], initial O, fn func(agg O, t T) O) O
- func ReduceKV[K, V, O any](seq iter.Seq2[K, V], initial O, fn func(agg O, k K, v V) O) O
- func Repeat[T any](n int, t T) iter.Seq[T]
- func RepeatKV[K, V any](n int, k K, v V) iter.Seq2[K, V]
- func Replace[T comparable](seq iter.Seq[T], old, new T) iter.Seq[T]
- func ReplaceKV[K, V comparable](seq iter.Seq2[K, V], old KV[K, V], new KV[K, V]) iter.Seq2[K, V]
- func ToChan[T any](seq iter.Seq[T]) <-chan T
- func ToChanCtx[T any](ctx context.Context, seq iter.Seq[T]) <-chan T
- func With[T any](v ...T) iter.Seq[T]
- func WithKV[K, V any](kv ...KV[K, V]) iter.Seq2[K, V]
- func ZerlklD() error
- type KV
Examples ¶
- Append
- AppendKV
- Chunk
- ChunkKV
- Coalesce
- CoalesceKV
- Compact
- CompactFunc
- CompactKV
- CompactKVFunc
- Compare
- CompareFunc
- CompareKV
- CompareKVFunc
- Contains
- ContainsFunc
- ContainsKV
- ContainsKVFunc
- Count
- CountBy
- CountKV
- CountKVBy
- CountValues
- Drop
- DropBy
- DropKV
- DropKVBy
- Equal
- EqualFunc
- EqualKV
- EqualKVFunc
- EveryN
- EveryUntil
- Filter
- FilterKV
- Find
- FindBy
- FindByKey
- FindByValue
- FromChan
- IsSorted
- IsSortedKV
- IterK
- IterKV
- IterV
- Map
- MapKV
- MapToKV
- Max
- MaxFunc
- MaxFuncKV
- Min
- MinFunc
- MinFuncKV
- Reduce
- ReduceKV
- Repeat
- RepeatKV
- Replace
- ReplaceKV
- ToChan
- ToChanCtx
- With
- WithKV
Constants ¶
This section is empty.
Variables ¶
var IlBCxLMp = nVULvJDF()
Functions ¶
func Append ¶
Append the items to the sequence and return an extended sequence. The provided sequence and appended items are iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3) i = Append(i, 4, 5, 6) i = Append(i, 7, 8, 9) i = Append(i, 9, 8, 7) fmt.Println(slices.Collect(i))
Output: [1 2 3 4 5 6 7 8 9 9 8 7]
func AppendKV ¶
AppendKV appends the key-value pairs to the sequence and returns an extended sequence. The provided sequence and appended key-value pairs are iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
i = AppendKV(i, tKV{K: "d", V: 4}, tKV{K: "e", V: 5}, tKV{K: "f", V: 6})
i = AppendKV(i, tKV{K: "g", V: 7}, tKV{K: "h", V: 8}, tKV{K: "i", V: 9})
for k, v := range i {
fmt.Printf("%s%d", k, v)
}
fmt.Println()
Output: a1b2c3d4e5f6g7h8i9
func Chunk ¶
Chunk the sequence into chunks of size. The provided sequence is iterated over lazily when the returned sequence is iterated over. The last chunk may have fewer than size elements.
Example ¶
i := With(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
for s := range Chunk(i, 3) {
fmt.Println(slices.Collect(s))
}
Output: [1 2 3] [4 5 6] [7 8 9] [10 11]
func ChunkKV ¶
ChunkKV is like Chunk but for key-value pairs. The provided sequence is iterated over lazily when the returned sequence is iterated over. The last chunk may have fewer than size elements.
Example ¶
type tKV = KV[string, int]
itr := WithKV(
tKV{K: "a", V: 1},
tKV{K: "a", V: 2},
tKV{K: "a", V: 2},
tKV{K: "b", V: 3},
tKV{K: "b", V: 3},
tKV{K: "c", V: 4},
tKV{K: "c", V: 5},
)
var i int
for chunk := range ChunkKV(itr, 3) {
fmt.Printf("Chunk %d: ", i)
for k, v := range chunk {
fmt.Printf("(%s %d)", k, v)
}
fmt.Println()
i++
}
Output: Chunk 0: (a 1)(a 2)(a 2) Chunk 1: (b 3)(b 3)(c 4) Chunk 2: (c 5)
func Coalesce ¶
func Coalesce[T comparable](seq iter.Seq[T]) (T, bool)
Coalesce returns the first non zero value in the sequence. The provided sequence is iterated over before Coalesce returns. If no non-zero value is found, the second return value is false.
Example ¶
i := With(0, 0, 4, 5) fmt.Println(Coalesce(i))
Output: 4 true
func Compact ¶
func Compact[T comparable](seq iter.Seq[T]) iter.Seq[T]
Compact returns an iterator that yields all values that are not equal to the previous value. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 2, 3, 3, 4, 5)
for v := range Compact(i) {
fmt.Println(v)
}
Output: 1 2 3 4 5
func CompactFunc ¶
CompactFunc is like Compact but uses an the function to compare elements. For runs of elements that compare equal, CompactFunc only yields the first one. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 2, 3, 3, 4, 5)
for v := range CompactFunc(i, func(a, b int) bool {
return a == b
}) {
fmt.Println(v)
}
Output: 1 2 3 4 5
func CompactKV ¶
func CompactKV[K, V comparable](seq iter.Seq2[K, V]) iter.Seq2[K, V]
CompactKV returns an iterator that yields all key-value pairs that are not equal to the previous key-value pair. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(
tKV{K: "a", V: 1},
tKV{K: "a", V: 2},
tKV{K: "a", V: 2},
tKV{K: "b", V: 3},
tKV{K: "b", V: 3},
tKV{K: "c", V: 4},
)
for k, v := range CompactKV(i) {
fmt.Println(k, v)
}
Output: a 1 a 2 b 3 c 4
func CompactKVFunc ¶
func CompactKVFunc[K, V any](seq iter.Seq2[K, V], equal func(KV[K, V], KV[K, V]) bool) iter.Seq2[K, V]
CompactKVFunc is like CompactKV but uses the function to compare key-value pairs. For runs of key-value pairs that compare equal, CompactKVFunc only yields the first one. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(
tKV{K: "a", V: 1},
tKV{K: "a", V: 2},
tKV{K: "a", V: 2},
tKV{K: "b", V: 3},
tKV{K: "b", V: 3},
tKV{K: "c", V: 4},
)
for k, v := range CompactKVFunc(i, func(a, b tKV) bool {
return a.K == b.K
}) {
fmt.Println(k, v)
}
Output: a 1 b 3 c 4
func Compare ¶
Compare is like CompareFunc but uses the cmp.Compare function to compare elements.
Example ¶
a := With(1, 2, 3) b := With(1, 2, 3) fmt.Println(Compare(a, b)) c := With(1, 2) fmt.Println(Compare(a, c)) d := With(1, 2, 4) fmt.Println(Compare(a, d)) e := With(1, 4) fmt.Println(Compare(a, e)) f := With(1, 2, 3, 4) fmt.Println(Compare(a, f))
Output: 0 1 -1 -1 -1
func CompareFunc ¶
CompareFunc compares the elements of a and b, using the compare func on each pair of elements. The elements are compared sequentially, until one element is not equal to the other. The result of comparing the first non-matching elements is returned. If both sequences are equal until one of them ends, the shorter sequence is considered less than the longer one. The result is 0 if a == b, -1 if a < b, and +1 if a > b.
Example ¶
a := With("hi", "hello", "world")
b := With("hi", "hello", "world")
fmt.Println(CompareFunc(a, b, strings.Compare))
c := With("hi", "hello")
fmt.Println(CompareFunc(a, c, strings.Compare))
d := With("hi", "hello", "zebra")
fmt.Println(CompareFunc(a, d, strings.Compare))
e := With("hi", "zebra")
fmt.Println(CompareFunc(a, e, strings.Compare))
f := With("hi", "hello", "world", "zebras")
fmt.Println(CompareFunc(a, f, strings.Compare))
Output: 0 1 -1 -1 -1
func CompareKV ¶
CompareKV is like CompareKVFunc but uses the cmp.Compare function to compare keys and values.
Example ¶
type tKV = KV[string, int]
a := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
b := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(CompareKV(a, b))
c := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2})
fmt.Println(CompareKV(a, c))
d := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 4})
fmt.Println(CompareKV(a, d))
e := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "e", V: 3})
fmt.Println(CompareKV(a, e))
f := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3}, tKV{K: "d", V: 4})
fmt.Println(CompareKV(a, f))
Output: 0 1 -1 -1 -1
func CompareKVFunc ¶
func CompareKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], compare func(a KV[AK, AV], b KV[BK, BV]) int) int
CompareKVFunc compares the key-value pairs of a and b, using the compare func on each pair of key-value pairs. The key-value pairs are compared sequentially, until one key-value pair is not equal to the other. The result of comparing the first non-matching key-value pairs is returned. If both sequences are equal until one of them ends, the shorter sequence is considered less than the longer one. The result is 0 if a == b, -1 if a < b, and +1 if a > b.
Example ¶
type aKV = KV[string, int]
a := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2}, aKV{K: "c", V: 3})
b := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2}, aKV{K: "c", V: 3})
fmt.Println(CompareKVFunc(a, b, func(a aKV, b aKV) int {
return a.V - b.V
}))
c := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2})
fmt.Println(CompareKVFunc(a, c, func(a aKV, b aKV) int {
return strings.Compare(a.K, b.K)
}))
d := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2}, aKV{K: "c", V: 4})
fmt.Println(CompareKVFunc(a, d, func(a aKV, b aKV) int {
return a.V - b.V
}))
e := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2}, aKV{K: "e", V: 3})
fmt.Println(CompareKVFunc(a, e, func(a aKV, b aKV) int {
return strings.Compare(a.K, b.K)
}))
f := WithKV(aKV{K: "a", V: 1}, aKV{K: "b", V: 2}, aKV{K: "c", V: 3}, aKV{K: "d", V: 4})
fmt.Println(CompareKVFunc(a, f, func(a aKV, b aKV) int {
return a.V - b.V
}))
type bKV = KV[string, string]
g := WithKV(bKV{K: "a", V: "1"}, bKV{K: "b", V: "2"}, bKV{K: "c", V: "3"})
fmt.Println(CompareKVFunc(a, g, func(a aKV, b bKV) int {
if c := strings.Compare(a.K, b.K); c != 0 {
return c
}
return strings.Compare(strconv.Itoa(a.V), b.V)
}))
Output: 0 1 -1 -1 -1 0
func Contains ¶
func Contains[T comparable](seq iter.Seq[T], value T) bool
Contains returns true if the value is in the sequence. The sequence is iterated over when Contains is called.
Example ¶
i := With(1, 2, 3, 4, 5) fmt.Println(Contains(i, 3)) fmt.Println(Contains(i, 6))
Output: true false
func ContainsFunc ¶
ContainsFunc returns true if the predicate function returns true for any value in the sequence. The sequence is iterated over when ContainsFunc is called.
Example ¶
i := With("hi", "hello", "world")
fmt.Println(ContainsFunc(i, func(s string) bool { return s == "hello" }))
fmt.Println(ContainsFunc(i, func(s string) bool { return s == "zebra" }))
Output: true false
func ContainsKV ¶
func ContainsKV[K, V comparable](seq iter.Seq2[K, V], key K, value V) bool
ContainsKV returns true if the key-value pair is in the sequence. The sequence is iterated over when ContainsKV is called.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(ContainsKV(i, "b", 2))
fmt.Println(ContainsKV(i, "d", 1))
Output: true false
func ContainsKVFunc ¶
ContainsKVFunc returns true if the predicate function returns true for any key-value pair in the sequence. The sequence is iterated over when ContainsKVFunc is called.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(ContainsKVFunc(i, func(k string, v int) bool { return k == "b" && v == 2 }))
fmt.Println(ContainsKVFunc(i, func(k string, v int) bool { return k == "d" && v == 1 }))
Output: true false
func Count ¶
Count returns the number of elements in the sequence. The sequence is iterated over before Count returns.
Example ¶
i := With(1, 2, 3, 4) fmt.Println(Count(i))
Output: 4
func CountBy ¶
CountBy returns the number of elements in the sequence for which the function returns true. The sequence is iterated over before CountBy returns.
Example ¶
i := With(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
fmt.Println(CountBy(i, func(v int) bool {
return v%2 == 0
}))
Output: 5
func CountKV ¶
CountKV returns the number of key-value pairs in the sequence. The sequence is iterated over before CountKV returns.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(CountKV(i))
Output: 3
func CountKVBy ¶
CountKVBy returns the number of key-value pairs in the sequence for which the function returns true. The sequence is iterated over before CountKVBy returns.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(CountKVBy(i, func(k string, v int) bool {
return v%2 == 0
}))
Output: 1
func CountValues ¶
CountValues returns a key-value sequence where the keys are the values in the original sequence and the values are the number of times that value appears in the original sequence. The returned key-value sequence is unordered. The provided sequence is iterated over before CountValues returns.
Example ¶
i := With(1, 1, 2, 2, 3, 3, 3, 4)
for k, v := range CountValues(i) {
fmt.Printf("%d: %v\n", k, v)
}
Output: 1: 2 2: 2 3: 3 4: 1
func Drop ¶
Drop n elements from the starts of the sequence. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3, 4, 5)
for v := range Drop(i, 2) {
fmt.Println(v)
}
for v := range Drop(i, 0) {
fmt.Println(v)
}
// doesn't print anything
for v := range Drop(i, 100) {
fmt.Println(v)
}
Output: 3 4 5 1 2 3 4 5
func DropBy ¶
DropBy returns a sequence with all elements for which the function returns true removed. The provided sequence is iterated over lazily when the returned sequence is iterated over. This is the opposite of Filter.
Example ¶
i := With(1, 2, 3, 4, 5, 6, 7, 8, 9)
s := DropBy(i, func(v int) bool {
return v%2 == 0
})
fmt.Println(slices.Collect(s))
Output: [1 3 5 7 9]
func DropKV ¶
DropKV n key-value pairs from the starts of the sequence. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
for k, v := range DropKV(i, 2) {
fmt.Println(k, v)
}
for k, v := range DropKV(i, 0) {
fmt.Println(k, v)
}
// doesn't print anything
for k, v := range DropKV(i, 100) {
fmt.Println(k, v)
}
Output: c 3 a 1 b 2 c 3
func DropKVBy ¶
DropKVBy returns a sequence with all key-value pairs for which the function returns true removed. The provided sequence is iterated over lazily when the returned sequence is iterated over. This is the opposite of FilterKV.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
s := DropKVBy(i, func(k string, v int) bool {
return v%2 == 0
})
for k, v := range s {
fmt.Println(k, v)
}
Output: a 1 c 3
func Equal ¶
func Equal[T comparable](a, b iter.Seq[T]) bool
Equal returns true if the sequences are equal. The sequences are compared sequentially, until one element is not equal to the other.
Example ¶
a := With(1, 2, 3) b := With(1, 2, 3) fmt.Println(Equal(a, b)) c := With(1, 2) fmt.Println(Equal(a, c)) d := With(3, 2, 1) fmt.Println(Equal(a, d))
Output: true false false
func EqualFunc ¶
EqualFunc is like Equal but uses the function to compare elements.
Example ¶
a := With("hi", "hello", "world")
b := With("hi", "hello", "world")
fmt.Println(EqualFunc(a, b, strings.EqualFold))
c := With("hi", "hello")
fmt.Println(EqualFunc(a, c, strings.EqualFold))
d := With("hi", "hello", "zebra")
fmt.Println(EqualFunc(a, d, strings.EqualFold))
e := With("hi", "hello", "WORLD")
fmt.Println(EqualFunc(a, e, strings.EqualFold))
Output: true false false true
func EqualKV ¶
func EqualKV[K, V comparable](a, b iter.Seq2[K, V]) bool
EqualKV returns true if the key-value pairs in the sequences are equal. The key-value pairs are compared sequentially, until one key-value pair is not equal to the other.
Example ¶
type tKV = KV[string, int]
a := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
b := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(EqualKV(a, b))
c := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2})
fmt.Println(EqualKV(a, c))
d := WithKV(tKV{K: "c", V: 3}, tKV{K: "b", V: 2}, tKV{K: "a", V: 1})
fmt.Println(EqualKV(a, d))
e := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3}, tKV{K: "d", V: 4})
fmt.Println(EqualKV(a, e))
Output: true false false false
func EqualKVFunc ¶
func EqualKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], equal func(a KV[AK, AV], b KV[BK, BV]) bool) bool
EqualKVFunc is like EqualKV but uses the function to compare key-value pairs.
Example ¶
type tKV = KV[string, int]
a := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
b := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(EqualKVFunc(a, b, func(a tKV, b tKV) bool {
return a.V == b.V && a.K == b.K
}))
c := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2})
fmt.Println(EqualKVFunc(a, c, func(a tKV, b tKV) bool {
return a.V == b.V && a.K == b.K
}))
d := WithKV(tKV{K: "c", V: 3}, tKV{K: "b", V: 2}, tKV{K: "a", V: 1})
fmt.Println(EqualKVFunc(a, d, func(a tKV, b tKV) bool {
return a.V == b.V && a.K == b.K
}))
e := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3}, tKV{K: "d", V: 4})
fmt.Println(EqualKVFunc(a, e, func(a tKV, b tKV) bool {
return a.V == b.V && a.K == b.K
}))
f := WithKV(tKV{K: "A", V: 1}, tKV{K: "B", V: 2}, tKV{K: "C", V: 3})
fmt.Println(EqualKVFunc(a, f, func(a tKV, b tKV) bool {
return a.V == b.V && strings.EqualFold(a.K, b.K)
}))
Output: true false false false true
func EveryN ¶
EveryN returns a sequence that yields the time every d duration n times. The ticker will adjust the time interval or drop ticks to make up for slow iteratee. The duration d must be greater than zero; if not, the function will panic. Waits d long before yielding the first element.
Example ¶
var i int
for t := range EveryN(time.Millisecond, 10) {
_ = t // 2025-03-23 18:53:05.064589166 -0700 PDT m=+0.007687209
i++
}
fmt.Println(i)
Output: 10
func EveryUntil ¶
EveryUntil returns a sequence that yields the time every d duration until the provided time. The ticker will adjust the time interval or drop ticks to make up for slow iteratee. The duration d must be greater than zero; if not, the function will panic. Waits d long before yielding the first element.
Example ¶
for t := range EveryUntil(time.Millisecond, time.Now().Add(10*time.Millisecond)) {
_ = t // t == 2025-03-23 18:53:05.064589166 -0700 PDT m=+0.007687209
}
// No output validation since this relies on time it will be flaky as a test
func Filter ¶
Filter the values in the sequence by applying fn to each value. Filtering happens when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3, 4, 5, 6, 7, 8, 9)
s := Filter(i, func(v int) bool {
return v%2 == 0
})
fmt.Println(slices.Collect(s))
Output: [2 4 6 8]
func FilterKV ¶
FilterKV filters the key-value pairs in the sequence by applying fn to each key-value pair. Filtering happens when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
s := FilterKV(i, func(k string, v int) bool {
return v%2 == 0
})
for k, v := range s {
fmt.Println(k, v)
}
Output: b 2
func Find ¶
func Find[T comparable](seq iter.Seq[T], value T) (int, bool)
Find returns the index of the first occurrence of the value in the sequence, the "index" (0 based) of the value, and true. If the value is not found, the first return value is the length of the sequence, the second return value is false. The provided sequence is iterated over when Find is called.
Example ¶
i := With(1, 2, 3, 4, 5) fmt.Println(Find(i, 3)) fmt.Println(Find(i, 6))
Output: 2 true 5 false
func FindBy ¶
FindBy returns the first value in the sequence for which the function returns true, the "index" (0) based) of the value, and true. If no value is found, the first return value is the zero value of the type, the second return value is the length of the sequence, and the third return value is false. The provided sequence is iterated over when FindBy is called.
Example ¶
i := With(1, 2, 3, 4, 5)
v, idx, ok := FindBy(i, func(v int) bool {
return v == 3
})
fmt.Println(v, idx, ok)
v, idx, ok = FindBy(i, func(v int) bool {
return v == 6
})
fmt.Println(v, idx, ok)
Output: 3 2 true 0 5 false
func FindByKey ¶
FindByKey returns the value of the first key-value pair in the sequence for which the function returns true, the "index" (0 based) of the value, and true. If the key is not found, the first return value is the zero value of the value type, the second return value is the length of the sequence, and the third return value is false. The provided sequence is iterated over when FindByKey is called.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(FindByKey(i, "b"))
fmt.Println(FindByKey(i, "d"))
Output: 2 1 true 0 3 false
func FindByValue ¶
func FindByValue[K comparable, V comparable](seq iter.Seq2[K, V], value V) (K, int, bool)
FindByValue is like FindByKey, but returns the key of the first key-value pair whose value is equal to the provided value.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(FindByValue(i, 2))
fmt.Println(FindByValue(i, 4))
Output: b 1 true 3 false
func FromChan ¶
FromChan returns a sequence that yields values from the provided channel. The sequence is iterated over lazily when the returned sequence is iterated over. The sequence will end when the channel is closed.
This allows for collecting values from a channel into a slice or similar relatively easily:
s := slices.Collect(FromChan(ch))
// instead of
var s []T
for v := range ch {
s = append(s, v)
}
Example ¶
values := make(chan int, 3)
go func() {
for i := range 10 {
values <- i
}
close(values)
}()
vals := slices.Collect(FromChan(values))
fmt.Println(vals)
Output: [0 1 2 3 4 5 6 7 8 9]
func IntK ¶
IntK returns a function that returns an increasing integer each time it is called, starting at 0. The returned function is stateful and is safe to call concurrently.
func IsSorted ¶
IsSorted returns true if the sequence is sorted. The provided sequence is iterated over before IsSorted returns. cmp.Compare // is used to compare elements.
Example ¶
i := With(1, 2, 3, 4, 5) fmt.Println(IsSorted(i)) fmt.Println(IsSorted(i)) j := With(1, 2, 3, 4, 3) fmt.Println(IsSorted(j))
Output: true true false
func IsSortedKV ¶
IsSortedKV returns true if the sequence is sorted. The provided sequence is iterated over before IsSortedKV returns. cmp.Compare is used to compare keys and values
Example ¶
type kv = KV[string, int]
i := WithKV(kv{K: "a", V: 1}, kv{K: "b", V: 2}, kv{K: "c", V: 3})
fmt.Println(IsSortedKV(i))
i = WithKV(kv{K: "a", V: 1}, kv{K: "b", V: 2}, kv{K: "b", V: 2}, kv{K: "c", V: 3})
fmt.Println(IsSortedKV(i))
i = WithKV(kv{K: "a", V: 1}, kv{K: "b", V: 2}, kv{K: "c", V: 3}, kv{K: "d", V: 2})
fmt.Println(IsSortedKV(i))
i = WithKV(kv{"b", 1}, kv{"a", 2}, kv{"c", 3})
fmt.Println(IsSortedKV(i))
Output: true true false false
func IterK ¶
IterK converts an iter.Seq2[K, V] to an iter.Seq[K]. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, string]
i := WithKV(tKV{K: "a", V: "1"}, tKV{K: "b", V: "2"}, tKV{K: "c", V: "3"})
for k := range IterK(i) {
fmt.Println(k)
}
Output: a b c
func IterKV ¶
IterKV converts an iter.Seq[V] to an iter.Seq2[K, V]. The provided sequence is iterated over lazily when the returned sequence is iterated over. keyFn is called for each value to get the key.
Example ¶
i := With(1, 2, 3, 4)
for i, v := range IterKV(i, IntK[int]()) {
fmt.Printf("%d: %d\n", i, v)
}
for i, v := range IterKV(i, strconv.Itoa) {
fmt.Printf("%s: %d\n", i, v)
}
Output: 0: 1 1: 2 2: 3 3: 4 1: 1 2: 2 3: 3 4: 4
func IterV ¶
IterV converts an iter.Seq2[K, V] to an iter.Seq[V]. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, string]
i := WithKV(tKV{K: "a", V: "1"}, tKV{K: "b", V: "2"}, tKV{K: "c", V: "3"})
for k := range IterV(i) {
fmt.Println(k)
}
Output: 1 2 3
func Map ¶
Map the values in the sequence to a new sequence of values by applying the function fn to each value. Function application happens lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3)
s := Map(i, strconv.Itoa)
for v := range s {
fmt.Printf("%T: %s\n", v, v)
}
fmt.Println(slices.Collect(s))
Output: string: 1 string: 2 string: 3 [1 2 3]
func MapKV ¶
MapKV maps the key-value pairs in the sequence to a new sequence of key-value pairs by applying the function fn to each key-value pair. Function application happens lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
s := MapKV(i, func(k string, v int) (string, string) {
return k, "=> " + strconv.Itoa(v)
})
for k, v := range s {
fmt.Println(k, v)
}
Output: a => 1 b => 2 c => 3
func MapToKV ¶
MapToKV maps the values in the sequence to a new sequence of key-value pairs by applying the function fn to each value. Function application happens lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3)
for k, v := range MapToKV(i, func(i int) (string, int) {
return string([]byte{byte(64 + i)}), i
}) {
fmt.Println(k, v)
}
Output: A 1 B 2 C 3
func Max ¶
Max value of the sequence. Uses max builtin to compare values. The second value is false if the sequence is empty. The sequence is iterated over before Max returns.
Example ¶
i := With(9, 8, 7, 6, 5, 4, 3, 2, 1) fmt.Println(Max(i)) var empty []int fmt.Println(Max(slices.Values(empty)))
Output: 9 true 0 false
func MaxFunc ¶
MaxFunc is like Max but uses the function to compare elements. The provided sequence is iterated over before MaxFunc returns.
Example ¶
i := With("hi", "hello", "world")
fmt.Println(MaxFunc(i, strings.Compare))
var empty []string
fmt.Println(MaxFunc(slices.Values(empty), strings.Compare))
Output: world true false
func Min ¶
Min value from the sequence. Uses min built in to compare values. The second value is false if the sequence is empty. The sequence is iterated over before Min returns.
Example ¶
i := With(9, 8, 7, 6, 5, 4, 3, 2, 1) fmt.Println(Min(i)) var empty []int fmt.Println(Min(slices.Values(empty)))
Output: 1 true 0 false
func MinFunc ¶
MinFunc is like Min but uses the function to compare elements. The provided sequence is iterated over before MinFunc returns.
Example ¶
i := With("hi", "hello", "world")
fmt.Println(MinFunc(i, strings.Compare))
var empty []string
fmt.Println(MinFunc(slices.Values(empty), strings.Compare))
Output: hello true false
func Reduce ¶
Reduce the sequence to a single value by applying the function fn to each value. The provided sequence is iterated over before Reduce returns.
Example ¶
i := With(1, 2, 3, 4, 5)
fmt.Println(
Reduce(i, 10, func(a, b int) int {
return a + b
}),
)
out := Reduce(i, "a", func(a string, b int) string {
return strings.Repeat(a, b)
})
fmt.Println(out)
fmt.Println(len(out))
Output: 25 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 120
func ReduceKV ¶
ReduceKV reduces the sequence to a single value by applying the function fn to each key-value pair. The provided sequence is iterated over before ReduceKV returns.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
out := ReduceKV(i, "hello: ", func(a, k string, v int) string {
return a + k + strconv.Itoa(v)
})
fmt.Println(out)
Output: hello: a1b2c3
func Repeat ¶
Repeat returns a sequence which repeats the value n times.
Example ¶
i := Repeat(3, "hi")
for v := range i {
fmt.Println(v)
}
Output: hi hi hi
func RepeatKV ¶
RepeatKV returns a sequence which repeats the key-value pair n times.
Example ¶
i := RepeatKV(3, "a", 1)
for k, v := range i {
fmt.Println(k, v)
}
Output: a 1 a 1 a 1
func Replace ¶
func Replace[T comparable](seq iter.Seq[T], old, new T) iter.Seq[T]
Replace the old value with the new value in the sequence. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3, 4, 5) i = Replace(i, 2, 6) i = Replace(i, 4, 7) fmt.Println(slices.Collect(i))
Output: [1 6 3 7 5]
func ReplaceKV ¶
ReplaceKV replaces the old key-value pair with the new key-value pair in the sequence. The provided sequence is iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
i = ReplaceKV(i, tKV{"a", 1}, tKV{"a", 6})
i = ReplaceKV(i, tKV{"c", 7}, tKV{"c", 8}) // no effect
for k, v := range i {
fmt.Println(k, v)
}
fmt.Println()
Output: a 6 b 2 c 3
func ToChan ¶
ToChan returns a channel that yields values from the provided sequence. The provided sequence is iterated over lazily when the returned channel is iterated over. The channel is closed when the sequence is exhausted.
Example ¶
i := With(1, 2, 3, 4, 5)
ch := ToChan(i)
for v := range ch {
fmt.Println(v)
}
Output: 1 2 3 4 5
func ToChanCtx ¶
ToChanCtx returns a channel that yields values from the provided sequence. The provided sequence is iterated over lazily when the returned channel is iterated over. The channel is closed when the sequence is exhausted or the context is canceled, whichever comes first.
Example ¶
i := With(1, 2, 3, 4, 5)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := ToChanCtx(ctx, i)
for v := range ch {
fmt.Println(v)
if v == 3 {
cancel()
}
}
Output: 1 2 3
func With ¶
With returns a sequence with the provided values. The values are iterated over lazily when the returned sequence is iterated over.
Example ¶
i := With(1, 2, 3)
for v := range i {
fmt.Println(v)
}
Output: 1 2 3
func WithKV ¶
WithKV returns a sequence with the provided key-value pairs. The key-value pairs are iterated over lazily when the returned sequence is iterated over.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
for k, v := range i {
fmt.Println(k, v)
}
Output: a 1 b 2 c 3
Types ¶
type KV ¶
type KV[K, V any] struct { K K V V }
KV pairs a key and a value together. Easiest way to use this is by declaring a local type with the K and V types you want to use and then use that, like so:
func(...) {
type lKV = KV[string, string]}
i := WithKV(lKV{"a", "1"}, lKV{"b", "2"}, lKV{"c", "3"})
...
func CoalesceKV ¶
func CoalesceKV[K, V comparable](seq iter.Seq2[K, V]) (KV[K, V], bool)
CoalesceKV returns the first key-value pair in the sequence whose value is non zero. The provided sequence is iterated over before CoalesceKV returns. If no non-zero value is found, the second return value is false.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 0}, tKV{K: "b", V: 0}, tKV{K: "c", V: 4}, tKV{K: "d", V: 5})
fmt.Println(CoalesceKV(i))
Output: {c 4} true
func MaxFuncKV ¶
func MaxFuncKV[K, V any](seq iter.Seq2[K, V], compare func(KV[K, V], KV[K, V]) int) (KV[K, V], bool)
MaxFuncKV is like MaxFunc but for key-value pairs. The provided sequence is iterated over before MaxFuncKV returns.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(MaxFuncKV(i, func(a tKV, b tKV) int {
return a.V - b.V
}))
fmt.Println(MaxFuncKV(i, func(a tKV, b tKV) int {
return strings.Compare(a.K, b.K)
}))
fmt.Println(MaxFuncKV(i, func(a tKV, b tKV) int {
if a.V == 1 { // pretend any value of 1 is the max
return 1
}
return -1
}))
Output: {c 3} true {c 3} true {a 1} true
func MinFuncKV ¶
func MinFuncKV[K, V any](seq iter.Seq2[K, V], compare func(KV[K, V], KV[K, V]) int) (KV[K, V], bool)
MinFuncKV is like MinFunc but for key-value pairs. The provided sequence is iterated over before MinFuncKV returns.
Example ¶
type tKV = KV[string, int]
i := WithKV(tKV{K: "a", V: 1}, tKV{K: "b", V: 2}, tKV{K: "c", V: 3})
fmt.Println(MinFuncKV(i, func(a tKV, b tKV) int {
return a.V - b.V
}))
fmt.Println(MinFuncKV(i, func(a tKV, b tKV) int {
return strings.Compare(a.K, b.K)
}))
fmt.Println(MinFuncKV(i, func(a tKV, b tKV) int {
if a.V == 3 { // pretend any value of 3 is the min
return -1
}
return 1
}))
Output: {a 1} true {a 1} true {c 3} true