seq

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

Sequence (Iterator) Utilities for Golang

ci status Go Report Card GoDoc

Golang's "missing" iterator/sequence functions.

Construction Functions

iter.Seq[T]
  • With(...T) iter.Seq[T]: Construct a sequence using the provided values
  • FromChan(<-chan T) iter.Seq[T]: Returns a sequence that produces values until the channel is closed
  • Repeat(int, T) iter.Seq[T]: Returns a sequence which repeats the value n times
iter.Seq2[K,V]
  • WithKV(...KV[K,V]) iter.Seq2[K,V]: Construct a key-value sequence using the provided key-values
  • RepeatKV(int, K, V) iter.Seq2[K,V]: Returns a sequence which repeats the key-value pair n times

Conversion Functions

  • ToChan(iter.Seq[T]) <-chan T: Returns a channel that produces values until the sequence is exhausted
  • ToChanCtx(context.Context, iter.Seq[T]) <-chan T: Returns a channel that produces values until the sequence is exhausted or the context is canceled
  • IterKV(iter.Seq[V], func(V) K) iter.Seq2[K,V]: Converts an iter.Seq[V] to an iter.Seq2[K,V] using keyFn for keys
  • IterK(iter.Seq2[K,V]) iter.Seq[K]: Converts an iter.Seq2[K,V] to an iter.Seq[K] (keys only)
  • IterV(iter.Seq2[K,V]) iter.Seq[V]: Converts an iter.Seq2[K,V] to an iter.Seq[V] (values only)
  • MapToKV(iter.Seq[T], func(T) (K,V)) iter.Seq2[K,V]: Maps values to key-value pairs

Transformation Functions

Mapping
  • Map(iter.Seq[T], func(T) O) iter.Seq[O]: Maps the items in the sequence to another type
  • MapKV(iter.Seq2[K,V], func(K,V) (K1,V1)) iter.Seq2[K1,V1]: Maps the key-value pairs to other types
Filtering
  • Filter(iter.Seq[T], func(T) bool) iter.Seq[T]: Filter values by applying fn to each value
  • FilterKV(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]: Filter key-value pairs by applying fn to each pair
Appending
  • Append(iter.Seq[T], ...T) iter.Seq[T]: Returns a new sequence with additional items appended
  • AppendKV(iter.Seq2[K,V], ...KV[K,V]) iter.Seq2[K,V]: Returns a new sequence with additional key-value pairs appended
Replacement
  • Replace(iter.Seq[T], old, new T) iter.Seq[T]: Replace old values with new values
  • ReplaceKV(iter.Seq2[K,V], old, new KV[K,V]) iter.Seq2[K,V]: Replace old key-value pairs with new ones
Compacting
  • Compact(iter.Seq[T]) iter.Seq[T]: Yields all values that are not equal to the previous value
  • CompactFunc(iter.Seq[T], func(T,T) bool) iter.Seq[T]: Like Compact but uses a function to compare elements
  • CompactKV(iter.Seq2[K,V]) iter.Seq2[K,V]: Yields all key-value pairs that are not equal to the previous pair
  • CompactKVFunc(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) bool) iter.Seq2[K,V]: Like CompactKV but uses a function to compare pairs
Chunking
  • Chunk(iter.Seq[T], int) iter.Seq[iter.Seq[T]]: Chunk the sequence into chunks of specified size
  • ChunkKV(iter.Seq2[K,V], int) iter.Seq[iter.Seq2[K,V]]: Chunk key-value pairs into chunks of specified size
Dropping
  • Drop(iter.Seq[T], int) iter.Seq[T]: Drop n elements from the start of the sequence
  • DropKV(iter.Seq2[K,V], int) iter.Seq2[K,V]: Drop n key-value pairs from the start of the sequence
  • DropBy(iter.Seq[T], func(T) bool) iter.Seq[T]: Drop all elements for which the function returns true
  • DropKVBy(iter.Seq2[K,V], func(K,V) bool) iter.Seq2[K,V]: Drop all key-value pairs for which the function returns true

Aggregation Functions

Min/Max
  • Min(iter.Seq[T]) (T, bool): Min value from the sequence using built-in comparison
  • MinFunc(iter.Seq[T], func(T,T) int) (T, bool): Min value using a comparison function
  • MinFuncKV(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) int) (KV[K,V], bool): Min key-value pair using a comparison function
  • Max(iter.Seq[T]) (T, bool): Max value from the sequence using built-in comparison
  • MaxFunc(iter.Seq[T], func(T,T) int) (T, bool): Max value using a comparison function
  • MaxFuncKV(iter.Seq2[K,V], func(KV[K,V], KV[K,V]) int) (KV[K,V], bool): Max key-value pair using a comparison function
Reduction
  • Reduce(iter.Seq[T], O, func(O,T) O) O: Reduce the sequence to a single value
  • ReduceKV(iter.Seq2[K,V], O, func(O,K,V) O) O: Reduce key-value pairs to a single value
Counting
  • Count(iter.Seq[T]) int: Returns the number of elements in the sequence
  • CountKV(iter.Seq2[K,V]) int: Returns the number of key-value pairs in the sequence
  • CountBy(iter.Seq[T], func(T) bool) int: Count elements for which the function returns true
  • CountKVBy(iter.Seq2[K,V], func(K,V) bool) int: Count key-value pairs for which the function returns true
  • CountValues(iter.Seq[T]) iter.Seq2[T,int]: Returns a sequence where keys are values and values are their counts

Comparison Functions

  • Compare(iter.Seq[T], iter.Seq[T]) int: Compare two sequences using cmp.Compare
  • CompareFunc(iter.Seq[T], iter.Seq[T], func(T,T) int) int: Compare two sequences using a comparison function
  • CompareKV(iter.Seq2[K,V], iter.Seq2[K,V]) int: Compare two key-value sequences using cmp.Compare
  • CompareKVFunc(iter.Seq2[AK,AV], iter.Seq2[BK,BV], func(KV[AK,AV], KV[BK,BV]) int) int: Compare two key-value sequences using a comparison function

Equality Functions

  • Equal(iter.Seq[T], iter.Seq[T]) bool: Returns true if sequences are equal
  • EqualKV(iter.Seq2[K,V], iter.Seq2[K,V]) bool: Returns true if key-value sequences are equal
  • EqualFunc(iter.Seq[T], iter.Seq[T], func(T,T) bool) bool: Test equality using a comparison function
  • EqualKVFunc(iter.Seq2[AK,AV], iter.Seq2[BK,BV], func(KV[AK,AV], KV[BK,BV]) bool) bool: Test key-value equality using a comparison function

Search Functions

Contains
  • Contains(iter.Seq[T], T) bool: Returns true if the value is in the sequence
  • ContainsKV(iter.Seq2[K,V], K, V) bool: Returns true if the key-value pair is in the sequence
  • ContainsFunc(iter.Seq[T], func(T) bool) bool: Returns true if predicate returns true for any value
  • ContainsKVFunc(iter.Seq2[K,V], func(K,V) bool) bool: Returns true if predicate returns true for any key-value pair
Finding
  • Find(iter.Seq[T], T) (int, bool): Returns the index of the first occurrence of the value
  • FindBy(iter.Seq[T], func(T) bool) (T, int, bool): Returns the first value for which the function returns true
  • FindByKey(iter.Seq2[K,V], K) (V, int, bool): Returns the value of the first key-value pair with the given key
  • FindByValue(iter.Seq2[K,V], V) (K, int, bool): Returns the key of the first key-value pair with the given value

Utility Functions

  • Coalesce(iter.Seq[T]) (T, bool): Returns the first non-zero value in the sequence
  • CoalesceKV(iter.Seq2[K,V]) (KV[K,V], bool): Returns the first key-value pair with a non-zero value
  • IsSorted(iter.Seq[T]) bool: Returns true if the sequence is sorted
  • IsSortedKV(iter.Seq2[K,V]) bool: Returns true if the key-value sequence is sorted
  • IntK() func(V) int: Returns a function that generates increasing integers starting at 0

Time-based Functions

  • EveryUntil(time.Duration, time.Time) iter.Seq[time.Time]: Yields time every duration until the specified time
  • EveryN(time.Duration, int) iter.Seq[time.Time]: Yields time every duration for n times

Types

  • KV[K,V]: A struct that pairs a key and value together for use with key-value sequence functions

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[T any](seq iter.Seq[T], items ...T) iter.Seq[T]

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 added in v0.1.0

func AppendKV[K, V any](seq iter.Seq2[K, V], items ...KV[K, V]) iter.Seq2[K, V]

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

func Chunk[T any](seq iter.Seq[T], size int) iter.Seq[iter.Seq[T]]

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 added in v0.2.0

func ChunkKV[K, V any](seq iter.Seq2[K, V], size int) iter.Seq[iter.Seq2[K, V]]

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 added in v0.2.1

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

func CompactFunc[T any](seq iter.Seq[T], equal func(T, T) bool) iter.Seq[T]

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 added in v0.2.0

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 added in v0.2.0

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

func Compare[T cmp.Ordered](a, b iter.Seq[T]) int

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

func CompareFunc[T any](a, b iter.Seq[T], compare func(T, T) int) int

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 added in v0.2.0

func CompareKV[K, V cmp.Ordered](a, b iter.Seq2[K, V]) int

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 added in v0.2.0

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

func ContainsFunc[T any](seq iter.Seq[T], predicate func(T) bool) bool

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 added in v0.2.0

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 added in v0.2.0

func ContainsKVFunc[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) bool

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 added in v0.2.1

func Count[T any](seq iter.Seq[T]) int

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 added in v0.2.1

func CountBy[T any](seq iter.Seq[T], fn func(T) bool) int

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 added in v0.2.1

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

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 added in v0.2.1

func CountKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) int

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 added in v0.2.1

func CountValues[T comparable](seq iter.Seq[T]) iter.Seq2[T, int]

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 added in v0.2.1

func Drop[T any](seq iter.Seq[T], n int) iter.Seq[T]

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 added in v0.2.1

func DropBy[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T]

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 added in v0.2.1

func DropKV[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]

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 added in v0.2.1

func DropKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V]

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

func EqualFunc[T any](a, b iter.Seq[T], equal func(T, T) bool) bool

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.1

func EveryN(d time.Duration, times int) iter.Seq[time.Time]

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 added in v0.2.1

func EveryUntil(d time.Duration, until time.Time) iter.Seq[time.Time]

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

func Filter[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T]

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 added in v0.1.0

func FilterKV[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V]

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 added in v0.2.1

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 added in v0.2.1

func FindBy[T any](seq iter.Seq[T], fn func(T) bool) (T, int, bool)

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 added in v0.2.1

func FindByKey[K comparable, V any](seq iter.Seq2[K, V], key K) (V, int, bool)

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 added in v0.2.1

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 added in v0.2.1

func FromChan[T any](ch <-chan T) iter.Seq[T]

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 added in v0.2.0

func IntK[V any]() func(V) int

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

func IsSorted[T cmp.Ordered](seq iter.Seq[T]) bool

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 added in v0.2.0

func IsSortedKV[K, V cmp.Ordered](seq iter.Seq2[K, V]) bool

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 added in v0.1.0

func IterK[K, V any](iter iter.Seq2[K, V]) iter.Seq[K]

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 added in v0.1.0

func IterKV[K, V any](iter iter.Seq[V], keyFn func(V) K) iter.Seq2[K, V]

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 added in v0.1.0

func IterV[K, V any](iter iter.Seq2[K, V]) iter.Seq[V]

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

func Map[T, O any](seq iter.Seq[T], fn func(T) O) iter.Seq[O]

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 added in v0.1.0

func MapKV[K, V, K1, V1 any](seq iter.Seq2[K, V], fn func(K, V) (K1, V1)) iter.Seq2[K1, V1]

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 added in v0.2.1

func MapToKV[T, K, V any](seq iter.Seq[T], fn func(T) (K, V)) iter.Seq2[K, V]

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

func Max[T cmp.Ordered](seq iter.Seq[T]) (T, bool)

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

func MaxFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool)

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

func Min[T cmp.Ordered](seq iter.Seq[T]) (T, bool)

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

func MinFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool)

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 added in v0.1.0

func Reduce[T, O any](seq iter.Seq[T], initial O, fn func(agg O, t T) O) O

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 added in v0.1.0

func ReduceKV[K, V, O any](seq iter.Seq2[K, V], initial O, fn func(agg O, k K, v V) O) O

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

func Repeat[T any](n int, t T) iter.Seq[T]

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 added in v0.2.0

func RepeatKV[K, V any](n int, k K, v V) iter.Seq2[K, V]

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 added in v0.2.0

func ReplaceKV[K, V comparable](seq iter.Seq2[K, V], old KV[K, V], new KV[K, V]) iter.Seq2[K, V]

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 added in v0.2.1

func ToChan[T any](seq iter.Seq[T]) <-chan T

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 added in v0.2.1

func ToChanCtx[T any](ctx context.Context, seq iter.Seq[T]) <-chan T

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

func With[T any](v ...T) iter.Seq[T]

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 added in v0.1.0

func WithKV[K, V any](kv ...KV[K, V]) iter.Seq2[K, V]

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 added in v0.1.0

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 added in v0.2.1

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 added in v0.2.0

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 added in v0.2.0

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

Jump to

Keyboard shortcuts

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