it

package
v1.52.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign[K comparable, V any, Map ~map[K]V](maps ...iter.Seq[Map]) Map

Assign merges multiple sequences of maps from left to right. Play: https://go.dev/play/p/P6tY8qW3rN2

Example
result := Assign(values(
	map[string]int{"a": 1, "b": 2},
	map[string]int{"b": 3, "c": 4},
))

fmt.Printf("%v %v %v %v", len(result), result["a"], result["b"], result["c"])
Output:

3 1 3 4

func Associate

func Associate[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V)) map[K]V

Associate returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. Will iterate through the entire sequence. Play: https://go.dev/play/p/4BvOSo-yza

func AssociateI

func AssociateI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V)) map[K]V

AssociateI returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. Will iterate through the entire sequence. Play: https://go.dev/play/p/5CwPTPz-zb

func ChannelToSeq

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

ChannelToSeq returns a sequence built from channels items. Blocks until channel closes. Play: https://go.dev/play/p/IXqSs2Ooqpm

func Chunk

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

Chunk returns a sequence of elements split into groups of length size. If the sequence can't be split evenly, the final chunk will be the remaining elements. Play: https://go.dev/play/p/4VpIM8-zu

Example
list := slices.Values([]int{0, 1, 2, 3, 4})

result := Chunk(list, 2)

for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

[0 1]
[2 3]
[4]

func ChunkEntries

func ChunkEntries[K comparable, V any](m map[K]V, size int) iter.Seq[map[K]V]

ChunkEntries splits a map into a sequence of elements in groups of length equal to its size. If the map cannot be split evenly, the final chunk will contain the remaining elements. Play: https://go.dev/play/p/Q4jR8m9T2nX

Example
result := ChunkEntries(
	map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
		"d": 4,
		"e": 5,
	},
	3,
)

for r := range result {
	fmt.Printf("%d\n", len(r))
}
Output:

3
2

func ChunkString

func ChunkString[T ~string](str T, size int) iter.Seq[T]

ChunkString returns a sequence of strings split into groups of length size. If the string can't be split evenly, the final chunk will be the remaining characters. Play: https://go.dev/play/p/Y4mN8bB2cXw

Example
result1 := ChunkString("123456", 2)
result2 := ChunkString("1234567", 2)
result3 := ChunkString("", 2)
result4 := ChunkString("1", 2)

fmt.Printf("%v\n", slices.Collect(result1))
fmt.Printf("%v\n", slices.Collect(result2))
fmt.Printf("%v\n", slices.Collect(result3))
fmt.Printf("%v\n", slices.Collect(result4))
Output:

[12 34 56]
[12 34 56 7]
[]
[1]

func CoalesceSeq

func CoalesceSeq[T any](v ...iter.Seq[T]) (iter.Seq[T], bool)

CoalesceSeq returns the first non-empty sequence. Will iterate through each sub-sequence at most once. Play: https://go.dev/play/p/H8iI4kK1Ef6

func CoalesceSeqOrEmpty

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

CoalesceSeqOrEmpty returns the first non-empty sequence. Will iterate through each sub-sequence at most once. Play: https://go.dev/play/p/I9jJ5lL2Fg7

func Compact

func Compact[T comparable, I ~func(func(T) bool)](collection I) I

Compact returns a sequence of all non-zero elements. Play: https://go.dev/play/p/8isgTsyfL-t

Example
list := slices.Values([]string{"", "foo", "", "bar", ""})

result := Compact(list)

fmt.Printf("%v", slices.Collect(result))
Output:

[foo bar]

func Contains

func Contains[T comparable](collection iter.Seq[T], element T) bool

Contains returns true if an element is present in a collection. Will iterate through the entire sequence if element is not found. Play: https://go.dev/play/p/1edj7hH3TS2

func ContainsBy

func ContainsBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool

ContainsBy returns true if predicate function return true. Will iterate through the entire sequence if predicate never returns true.

func Count

func Count[T comparable](collection iter.Seq[T], value T) int

Count counts the number of elements in the collection that equal value. Will iterate through the entire sequence. Play: https://go.dev/play/p/0XrQKOk-vw

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3})

result := Count(list, 2)

fmt.Printf("%v", result)
Output:

2

func CountBy

func CountBy[T any](collection iter.Seq[T], predicate func(item T) bool) int

CountBy counts the number of elements in the collection for which predicate is true. Will iterate through the entire sequence. Play: https://go.dev/play/p/1SmFJ5-zr

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3})

result := CountBy(list, func(i int) bool {
	return i < 4
})

fmt.Printf("%v", result)
Output:

8

func CountValues

func CountValues[T comparable](collection iter.Seq[T]) map[T]int

CountValues counts the number of each element in the collection. Will iterate through the entire sequence. Play: https://go.dev/play/p/2TnGK6-zs

Example
result1 := CountValues(slices.Values([]int{}))
result2 := CountValues(slices.Values([]int{1, 2}))
result3 := CountValues(slices.Values([]int{1, 2, 2}))
result4 := CountValues(slices.Values([]string{"foo", "bar", ""}))
result5 := CountValues(slices.Values([]string{"foo", "bar", "bar"}))

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[1:1 2:1]
map[1:1 2:2]
map[:1 bar:1 foo:1]
map[bar:2 foo:1]

func CountValuesBy

func CountValuesBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U]int

CountValuesBy counts the number of each element returned from transform function. Is equivalent to chaining Map and CountValues. Will iterate through the entire sequence. Play: https://go.dev/play/p/3UoHL7-zt

Example
isEven := func(v int) bool {
	return v%2 == 0
}

result1 := CountValuesBy(slices.Values([]int{}), isEven)
result2 := CountValuesBy(slices.Values([]int{1, 2}), isEven)
result3 := CountValuesBy(slices.Values([]int{1, 2, 2}), isEven)

length := func(v string) int {
	return len(v)
}

result4 := CountValuesBy(slices.Values([]string{"foo", "bar", ""}), length)
result5 := CountValuesBy(slices.Values([]string{"foo", "bar", "bar"}), length)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[false:1 true:1]
map[false:1 true:2]
map[0:1 3:2]
map[3:3]

func CrossJoin2

func CrossJoin2[A, B any](listA iter.Seq[A], listB iter.Seq[B]) iter.Seq[lo.Tuple2[A, B]]

CrossJoin2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/ZBDEXBYj6nU

Example
result := CrossJoin2(values("a", "b"), values(1, 2, 3, 4))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1}
{a 2}
{a 3}
{a 4}
{b 1}
{b 2}
{b 3}
{b 4}

func CrossJoin3

func CrossJoin3[A, B, C any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C]) iter.Seq[lo.Tuple3[A, B, C]]

CrossJoin3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/0XrQKOk-vw

Example
result := CrossJoin3(values("a", "b"), values(1, 2, 3, 4), values(true, false))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true}
{a 1 false}
{a 2 true}
{a 2 false}
{a 3 true}
{a 3 false}
{a 4 true}
{a 4 false}
{b 1 true}
{b 1 false}
{b 2 true}
{b 2 false}
{b 3 true}
{b 3 false}
{b 4 true}
{b 4 false}

func CrossJoin4

func CrossJoin4[A, B, C, D any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D]) iter.Seq[lo.Tuple4[A, B, C, D]]

CrossJoin4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/1SmFJ5-zr

Example
result := CrossJoin4(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar}}
{a 1 false {bar}}
{a 2 true {bar}}
{a 2 false {bar}}
{a 3 true {bar}}
{a 3 false {bar}}
{a 4 true {bar}}
{a 4 false {bar}}
{b 1 true {bar}}
{b 1 false {bar}}
{b 2 true {bar}}
{b 2 false {bar}}
{b 3 true {bar}}
{b 3 false {bar}}
{b 4 true {bar}}
{b 4 false {bar}}

func CrossJoin5

func CrossJoin5[A, B, C, D, E any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E]) iter.Seq[lo.Tuple5[A, B, C, D, E]]

CrossJoin5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/2TnGK6-zs

Example
result := CrossJoin5(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2}
{a 1 false {bar} 4.2}
{a 2 true {bar} 4.2}
{a 2 false {bar} 4.2}
{a 3 true {bar} 4.2}
{a 3 false {bar} 4.2}
{a 4 true {bar} 4.2}
{a 4 false {bar} 4.2}
{b 1 true {bar} 4.2}
{b 1 false {bar} 4.2}
{b 2 true {bar} 4.2}
{b 2 false {bar} 4.2}
{b 3 true {bar} 4.2}
{b 3 false {bar} 4.2}
{b 4 true {bar} 4.2}
{b 4 false {bar} 4.2}

func CrossJoin6

func CrossJoin6[A, B, C, D, E, F any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F]) iter.Seq[lo.Tuple6[A, B, C, D, E, F]]

CrossJoin6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3UoHL7-zt

Example
result := CrossJoin6(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop}
{a 1 false {bar} 4.2 plop}
{a 2 true {bar} 4.2 plop}
{a 2 false {bar} 4.2 plop}
{a 3 true {bar} 4.2 plop}
{a 3 false {bar} 4.2 plop}
{a 4 true {bar} 4.2 plop}
{a 4 false {bar} 4.2 plop}
{b 1 true {bar} 4.2 plop}
{b 1 false {bar} 4.2 plop}
{b 2 true {bar} 4.2 plop}
{b 2 false {bar} 4.2 plop}
{b 3 true {bar} 4.2 plop}
{b 3 false {bar} 4.2 plop}
{b 4 true {bar} 4.2 plop}
{b 4 false {bar} 4.2 plop}

func CrossJoin7

func CrossJoin7[A, B, C, D, E, F, G any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G]) iter.Seq[lo.Tuple7[A, B, C, D, E, F, G]]

CrossJoin7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/4VpIM8-zu

Example
result := CrossJoin7(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false}
{a 1 false {bar} 4.2 plop false}
{a 2 true {bar} 4.2 plop false}
{a 2 false {bar} 4.2 plop false}
{a 3 true {bar} 4.2 plop false}
{a 3 false {bar} 4.2 plop false}
{a 4 true {bar} 4.2 plop false}
{a 4 false {bar} 4.2 plop false}
{b 1 true {bar} 4.2 plop false}
{b 1 false {bar} 4.2 plop false}
{b 2 true {bar} 4.2 plop false}
{b 2 false {bar} 4.2 plop false}
{b 3 true {bar} 4.2 plop false}
{b 3 false {bar} 4.2 plop false}
{b 4 true {bar} 4.2 plop false}
{b 4 false {bar} 4.2 plop false}

func CrossJoin8

func CrossJoin8[A, B, C, D, E, F, G, H any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G], listH iter.Seq[H]) iter.Seq[lo.Tuple8[A, B, C, D, E, F, G, H]]

CrossJoin8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/5WqJN9-zv

Example
result := CrossJoin8(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false 42}
{a 1 false {bar} 4.2 plop false 42}
{a 2 true {bar} 4.2 plop false 42}
{a 2 false {bar} 4.2 plop false 42}
{a 3 true {bar} 4.2 plop false 42}
{a 3 false {bar} 4.2 plop false 42}
{a 4 true {bar} 4.2 plop false 42}
{a 4 false {bar} 4.2 plop false 42}
{b 1 true {bar} 4.2 plop false 42}
{b 1 false {bar} 4.2 plop false 42}
{b 2 true {bar} 4.2 plop false 42}
{b 2 false {bar} 4.2 plop false 42}
{b 3 true {bar} 4.2 plop false 42}
{b 3 false {bar} 4.2 plop false 42}
{b 4 true {bar} 4.2 plop false 42}
{b 4 false {bar} 4.2 plop false 42}

func CrossJoin9

func CrossJoin9[A, B, C, D, E, F, G, H, I any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G], listH iter.Seq[H], listI iter.Seq[I]) iter.Seq[lo.Tuple9[A, B, C, D, E, F, G, H, I]]

CrossJoin9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/6XrKO0-zw

Example
result := CrossJoin9(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42), values("hello world"))
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

{a 1 true {bar} 4.2 plop false 42 hello world}
{a 1 false {bar} 4.2 plop false 42 hello world}
{a 2 true {bar} 4.2 plop false 42 hello world}
{a 2 false {bar} 4.2 plop false 42 hello world}
{a 3 true {bar} 4.2 plop false 42 hello world}
{a 3 false {bar} 4.2 plop false 42 hello world}
{a 4 true {bar} 4.2 plop false 42 hello world}
{a 4 false {bar} 4.2 plop false 42 hello world}
{b 1 true {bar} 4.2 plop false 42 hello world}
{b 1 false {bar} 4.2 plop false 42 hello world}
{b 2 true {bar} 4.2 plop false 42 hello world}
{b 2 false {bar} 4.2 plop false 42 hello world}
{b 3 true {bar} 4.2 plop false 42 hello world}
{b 3 false {bar} 4.2 plop false 42 hello world}
{b 4 true {bar} 4.2 plop false 42 hello world}
{b 4 false {bar} 4.2 plop false 42 hello world}

func CrossJoinBy2

func CrossJoinBy2[A, B, Out any](listA iter.Seq[A], listB iter.Seq[B], project func(a A, b B) Out) iter.Seq[Out]

CrossJoinBy2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/7YsLP1-zx

Example
result := CrossJoinBy2(values("a", "b"), values(1, 2, 3, 4), func(a string, b int) string {
	return fmt.Sprintf("%v-%v", a, b)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1
a-2
a-3
a-4
b-1
b-2
b-3
b-4

func CrossJoinBy3

func CrossJoinBy3[A, B, C, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], project func(a A, b B, c C) Out) iter.Seq[Out]

CrossJoinBy3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8isgTsyfL-t

Example
result := CrossJoinBy3(values("a", "b"), values(1, 2, 3, 4), values(true, false), func(a string, b int, c bool) string {
	return fmt.Sprintf("%v-%v-%v", a, b, c)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true
a-1-false
a-2-true
a-2-false
a-3-true
a-3-false
a-4-true
a-4-false
b-1-true
b-1-false
b-2-true
b-2-false
b-3-true
b-3-false
b-4-true
b-4-false

func CrossJoinBy4

func CrossJoinBy4[A, B, C, D, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], project func(a A, b B, c C, d D) Out) iter.Seq[Out]

CrossJoinBy4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/9jthUzgF-u

Example
result := CrossJoinBy4(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), func(a string, b int, c bool, d foo) string {
	return fmt.Sprintf("%v-%v-%v-%v", a, b, c, d)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}
a-1-false-{bar}
a-2-true-{bar}
a-2-false-{bar}
a-3-true-{bar}
a-3-false-{bar}
a-4-true-{bar}
a-4-false-{bar}
b-1-true-{bar}
b-1-false-{bar}
b-2-true-{bar}
b-2-false-{bar}
b-3-true-{bar}
b-3-false-{bar}
b-4-true-{bar}
b-4-false-{bar}

func CrossJoinBy5

func CrossJoinBy5[A, B, C, D, E, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], project func(a A, b B, c C, d D, e E) Out) iter.Seq[Out]

CrossJoinBy5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/0XrQKOk-vw

Example
result := CrossJoinBy5(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), func(a string, b int, c bool, d foo, e float64) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v", a, b, c, d, e)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2
a-1-false-{bar}-4.2
a-2-true-{bar}-4.2
a-2-false-{bar}-4.2
a-3-true-{bar}-4.2
a-3-false-{bar}-4.2
a-4-true-{bar}-4.2
a-4-false-{bar}-4.2
b-1-true-{bar}-4.2
b-1-false-{bar}-4.2
b-2-true-{bar}-4.2
b-2-false-{bar}-4.2
b-3-true-{bar}-4.2
b-3-false-{bar}-4.2
b-4-true-{bar}-4.2
b-4-false-{bar}-4.2

func CrossJoinBy6

func CrossJoinBy6[A, B, C, D, E, F, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], project func(a A, b B, c C, d D, e E, f F) Out) iter.Seq[Out]

CrossJoinBy6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/1SmFJ5-zr

Example
result := CrossJoinBy6(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), func(a string, b int, c bool, d foo, e float64, f string) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v", a, b, c, d, e, f)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop
a-1-false-{bar}-4.2-plop
a-2-true-{bar}-4.2-plop
a-2-false-{bar}-4.2-plop
a-3-true-{bar}-4.2-plop
a-3-false-{bar}-4.2-plop
a-4-true-{bar}-4.2-plop
a-4-false-{bar}-4.2-plop
b-1-true-{bar}-4.2-plop
b-1-false-{bar}-4.2-plop
b-2-true-{bar}-4.2-plop
b-2-false-{bar}-4.2-plop
b-3-true-{bar}-4.2-plop
b-3-false-{bar}-4.2-plop
b-4-true-{bar}-4.2-plop
b-4-false-{bar}-4.2-plop

func CrossJoinBy7

func CrossJoinBy7[A, B, C, D, E, F, G, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G], project func(a A, b B, c C, d D, e E, f F, g G) Out) iter.Seq[Out]

CrossJoinBy7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/2TnGK6-zs

Example
result := CrossJoinBy7(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), func(a string, b int, c bool, d foo, e float64, f string, g bool) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false
a-1-false-{bar}-4.2-plop-false
a-2-true-{bar}-4.2-plop-false
a-2-false-{bar}-4.2-plop-false
a-3-true-{bar}-4.2-plop-false
a-3-false-{bar}-4.2-plop-false
a-4-true-{bar}-4.2-plop-false
a-4-false-{bar}-4.2-plop-false
b-1-true-{bar}-4.2-plop-false
b-1-false-{bar}-4.2-plop-false
b-2-true-{bar}-4.2-plop-false
b-2-false-{bar}-4.2-plop-false
b-3-true-{bar}-4.2-plop-false
b-3-false-{bar}-4.2-plop-false
b-4-true-{bar}-4.2-plop-false
b-4-false-{bar}-4.2-plop-false

func CrossJoinBy8

func CrossJoinBy8[A, B, C, D, E, F, G, H, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G], listH iter.Seq[H], project func(a A, b B, c C, d D, e E, f F, g G, h H) Out) iter.Seq[Out]

CrossJoinBy8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3UoHL7-zt

Example
result := CrossJoinBy8(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42), func(a string, b int, c bool, d foo, e float64, f string, g bool, h int) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false-42
a-1-false-{bar}-4.2-plop-false-42
a-2-true-{bar}-4.2-plop-false-42
a-2-false-{bar}-4.2-plop-false-42
a-3-true-{bar}-4.2-plop-false-42
a-3-false-{bar}-4.2-plop-false-42
a-4-true-{bar}-4.2-plop-false-42
a-4-false-{bar}-4.2-plop-false-42
b-1-true-{bar}-4.2-plop-false-42
b-1-false-{bar}-4.2-plop-false-42
b-2-true-{bar}-4.2-plop-false-42
b-2-false-{bar}-4.2-plop-false-42
b-3-true-{bar}-4.2-plop-false-42
b-3-false-{bar}-4.2-plop-false-42
b-4-true-{bar}-4.2-plop-false-42
b-4-false-{bar}-4.2-plop-false-42

func CrossJoinBy9

func CrossJoinBy9[A, B, C, D, E, F, G, H, I, Out any](listA iter.Seq[A], listB iter.Seq[B], listC iter.Seq[C], listD iter.Seq[D], listE iter.Seq[E], listF iter.Seq[F], listG iter.Seq[G], listH iter.Seq[H], listI iter.Seq[I], project func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) iter.Seq[Out]

CrossJoinBy9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/4VpIM8-zu

Example
result := CrossJoinBy9(values("a", "b"), values(1, 2, 3, 4), values(true, false), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42), values("hello world"), func(a string, b int, c bool, d foo, e float64, f string, g bool, h int, i string) string {
	return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h, i)
})
for r := range result {
	fmt.Printf("%v\n", r)
}
Output:

a-1-true-{bar}-4.2-plop-false-42-hello world
a-1-false-{bar}-4.2-plop-false-42-hello world
a-2-true-{bar}-4.2-plop-false-42-hello world
a-2-false-{bar}-4.2-plop-false-42-hello world
a-3-true-{bar}-4.2-plop-false-42-hello world
a-3-false-{bar}-4.2-plop-false-42-hello world
a-4-true-{bar}-4.2-plop-false-42-hello world
a-4-false-{bar}-4.2-plop-false-42-hello world
b-1-true-{bar}-4.2-plop-false-42-hello world
b-1-false-{bar}-4.2-plop-false-42-hello world
b-2-true-{bar}-4.2-plop-false-42-hello world
b-2-false-{bar}-4.2-plop-false-42-hello world
b-3-true-{bar}-4.2-plop-false-42-hello world
b-3-false-{bar}-4.2-plop-false-42-hello world
b-4-true-{bar}-4.2-plop-false-42-hello world
b-4-false-{bar}-4.2-plop-false-42-hello world

func CutPrefix

func CutPrefix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (after I, found bool)

CutPrefix returns collection without the provided leading prefix and reports whether it found the prefix. If collection doesn't start with prefix, CutPrefix returns collection, false. If prefix is empty, CutPrefix returns collection, true. Will iterate at most the size of separator before returning. Play: https://go.dev/play/p/2TnGK6-zs

Example
collection := slices.Values([]string{"a", "b", "c", "d", "e", "f", "g"})

// Test with valid prefix
after, found := CutPrefix(collection, []string{"a", "b", "c"})
fmt.Printf("After: %v, Found: %t\n", slices.Collect(after), found)

// Test with prefix not found
after2, found2 := CutPrefix(collection, []string{"b"})
fmt.Printf("After: %v, Found: %t\n", slices.Collect(after2), found2)

// Test with empty prefix
after3, found3 := CutPrefix(collection, []string{})
fmt.Printf("After: %v, Found: %t\n", slices.Collect(after3), found3)
Output:

After: [d e f g], Found: true
After: [a b c d e f g], Found: false
After: [a b c d e f g], Found: true

func CutSuffix

func CutSuffix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (before I, found bool)

CutSuffix returns collection without the provided ending suffix and reports whether it found the suffix. If collection doesn't end with suffix, CutSuffix returns collection, false. If suffix is empty, CutSuffix returns collection, true. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/3UoHL7-zt

Example
collection := slices.Values([]string{"a", "b", "c", "d", "e", "f", "g"})

// Test with valid suffix
before, found := CutSuffix(collection, []string{"f", "g"})
fmt.Printf("Before: %v, Found: %t\n", slices.Collect(before), found)

// Test with suffix not found
before2, found2 := CutSuffix(collection, []string{"b"})
fmt.Printf("Before: %v, Found: %t\n", slices.Collect(before2), found2)

// Test with empty suffix
before3, found3 := CutSuffix(collection, []string{})
fmt.Printf("Before: %v, Found: %t\n", slices.Collect(before3), found3)
Output:

Before: [a b c d e], Found: true
Before: [a b c d e f g], Found: false
Before: [a b c d e f g], Found: true

func Drain

func Drain[T any](collection iter.Seq[T])

Drain consumes an entire sequence.

Example
list := slices.Values([]int64{1, 2, 3, 4})

Drain(list)

func Drop

func Drop[T any, I ~func(func(T) bool)](collection I, n int) I

Drop drops n elements from the beginning of a sequence. Play: https://go.dev/play/p/1SmFJ5-zr

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := Drop(list, 2)

fmt.Printf("%v", slices.Collect(result))
Output:

[2 3 4 5]

func DropByIndex

func DropByIndex[T any, I ~func(func(T) bool)](collection I, indexes ...int) I

DropByIndex drops elements from a sequence by the index. Will allocate a map large enough to hold all distinct indexes. Play: https://go.dev/play/p/5WqJN9-zv

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := DropByIndex(list, 2)

fmt.Printf("%v", slices.Collect(result))
Output:

[0 1 3 4 5]

func DropLast

func DropLast[T any, I ~func(func(T) bool)](collection I, n int) I

DropLast drops n elements from the end of a sequence. Will allocate a slice of length n. Play: https://go.dev/play/p/2TnGK6-zs

func DropLastWhile

func DropLastWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I

DropLastWhile drops elements from the end of a sequence while the predicate returns true. Will allocate a slice large enough to hold the longest sequence of matching elements. Long input sequences of consecutive matches can cause excessive memory usage. Play: https://go.dev/play/p/4VpIM8-zu

func DropWhile

func DropWhile[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I

DropWhile drops elements from the beginning of a sequence while the predicate returns true. Play: https://go.dev/play/p/3UoHL7-zt

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := DropWhile(list, func(val int) bool {
	return val < 2
})

fmt.Printf("%v", slices.Collect(result))
Output:

[2 3 4 5]

func Earliest

func Earliest(times iter.Seq[time.Time]) time.Time

Earliest search the minimum time.Time of a collection. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/7EyYRV1-zd

Example
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)

result := Earliest(slices.Values([]time.Time{future, now, past}))

fmt.Printf("%t", result.Equal(past))
Output:

true

func EarliestBy

func EarliestBy[T any](collection iter.Seq[T], transform func(item T) time.Time) T

EarliestBy search the minimum time.Time of a collection using the given transform function. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/8FzSW2-ze

Example
type Event struct {
	Name string
	Time time.Time
}

now := time.Now()
events := slices.Values([]Event{
	{Name: "Event A", Time: now.Add(time.Hour)},
	{Name: "Event B", Time: now},
	{Name: "Event C", Time: now.Add(-time.Hour)},
})

result := EarliestBy(events, func(event Event) time.Time {
	return event.Time
})

fmt.Printf("%s", result.Name)
Output:

Event C

func ElementsMatch

func ElementsMatch[T comparable](list1, list2 iter.Seq[T]) bool

ElementsMatch returns true if lists contain the same set of elements (including empty set). If there are duplicate elements, the number of occurrences in each list should match. The order of elements is not checked. Will iterate through each sequence before returning and allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/yGpdBGaWPCA

func ElementsMatchBy

func ElementsMatchBy[T any, K comparable](list1, list2 iter.Seq[T], transform func(item T) K) bool

ElementsMatchBy returns true if lists contain the same set of elements' keys (including empty set). If there are duplicate keys, the number of occurrences in each list should match. The order of elements is not checked. Will iterate through each sequence before returning and allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage.

func Empty

func Empty[T any]() iter.Seq[T]

Empty returns an empty sequence. Play: https://go.dev/play/p/E5fF1hH8Bc3

func Entries

func Entries[K comparable, V any](in ...map[K]V) iter.Seq2[K, V]

Entries transforms a map into a sequence of key/value pairs. Play: https://go.dev/play/p/N8RbJ5t6H2k

Example
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := maps.Collect(Entries(kv))

fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
Output:

3 1 2 3

func Every

func Every[T comparable](collection iter.Seq[T], subset ...T) bool

Every returns true if all elements of a subset are contained in a collection or if the subset is empty. Will iterate through the entire sequence if subset elements always match. Play: https://go.dev/play/p/rwM9Y353aIC

func EveryBy

func EveryBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool

EveryBy returns true if the predicate returns true for all elements in the collection or if the collection is empty. Will iterate through the entire sequence if predicate never returns false.

func Fill

func Fill[T lo.Clonable[T], I ~func(func(T) bool)](collection I, initial T) I

Fill replaces elements of a sequence with `initial` value. Play: https://go.dev/play/p/0XrQKOk-vw

Example
list := slices.Values([]foo{{"a"}, {"a"}})

result := Fill(list, foo{"b"})

fmt.Printf("%v", slices.Collect(result))
Output:

[{b} {b}]

func Filter

func Filter[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I

Filter iterates over elements of collection, returning a sequence of all elements predicate returns true for. Play: https://go.dev/play/p/psenko2KKsX

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := Filter(list, func(nbr int64) bool {
	return nbr%2 == 0
})

fmt.Printf("%v", slices.Collect(result))
Output:

[2 4]

func FilterI

func FilterI[T any, I ~func(func(T) bool)](collection I, predicate func(item T, index int) bool) I

FilterI iterates over elements of collection, returning a sequence of all elements predicate returns true for. Play: https://go.dev/play/p/5fpdlQvdL-q

func FilterKeys

func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) iter.Seq[K]

FilterKeys transforms a map into a sequence based on predicate returns true for specific elements. It is a mix of Filter and Keys. Play: https://go.dev/play/p/T8vW9kX7nLm

Example
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := slices.Collect(FilterKeys(kv, func(k int, v string) bool {
	return v == "foo"
}))

fmt.Printf("%v", result)
Output:

[1]

func FilterMap

func FilterMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]

FilterMap returns a sequence obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/9jthUzgF-u

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := FilterMap(list, func(nbr int64) (string, bool) {
	return strconv.FormatInt(nbr*2, 10), nbr%2 == 0
})

fmt.Printf("%v", slices.Collect(result))
Output:

[4 8]

func FilterMapI

func FilterMapI[T, R any](collection iter.Seq[T], callback func(item T, index int) (R, bool)) iter.Seq[R]

FilterMapI returns a sequence obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/0XrQKOk-vw

func FilterMapToSeq

func FilterMapToSeq[K comparable, V, R any](in map[K]V, transform func(key K, value V) (R, bool)) iter.Seq[R]

FilterMapToSeq transforms a map into a sequence based on specified transform. The transform returns a value and a boolean. If the boolean is true, the value is added to the result sequence. If the boolean is false, the value is not added to the result sequence. The order of the keys in the input map is not specified and the order of the keys in the output sequence is not guaranteed. Play: https://go.dev/play/p/S6tY2uJ7nWq

Example
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := slices.Collect(FilterMapToSeq(kv, func(k int, v int64) (string, bool) {
	return fmt.Sprintf("%d_%d", k, v), k%2 == 0
}))

sort.Strings(result)
fmt.Printf("%v", result)
Output:

[2_2 4_4]

func FilterSeqToMap

func FilterSeqToMap[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V, bool)) map[K]V

FilterSeqToMap returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Will iterate through the entire sequence. Play: https://go.dev/play/p/8PjCG2-zo

Example
list := slices.Values([]string{"a", "aa", "aaa"})

result := FilterSeqToMap(list, func(str string) (string, int, bool) {
	return str, len(str), len(str) > 1
})

fmt.Printf("%v", result)
Output:

map[aa:2 aaa:3]

func FilterSeqToMapI

func FilterSeqToMapI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V, bool)) map[K]V

FilterSeqToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Will iterate through the entire sequence. Play: https://go.dev/play/p/9QkDH3-zp

func FilterValues

func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) iter.Seq[V]

FilterValues transforms a map into a sequence based on predicate returns true for specific elements. It is a mix of Filter and Values. Play: https://go.dev/play/p/U3yN7kV8oXp

Example
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}

result := slices.Collect(FilterValues(kv, func(k int, v string) bool {
	return v == "foo"
}))

fmt.Printf("%v", result)
Output:

[foo]

func Find

func Find[T any](collection iter.Seq[T], predicate func(item T) bool) (T, bool)

Find searches for an element in a sequence based on a predicate. Returns element and true if element was found. Will iterate through the entire sequence if predicate never returns true. Play: https://go.dev/play/p/5SdLM6jf-q

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
})

result, found := Find(users, func(user User) bool {
	return user.Age > 30
})

fmt.Printf("%s %t", result.Name, found)
Output:

Charlie true
Example (NotFound)
list := slices.Values([]int{1, 2, 3, 4, 5})

result, found := Find(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %t", result, found)
Output:

0 false

func FindDuplicates

func FindDuplicates[T comparable, I ~func(func(T) bool)](collection I) I

FindDuplicates returns a sequence with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order duplicates occur in the collection. Will allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/1YsRLPl-wx

Example
list := slices.Values([]int{1, 2, 2, 3, 3, 3, 4, 5})

result := FindDuplicates(list)

fmt.Printf("%v", slices.Collect(result))
Output:

[2 3]

func FindDuplicatesBy

func FindDuplicatesBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I

FindDuplicatesBy returns a sequence with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order duplicates occur in the sequence. A transform function is invoked for each element in the sequence to generate the criterion by which uniqueness is computed. Will allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/2ZtSMQm-xy

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 25},
	{Name: "David", Age: 30},
	{Name: "Eve", Age: 35},
})

result := FindDuplicatesBy(users, func(user User) int {
	return user.Age
})

fmt.Printf("%d", len(slices.Collect(result)))
Output:

2

func FindIndexOf

func FindIndexOf[T any](collection iter.Seq[T], predicate func(item T) bool) (T, int, bool)

FindIndexOf searches for an element in a sequence based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Will iterate through the entire sequence if predicate never returns true. Play: https://go.dev/play/p/6TeNM7kg-r

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result, index, found := FindIndexOf(list, func(n int) bool {
	return n > 2
})

fmt.Printf("%d %d %t", result, index, found)
Output:

3 2 true
Example (NotFound)
list := slices.Values([]int{1, 2, 3, 4, 5})

result, index, found := FindIndexOf(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %d %t", result, index, found)
Output:

0 -1 false

func FindLastIndexOf

func FindLastIndexOf[T any](collection iter.Seq[T], predicate func(item T) bool) (T, int, bool)

FindLastIndexOf searches for the last element in a sequence based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Will iterate through the entire sequence. Play: https://go.dev/play/p/7UfON8lh-s

Example
list := slices.Values([]int{1, 2, 3, 4, 3, 5})

result, index, found := FindLastIndexOf(list, func(n int) bool {
	return n == 3
})

fmt.Printf("%d %d %t", result, index, found)
Output:

3 4 true
Example (NotFound)
list := slices.Values([]int{1, 2, 3, 4, 5})

result, index, found := FindLastIndexOf(list, func(n int) bool {
	return n > 10
})

fmt.Printf("%d %d %t", result, index, found)
Output:

0 -1 false

func FindOrElse

func FindOrElse[T any](collection iter.Seq[T], fallback T, predicate func(item T) bool) T

FindOrElse searches for an element in a sequence based on a predicate. Returns the element if found or a given fallback value otherwise. Will iterate through the entire sequence if predicate never returns true. Play: https://go.dev/play/p/8VgPO9mi-t

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := FindOrElse(list, -1, func(n int) bool {
	return n > 10
})

fmt.Printf("%d", result)
Output:

-1
Example (Found)
list := slices.Values([]int{1, 2, 3, 4, 5})

result := FindOrElse(list, -1, func(n int) bool {
	return n > 3
})

fmt.Printf("%d", result)
Output:

4

func FindUniques

func FindUniques[T comparable, I ~func(func(T) bool)](collection I) I

FindUniques returns a sequence with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the collection. Will iterate through the entire sequence before yielding and allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/9WhQPJnj-u

Example
list := slices.Values([]int{1, 2, 2, 3, 3, 3, 4, 5})

result := FindUniques(list)

fmt.Printf("%v", slices.Collect(result))
Output:

[1 4 5]

func FindUniquesBy

func FindUniquesBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I

FindUniquesBy returns a sequence with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the sequence. A transform function is invoked for each element in the sequence to generate the criterion by which uniqueness is computed. Will iterate through the entire sequence before yielding and allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/0XrQKOk-vw

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 25},
	{Name: "David", Age: 30},
	{Name: "Eve", Age: 35},
})

result := FindUniquesBy(users, func(user User) int {
	return user.Age
})

fmt.Printf("%d", len(slices.Collect(result)))
Output:

1

func First

func First[T any](collection iter.Seq[T]) (T, bool)

First returns the first element of a collection and check for availability of the first element. Will iterate at most once. Play: https://go.dev/play/p/5MgZD9-zl

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result, found := First(list)

fmt.Printf("%d %t", result, found)
Output:

1 true
Example (Empty)
list := slices.Values([]int{})

result, found := First(list)

fmt.Printf("%d %t", result, found)
Output:

0 false

func FirstOr

func FirstOr[T any](collection iter.Seq[T], fallback T) T

FirstOr returns the first element of a collection or the fallback value if empty. Will iterate at most once. Play: https://go.dev/play/p/7OiBF1-zn

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := FirstOr(list, -1)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := slices.Values([]int{})

result := FirstOr(list, -1)

fmt.Printf("%d", result)
Output:

-1

func FirstOrEmpty

func FirstOrEmpty[T any](collection iter.Seq[T]) T

FirstOrEmpty returns the first element of a collection or zero value if empty. Will iterate at most once. Play: https://go.dev/play/p/6NhAE0-zm

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := FirstOrEmpty(list)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := slices.Values([]int{})

result := FirstOrEmpty(list)

fmt.Printf("%d", result)
Output:

0

func FlatMap

func FlatMap[T, R any](collection iter.Seq[T], transform func(item T) iter.Seq[R]) iter.Seq[R]

FlatMap manipulates a sequence and transforms and flattens it to a sequence of another type. The transform function can either return a sequence or a `nil`, and in the `nil` case no value is yielded. Play: https://go.dev/play/p/1YsRLPl-wx

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := FlatMap(list, func(nbr int64) iter.Seq[string] {
	return slices.Values([]string{
		strconv.FormatInt(nbr, 10), // base 10
		strconv.FormatInt(nbr, 2),  // base 2
	})
})

fmt.Printf("%v", slices.Collect(result))
Output:

[1 1 2 10 3 11 4 100]

func FlatMapI

func FlatMapI[T, R any](collection iter.Seq[T], transform func(item T, index int) iter.Seq[R]) iter.Seq[R]

FlatMapI manipulates a sequence and transforms and flattens it to a sequence of another type. The transform function can either return a sequence or a `nil`, and in the `nil` case no value is yielded. Play: https://go.dev/play/p/2ZtSMQm-xy

func Flatten

func Flatten[T any, I ~func(func(T) bool)](collection []I) I

Flatten returns a sequence a single level deep. Play: https://go.dev/play/p/6XrKO0-zw

Example
list := []iter.Seq[int]{slices.Values([]int{0, 1, 2}), slices.Values([]int{3, 4, 5})}

result := Flatten(list)

fmt.Printf("%v", slices.Collect(result))
Output:

[0 1 2 3 4 5]

func ForEach

func ForEach[T any](collection iter.Seq[T], transform func(item T))

ForEach iterates over elements of collection and invokes transform for each element. Will iterate through the entire sequence. Play: https://go.dev/play/p/agIsKpG-S-P

Example
list := slices.Values([]int64{1, 2, 3, 4})

ForEach(list, func(x int64) {
	fmt.Println(x)
})
Output:

1
2
3
4

func ForEachI

func ForEachI[T any](collection iter.Seq[T], transform func(item T, index int))

ForEachI iterates over elements of collection and invokes transform for each element. Will iterate through the entire sequence. Play: https://go.dev/play/p/6NhAE0-zm

func ForEachWhile

func ForEachWhile[T any](collection iter.Seq[T], predicate func(item T) bool)

ForEachWhile iterates over elements of collection and invokes predicate for each element collection return value decide to continue or break, like do while(). Will iterate through the entire sequence. Play: https://go.dev/play/p/7OiBF1-zn

Example
list := slices.Values([]int64{1, 2, -math.MaxInt, 4})

ForEachWhile(list, func(x int64) bool {
	if x < 0 {
		return false
	}
	fmt.Println(x)
	return true
})
Output:

1
2

func ForEachWhileI

func ForEachWhileI[T any](collection iter.Seq[T], predicate func(item T, index int) bool)

ForEachWhileI iterates over elements of collection and invokes predicate for each element collection return value decide to continue or break, like do while(). Will iterate through the entire sequence. Play: https://go.dev/play/p/8PjCG2-zo

func FromAnySeq

func FromAnySeq[T any](collection iter.Seq[any]) iter.Seq[T]

FromAnySeq returns a sequence with all elements mapped to a type. Panics on type conversion failure. Play: https://go.dev/play/p/D4eE0gG7Ab2

func FromEntries

func FromEntries[K comparable, V any](entries ...iter.Seq2[K, V]) map[K]V

FromEntries transforms a sequence of key/value pairs into a map. Play: https://go.dev/play/p/K3wL9j7TmXs

Example
result := FromEntries(maps.All(map[string]int{
	"foo": 1,
	"bar": 2,
	"baz": 3,
}))

fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
Output:

3 1 2 3

func FromPairs

func FromPairs[K comparable, V any](entries ...iter.Seq2[K, V]) map[K]V

FromPairs transforms a sequence of key/value pairs into a map. Alias of FromEntries(). Play: https://go.dev/play/p/K3wL9j7TmXs

func FromSeqPtr

func FromSeqPtr[T any](collection iter.Seq[*T]) iter.Seq[T]

FromSeqPtr returns a sequence with the pointer values. Returns a zero value in case of a nil pointer element. Play: https://go.dev/play/p/A1bB9dD4eYx

func FromSeqPtrOr

func FromSeqPtrOr[T any](collection iter.Seq[*T], fallback T) iter.Seq[T]

FromSeqPtrOr returns a sequence with the pointer values or the fallback value. Play: https://go.dev/play/p/B2cC8eE5fYz

func GroupBy

func GroupBy[T any, U comparable](collection iter.Seq[T], transform func(item T) U) map[U][]T

GroupBy returns an object composed of keys generated from the results of running each element of collection through transform. Will iterate through the entire sequence. Play: https://go.dev/play/p/2TnGK6-zs

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := GroupBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output:

[0 3]
[1 4]
[2 5]

func GroupByMap

func GroupByMap[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V)) map[K][]V

GroupByMap returns an object composed of keys generated from the results of running each element of collection through transform. Will iterate through the entire sequence. Play: https://go.dev/play/p/3UoHL7-zt

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := GroupByMap(list, func(i int) (int, int) {
	return i % 3, i * 2
})

fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output:

[0 6]
[2 8]
[4 10]

func HasPrefix

func HasPrefix[T comparable](collection iter.Seq[T], prefix ...T) bool

HasPrefix returns true if the collection has the prefix. Will iterate at most the size of prefix. Play: https://go.dev/play/p/3QbJK4hd-o

func HasSuffix

func HasSuffix[T comparable](collection iter.Seq[T], suffix ...T) bool

HasSuffix returns true if the collection has the suffix. Will iterate through the entire sequence and allocate a slice the size of suffix. Play: https://go.dev/play/p/4RcKL5ie-p

func IndexOf

func IndexOf[T comparable](collection iter.Seq[T], element T) int

IndexOf returns the index at which the first occurrence of a value is found in a sequence or -1 if the value cannot be found. Will iterate through the entire sequence if element is not found. Play: https://go.dev/play/p/1OZHU2yfb-m

Example
list := slices.Values([]string{"foo", "bar", "baz"})

result := IndexOf(list, "bar")

fmt.Printf("%d", result)
Output:

1
Example (NotFound)
list := slices.Values([]string{"foo", "bar", "baz"})

result := IndexOf(list, "qux")

fmt.Printf("%d", result)
Output:

-1

func Interleave

func Interleave[T any](collections ...iter.Seq[T]) iter.Seq[T]

Interleave round-robin alternating input sequences and sequentially appending value at index into result. Will allocate a slice the size of collections. Play: https://go.dev/play/p/7YsLP1-zx

Example
list1 := []iter.Seq[int]{slices.Values([]int{1, 4, 7}), slices.Values([]int{2, 5, 8}), slices.Values([]int{3, 6, 9})}
list2 := []iter.Seq[int]{slices.Values([]int{1}), slices.Values([]int{2, 5, 8}), slices.Values([]int{3, 6}), slices.Values([]int{4, 7, 9, 10})}

result1 := slices.Collect(Interleave(list1...))
result2 := slices.Collect(Interleave(list2...))

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
Output:

[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9 10]

func Intersect

func Intersect[T comparable, I ~func(func(T) bool)](lists ...I) I

Intersect returns the intersection between given collections. Will allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/kz3cGhGZZWF

func Invert

func Invert[K, V comparable](in iter.Seq2[K, V]) iter.Seq2[V, K]

Invert creates a sequence composed of inverted keys and values. Play: https://go.dev/play/p/H4jR7n2sF8k

Example
kv := maps.All(map[string]int{"foo": 1, "bar": 2, "baz": 3})

result := maps.Collect(Invert(kv))

fmt.Printf("%v %v %v %v", len(result), result[1], result[2], result[3])
Output:

3 foo bar baz

func IsEmpty

func IsEmpty[T any](collection iter.Seq[T]) bool

IsEmpty returns true if the sequence is empty. Will iterate at most once. Play: https://go.dev/play/p/F6gG2iI9Cd4

func IsNotEmpty

func IsNotEmpty[T any](collection iter.Seq[T]) bool

IsNotEmpty returns true if the sequence is not empty. Will iterate at most once. Play: https://go.dev/play/p/G7hH3jJ0De5

func IsSorted

func IsSorted[T constraints.Ordered](collection iter.Seq[T]) bool

IsSorted checks if a sequence is sorted. Will iterate through the entire sequence. Play: https://go.dev/play/p/9jthUzgF-u

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})

result := IsSorted(list)

fmt.Printf("%v", result)
Output:

true

func IsSortedBy

func IsSortedBy[T any, K constraints.Ordered](collection iter.Seq[T], transform func(item T) K) bool

IsSortedBy checks if a sequence is sorted by transform. Will iterate through the entire sequence. Play: https://go.dev/play/p/0XrQKOk-vw

Example
list := slices.Values([]string{"a", "bb", "ccc"})

result := IsSortedBy(list, func(s string) int {
	return len(s)
})

fmt.Printf("%v", result)
Output:

true

func KeyBy

func KeyBy[K comparable, V any](collection iter.Seq[V], transform func(item V) K) map[K]V

KeyBy transforms a sequence to a map based on a pivot transform function. Will iterate through the entire sequence. Play: https://go.dev/play/p/3AuTNRn-yz

Example
list := slices.Values([]string{"a", "aa", "aaa"})

result := KeyBy(list, func(str string) int {
	return len(str)
})

fmt.Printf("%v", result)
Output:

map[1:a 2:aa 3:aaa]

func Keyify

func Keyify[T comparable](collection iter.Seq[T]) map[T]struct{}

Keyify returns a map with each unique element of the sequence as a key. Will iterate through the entire sequence. Play: https://go.dev/play/p/0RlEI4-zq

Example
list := slices.Values([]string{"a", "a", "b", "b", "d"})

set := Keyify(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
Output:

true
false
map[a:{} b:{} d:{}]

func Keys

func Keys[K comparable, V any](in ...map[K]V) iter.Seq[K]

Keys creates a sequence of the map keys. Play: https://go.dev/play/p/Fu7h-eW18QM

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}

result := slices.Collect(Keys(kv, kv2))
sort.Strings(result)
fmt.Printf("%v", result)
Output:

[bar baz foo]

func Last

func Last[T any](collection iter.Seq[T]) (T, bool)

Last returns the last element of a collection or error if empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/8PjCG2-zo

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result, found := Last(list)

fmt.Printf("%d %t", result, found)
Output:

5 true
Example (Empty)
list := slices.Values([]int{})

result, found := Last(list)

fmt.Printf("%d %t", result, found)
Output:

0 false

func LastIndexOf

func LastIndexOf[T comparable](collection iter.Seq[T], element T) int

LastIndexOf returns the index at which the last occurrence of a value is found in a sequence or -1 if the value cannot be found. Will iterate through the entire sequence. Play: https://go.dev/play/p/2PaIZ3gc-n

Example
list := slices.Values([]string{"foo", "bar", "baz", "bar"})

result := LastIndexOf(list, "bar")

fmt.Printf("%d", result)
Output:

3
Example (NotFound)
list := slices.Values([]string{"foo", "bar", "baz"})

result := LastIndexOf(list, "qux")

fmt.Printf("%d", result)
Output:

-1

func LastOr

func LastOr[T any](collection iter.Seq[T], fallback T) T

LastOr returns the last element of a collection or the fallback value if empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/0RlEI4-zq

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := LastOr(list, -1)

fmt.Printf("%d", result)
Output:

5
Example (Empty)
list := slices.Values([]int{})

result := LastOr(list, -1)

fmt.Printf("%d", result)
Output:

-1

func LastOrEmpty

func LastOrEmpty[T any](collection iter.Seq[T]) T

LastOrEmpty returns the last element of a collection or zero value if empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/9QkDH3-zp

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := LastOrEmpty(list)

fmt.Printf("%d", result)
Output:

5
Example (Empty)
list := slices.Values([]int{})

result := LastOrEmpty(list)

fmt.Printf("%d", result)
Output:

0

func Latest

func Latest(times iter.Seq[time.Time]) time.Time

Latest search the maximum time.Time of a collection. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/3KeXB7-zj

Example
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)

result := Latest(slices.Values([]time.Time{future, now, past}))

fmt.Printf("%t", result.Equal(future))
Output:

true

func LatestBy

func LatestBy[T any](collection iter.Seq[T], transform func(item T) time.Time) T

LatestBy search the maximum time.Time of a collection using the given transform function. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/4LfYC8-zk

Example
type Event struct {
	Name string
	Time time.Time
}

now := time.Now()
events := slices.Values([]Event{
	{Name: "Event A", Time: now.Add(time.Hour)},
	{Name: "Event B", Time: now},
	{Name: "Event C", Time: now.Add(-time.Hour)},
})

result := LatestBy(events, func(event Event) time.Time {
	return event.Time
})

fmt.Printf("%s", result.Name)
Output:

Event A

func Length

func Length[T any](collection iter.Seq[T]) int

Length returns the length of collection. Will iterate through the entire sequence.

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := Length(list)

fmt.Printf("%v", result)
Output:

4

func Map

func Map[T, R any](collection iter.Seq[T], transform func(item T) R) iter.Seq[R]

Map manipulates a sequence and transforms it to a sequence of another type. Play: https://go.dev/play/p/rWZiPB-RZOo

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := Map(list, func(nbr int64) string {
	return strconv.FormatInt(nbr*2, 10)
})

fmt.Printf("%v", slices.Collect(result))
Output:

[2 4 6 8]

func MapI

func MapI[T, R any](collection iter.Seq[T], transform func(item T, index int) R) iter.Seq[R]

MapI manipulates a sequence and transforms it to a sequence of another type. Play: https://go.dev/play/p/6gqemRweL-r

func MapToSeq

func MapToSeq[K comparable, V, R any](in map[K]V, transform func(key K, value V) R) iter.Seq[R]

MapToSeq transforms a map into a sequence based on specified transform. Play: https://go.dev/play/p/R7sL5h4K3mV

Example
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := slices.Collect(MapToSeq(kv, func(k int, v int64) string {
	return fmt.Sprintf("%d_%d", k, v)
}))

sort.Strings(result)
fmt.Printf("%v", result)
Output:

[1_1 2_2 3_3 4_4]

func Max

func Max[T constraints.Ordered](collection iter.Seq[T]) T

Max searches the maximum value of a collection. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/9GaTX3-zf

Example
list := slices.Values([]int{3, 1, 4, 1, 5, 9, 2, 6})

result := Max(list)

fmt.Printf("%d", result)
Output:

9
Example (Empty)
list := slices.Values([]int{})

result := Max(list)

fmt.Printf("%d", result)
Output:

0

func MaxBy

func MaxBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) T

MaxBy search the maximum value of a collection using the given comparison function. If several values of the collection are equal to the greatest value, returns the first such value. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/1IcVZ5-zh

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
})

result := MaxBy(users, func(a, b User) bool {
	return a.Age > b.Age
})

fmt.Printf("%s", result.Name)
Output:

Charlie

func MaxIndex

func MaxIndex[T constraints.Ordered](collection iter.Seq[T]) (T, int)

MaxIndex searches the maximum value of a collection and the index of the maximum value. Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/0HbUY4-zg

Example
list := slices.Values([]int{3, 1, 4, 1, 5, 9, 2, 6})

result, index := MaxIndex(list)

fmt.Printf("%d %d", result, index)
Output:

9 5
Example (Empty)
list := slices.Values([]int{})

result, index := MaxIndex(list)

fmt.Printf("%d %d", result, index)
Output:

0 -1

func MaxIndexBy

func MaxIndexBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) (T, int)

MaxIndexBy search the maximum value of a collection using the given comparison function and the index of the maximum value. If several values of the collection are equal to the greatest value, returns the first such value. Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/2JdWA6-zi

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
})

result, index := MaxIndexBy(users, func(a, b User) bool {
	return a.Age > b.Age
})

fmt.Printf("%s %d", result.Name, index)
Output:

Charlie 2

func Mean

func Mean[T constraints.Float | constraints.Integer](collection iter.Seq[T]) T

Mean calculates the mean of a collection of numbers. Will iterate through the entire sequence. Play: https://go.dev/play/p/Lez0CsvVRl_l

Example
ints := slices.Values([]int{1, 2, 3, 4, 5})

result := Mean(ints)

fmt.Printf("%v", result)
Output:

3

func MeanBy

func MeanBy[T any, R constraints.Float | constraints.Integer](collection iter.Seq[T], transform func(item T) R) R

MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function. Will iterate through the entire sequence. Play: https://go.dev/play/p/Ked4rpztH5Y

Example
strs := slices.Values([]string{"foo", "bar"})

result := MeanBy(strs, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

3

func Min

func Min[T constraints.Ordered](collection iter.Seq[T]) T

Min search the minimum value of a collection. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/3AuTNRn-yz

Example
list := slices.Values([]int{3, 1, 4, 1, 5, 9, 2, 6})

result := Min(list)

fmt.Printf("%d", result)
Output:

1
Example (Empty)
list := slices.Values([]int{})

result := Min(list)

fmt.Printf("%d", result)
Output:

0

func MinBy

func MinBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) T

MinBy search the minimum value of a collection using the given comparison function. If several values of the collection are equal to the smallest value, returns the first such value. Returns zero value when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/5CwPTPz-zb

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
})

result := MinBy(users, func(a, b User) bool {
	return a.Age < b.Age
})

fmt.Printf("%s", result.Name)
Output:

Alice

func MinIndex

func MinIndex[T constraints.Ordered](collection iter.Seq[T]) (T, int)

MinIndex search the minimum value of a collection and the index of the minimum value. Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/4BvOSo-yza

Example
list := slices.Values([]int{3, 1, 4, 1, 5, 9, 2, 6})

result, index := MinIndex(list)

fmt.Printf("%d %d", result, index)
Output:

1 1
Example (Empty)
list := slices.Values([]int{})

result, index := MinIndex(list)

fmt.Printf("%d %d", result, index)
Output:

0 -1

func MinIndexBy

func MinIndexBy[T any](collection iter.Seq[T], comparison func(a, b T) bool) (T, int)

MinIndexBy search the minimum value of a collection using the given comparison function and the index of the minimum value. If several values of the collection are equal to the smallest value, returns the first such value. Returns (zero value, -1) when the collection is empty. Will iterate through the entire sequence. Play: https://go.dev/play/p/6DxQUQ0-zc

Example
type User struct {
	Name string
	Age  int
}

users := slices.Values([]User{
	{Name: "Alice", Age: 25},
	{Name: "Bob", Age: 30},
	{Name: "Charlie", Age: 35},
})

result, index := MinIndexBy(users, func(a, b User) bool {
	return a.Age < b.Age
})

fmt.Printf("%s %d", result.Name, index)
Output:

Alice 0

func Mode

func Mode[T constraints.Integer | constraints.Float](collection iter.Seq[T]) []T

Mode returns the mode (most frequent value) of a collection. If multiple values have the same highest frequency, then multiple values are returned. If the collection is empty, then the zero value of T is returned. Will iterate through the entire sequence and allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/c_cmMMA5EhH

func None

func None[T comparable](collection iter.Seq[T], subset ...T) bool

None returns true if no element of a subset is contained in a collection or if the subset is empty. Will iterate through the entire sequence if subset elements never match. Play: https://go.dev/play/p/KmX-fXictQl

func NoneBy

func NoneBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool

NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty. Will iterate through the entire sequence if predicate never returns true.

func Nth

func Nth[T any, N constraints.Integer](collection iter.Seq[T], nth N) (T, error)

Nth returns the element at index `nth` of collection. An error is returned when nth is out of bounds. Will iterate n times through the sequence. Play: https://go.dev/play/p/1SmFJ5-zr

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result, err := Nth(list, 2)

fmt.Printf("%d %v", result, err)
Output:

3 <nil>
Example (OutOfBounds)
list := slices.Values([]int{1, 2, 3, 4, 5})

result, err := Nth(list, 10)

fmt.Printf("%d %v", result, err)
Output:

0 nth: 10 out of bounds

func NthOr

func NthOr[T any, N constraints.Integer](collection iter.Seq[T], nth N, fallback T) T

NthOr returns the element at index `nth` of collection. If `nth` is out of bounds, it returns the fallback value instead of an error. Will iterate n times through the sequence. Play: https://go.dev/play/p/2TnGK6-zs

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := NthOr(list, 2, -1)

fmt.Printf("%d", result)
Output:

3
Example (OutOfBounds)
list := slices.Values([]int{1, 2, 3, 4, 5})

result := NthOr(list, 10, -1)

fmt.Printf("%d", result)
Output:

-1

func NthOrEmpty

func NthOrEmpty[T any, N constraints.Integer](collection iter.Seq[T], nth N) T

NthOrEmpty returns the element at index `nth` of collection. If `nth` is out of bounds, it returns the zero value (empty value) for that type. Will iterate n times through the sequence. Play: https://go.dev/play/p/3UoHL7-zt

Example
list := slices.Values([]int{1, 2, 3, 4, 5})

result := NthOrEmpty(list, 2)

fmt.Printf("%d", result)
Output:

3
Example (OutOfBounds)
list := slices.Values([]int{1, 2, 3, 4, 5})

result := NthOrEmpty(list, 10)

fmt.Printf("%d", result)
Output:

0

func PartitionBy

func PartitionBy[T any, K comparable](collection iter.Seq[T], transform func(item T) K) [][]T

PartitionBy returns a sequence of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through transform. Will allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/5WqJN9-zv

Example
list := slices.Values([]int{-2, -1, 0, 1, 2, 3, 4})

result := PartitionBy(list, func(x int) string {
	if x < 0 {
		return "negative"
	} else if x%2 == 0 {
		return "even"
	}
	return "odd"
})

for _, v := range result {
	fmt.Printf("%v\n", v)
}
Output:

[-2 -1]
[0 2 4]
[1 3]

func Product

func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T]) T

Product gets the product of the values in a collection. If collection is empty 1 is returned. Will iterate through the entire sequence. Play: https://go.dev/play/p/ebgxKxJmhLj

Example
ints := slices.Values([]int{1, 2, 3, 4, 5})

result := Product(ints)

fmt.Printf("%v", result)
Output:

120

func ProductBy

func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T], transform func(item T) R) R

ProductBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 1 is returned. Will iterate through the entire sequence. Play: https://go.dev/play/p/BkRYS0NG42b

Example
strs := slices.Values([]string{"foo", "bar"})

result := ProductBy(strs, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

9

func Range

func Range(elementNum int) iter.Seq[int]

Range creates a sequence of numbers (positive and/or negative) with given length. Play: https://go.dev/play/p/6ksL0W6KEuQ

Example
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)

fmt.Printf("%v\n", slices.Collect(result1))
fmt.Printf("%v\n", slices.Collect(result2))
fmt.Printf("%v\n", slices.Collect(result3))
fmt.Printf("%v\n", slices.Collect(result4))
fmt.Printf("%v\n", slices.Collect(result5))
fmt.Printf("%v\n", slices.Collect(result6))
fmt.Printf("%v\n", slices.Collect(result7))
fmt.Printf("%v\n", slices.Collect(result8))
Output:

[0 1 2 3]
[0 -1 -2 -3]
[1 2 3 4 5]
[1 2 3 4 5]
[0 5 10 15]
[-1 -2 -3]
[]
[]

func RangeFrom

func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) iter.Seq[T]

RangeFrom creates a sequence of numbers from start with specified length. Play: https://go.dev/play/p/WHP_NI5scj9

func RangeWithSteps

func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) iter.Seq[T]

RangeWithSteps creates a sequence of numbers (positive and/or negative) progressing from start up to, but not including end. step set to zero will return an empty sequence. Play: https://go.dev/play/p/qxm2YNLG0vT

func Reduce

func Reduce[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R

Reduce reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous. Will iterate through the entire sequence. Play: https://go.dev/play/p/FmkVUf39ZP_Y

Example
list := slices.Values([]int64{1, 2, 3, 4})

result := Reduce(list, func(agg, item int64) int64 {
	return agg + item
}, 0)

fmt.Printf("%v", result)
Output:

10

func ReduceI

func ReduceI[T, R any](collection iter.Seq[T], accumulator func(agg R, item T, index int) R, initial R) R

ReduceI reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous. Will iterate through the entire sequence. Play: https://go.dev/play/p/3AuTNRn-yz

func ReduceLast

func ReduceLast[T, R any](collection iter.Seq[T], accumulator func(agg R, item T) R, initial R) R

ReduceLast is like Reduce except that it iterates over elements of collection in reverse. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/4BvOSo-yza

Example
list := slices.Values([][]int{{0, 1}, {2, 3}, {4, 5}})

result := ReduceLast(list, func(agg, item []int) []int {
	return append(agg, item...)
}, []int{})

fmt.Printf("%v", result)
Output:

[4 5 2 3 0 1]

func ReduceLastI

func ReduceLastI[T, R any](collection iter.Seq[T], accumulator func(agg R, item T, index int) R, initial R) R

ReduceLastI is like Reduce except that it iterates over elements of collection in reverse. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/5CwPTPz-zb

func Reject

func Reject[T any, I ~func(func(T) bool)](collection I, predicate func(item T) bool) I

Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return true for. Play: https://go.dev/play/p/6XrKO0-zw

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := Reject(list, func(x int) bool {
	return x%2 == 0
})

fmt.Printf("%v", slices.Collect(result))
Output:

[1 3 5]

func RejectI

func RejectI[T any, I ~func(func(T) bool)](collection I, predicate func(item T, index int) bool) I

RejectI is the opposite of Filter, this method returns the elements of collection that predicate does not return true for. Play: https://go.dev/play/p/7YsLP1-zx

func RejectMap

func RejectMap[T, R any](collection iter.Seq[T], callback func(item T) (R, bool)) iter.Seq[R]

RejectMap is the opposite of FilterMap, this method returns a sequence obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/8isgTsyfL-t

func RejectMapI

func RejectMapI[T, R any](collection iter.Seq[T], callback func(item T, index int) (R, bool)) iter.Seq[R]

RejectMapI is the opposite of FilterMap, this method returns a sequence obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/9jthUzgF-u

func Repeat

func Repeat[T lo.Clonable[T]](count int, initial T) iter.Seq[T]

Repeat builds a sequence with N copies of initial value. Play: https://go.dev/play/p/1YsRLPl-wx

Example
result := Repeat(2, foo{"a"})

fmt.Printf("%v", slices.Collect(result))
Output:

[{a} {a}]

func RepeatBy

func RepeatBy[T any](count int, transform func(index int) T) iter.Seq[T]

RepeatBy builds a sequence with values returned by N calls of transform. Play: https://go.dev/play/p/2ZtSMQm-xy

Example
result := RepeatBy(5, func(i int) string {
	return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})

fmt.Printf("%v", slices.Collect(result))
Output:

[0 1 4 9 16]

func Replace

func Replace[T comparable, I ~func(func(T) bool)](collection I, old, nEw T, n int) I

Replace returns a sequence with the first n non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/6XrKO0-zw

Example
list := slices.Values([]int{0, 1, 0, 1, 2, 3, 0})

result := Replace(list, 0, 42, 1)
fmt.Printf("%v\n", slices.Collect(result))

result = Replace(list, -1, 42, 1)
fmt.Printf("%v\n", slices.Collect(result))

result = Replace(list, 0, 42, 2)
fmt.Printf("%v\n", slices.Collect(result))

result = Replace(list, 0, 42, -1)
fmt.Printf("%v\n", slices.Collect(result))
Output:

[42 1 0 1 2 3 0]
[0 1 0 1 2 3 0]
[42 1 42 1 2 3 0]
[42 1 42 1 2 3 42]

func ReplaceAll

func ReplaceAll[T comparable, I ~func(func(T) bool)](collection I, old, nEw T) I

ReplaceAll returns a sequence with all non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/7YsLP1-zx

func Reverse

func Reverse[T any, I ~func(func(T) bool)](collection I) I

Reverse reverses a sequence so that the first element becomes the last, the second element becomes the second to last, and so on. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/9jthUzgF-u

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := slices.Collect(Reverse(list))

fmt.Printf("%v", result)
Output:

[5 4 3 2 1 0]

func Sample

func Sample[T any](collection iter.Seq[T]) T

Sample returns a random item from collection. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/4VpIM8-zu

func SampleBy

func SampleBy[T any](collection iter.Seq[T], randomIntGenerator func(int) int) T

SampleBy returns a random item from collection, using randomIntGenerator as the random index generator. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/5WqJN9-zv

func Samples

func Samples[T any, I ~func(func(T) bool)](collection I, count int) I

Samples returns N random unique items from collection. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/6XrKO0-zw

func SamplesBy

func SamplesBy[T any, I ~func(func(T) bool)](collection I, count int, randomIntGenerator func(int) int) I

SamplesBy returns N random unique items from collection, using randomIntGenerator as the random index generator. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/7YsLP1-zx

func Seq2KeyToSeq

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

Seq2KeyToSeq converts a sequence of key-value pairs into a sequence of keys. Play: https://go.dev/play/p/W6xM7zZ9oSt

Example
result := slices.Collect(Seq2KeyToSeq(maps.All(map[string]int{
	"foo": 1,
	"bar": 2,
	"baz": 3,
})))

sort.Strings(result)
fmt.Printf("%v", result)
Output:

[bar baz foo]

func Seq2ValueToSeq

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

Seq2ValueToSeq converts a sequence of key-value pairs into a sequence of values. Play: https://go.dev/play/p/X7yN8aA1pUv

Example
result := slices.Collect(Seq2ValueToSeq(maps.All(map[string]int{
	"foo": 1,
	"bar": 2,
	"baz": 3,
})))

sort.Ints(result)
fmt.Printf("%v", result)
Output:

[1 2 3]

func SeqToChannel

func SeqToChannel[T any](bufferSize int, collection iter.Seq[T]) <-chan T

SeqToChannel returns a read-only channel of collection elements. Play: https://go.dev/play/p/id3jqJPffT6

func SeqToChannel2

func SeqToChannel2[K, V any](bufferSize int, collection iter.Seq2[K, V]) <-chan lo.Tuple2[K, V]

SeqToChannel2 returns a read-only channel of collection elements. Play: https://go.dev/play/p/rpJdVnXUaG-

func SeqToMap

func SeqToMap[T any, K comparable, V any](collection iter.Seq[T], transform func(item T) (K, V)) map[K]V

SeqToMap returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. Alias of Associate(). Will iterate through the entire sequence. Play: https://go.dev/play/p/6NhAE0-zm

Example
list := slices.Values([]string{"a", "aa", "aaa"})

result := SeqToMap(list, func(str string) (string, int) {
	return str, len(str)
})

fmt.Printf("%v", result)
Output:

map[a:1 aa:2 aaa:3]

func SeqToMapI

func SeqToMapI[T any, K comparable, V any](collection iter.Seq[T], transform func(item T, index int) (K, V)) map[K]V

SeqToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given sequence. If any of two pairs have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original sequence. Alias of AssociateI(). Will iterate through the entire sequence. Play: https://go.dev/play/p/7OiBF1-zn

func SeqToSeq2

func SeqToSeq2[T any](in iter.Seq[T]) iter.Seq2[int, T]

SeqToSeq2 converts a sequence into a sequence of key-value pairs keyed by index. Play: https://go.dev/play/p/V5wL9xY8nQr

Example
result := maps.Collect(SeqToSeq2(slices.Values([]string{"foo", "bar", "baz"})))

fmt.Printf("%v %v %v %v", len(result), result[0], result[1], result[2])
Output:

3 foo bar baz

func Shuffle

func Shuffle[T any, I ~func(func(T) bool)](collection I) I

Shuffle returns a sequence of shuffled values. Uses the Fisher-Yates shuffle algorithm. Will iterate through the entire sequence and allocate a slice large enough to hold all elements. Long input sequences can cause excessive memory usage. Play: https://go.dev/play/p/8isgTsyfL-t

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := slices.Collect(Shuffle(list))

fmt.Printf("%v", result)

func Slice

func Slice[T any, I ~func(func(T) bool)](collection I, start, end int) I

Slice returns a subset of a sequence from `start` up to, but not including `end`. Will iterate at most end times. Play: https://go.dev/play/p/5WqJN9-zv

Example
list := values(0, 1, 2, 3, 4, 5)

result := Slice(list, 1, 4)
fmt.Printf("%v\n", slices.Collect(result))

result = Slice(list, 4, 1)
fmt.Printf("%v\n", slices.Collect(result))

result = Slice(list, 4, 5)
fmt.Printf("%v\n", slices.Collect(result))
Output:

[1 2 3]
[]
[4]

func Some

func Some[T comparable](collection iter.Seq[T], subset ...T) bool

Some returns true if at least 1 element of a subset is contained in a collection. If the subset is empty Some returns false. Will iterate through the entire sequence if subset elements never match. Play: https://go.dev/play/p/KmX-fXictQl

func SomeBy

func SomeBy[T any](collection iter.Seq[T], predicate func(item T) bool) bool

SomeBy returns true if the predicate returns true for any of the elements in the collection. If the collection is empty SomeBy returns false. Will iterate through the entire sequence if predicate never returns true.

func Splice

func Splice[T any, I ~func(func(T) bool)](collection I, index int, elements ...T) I

Splice inserts multiple elements at index i. The helper is protected against overflow errors. Play: https://go.dev/play/p/1SmFJ5-zr

func Subset

func Subset[T any, I ~func(func(T) bool)](collection I, offset, length int) I

Subset returns a subset of a sequence from `offset` up to `length` elements. Will iterate at most offset+length times. Play: https://go.dev/play/p/4VpIM8-zu

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := Subset(list, 2, 3)

fmt.Printf("%v", slices.Collect(result))
Output:

[2 3 4]

func Sum

func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T]) T

Sum sums the values in a collection. If collection is empty 0 is returned. Will iterate through the entire sequence. Play: https://go.dev/play/p/nHbGFOEIeTa

Example
ints := slices.Values([]int{1, 2, 3, 4, 5})

sum := Sum(ints)

fmt.Printf("%v", sum)
Output:

15

func SumBy

func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection iter.Seq[T], transform func(item T) R) R

SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned. Will iterate through the entire sequence. Play: https://go.dev/play/p/ZNiqXNMu5QP

Example
ints := slices.Values([]string{"foo", "bar"})

result := SumBy(ints, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

6

func Times

func Times[T any](count int, transform func(index int) T) iter.Seq[T]

Times invokes transform n times and returns a sequence of results. The transform is invoked with index as argument. Play: https://go.dev/play/p/9QkDH3-zp

Example
result := Times(3, func(i int) string {
	return strconv.FormatInt(int64(i), 10)
})

fmt.Printf("%v", slices.Collect(result))
Output:

[0 1 2]

func ToAnySeq

func ToAnySeq[T any](collection iter.Seq[T]) iter.Seq[any]

ToAnySeq returns a sequence with all elements mapped to `any` type. Play: https://go.dev/play/p/C3dD9fF6Za1

func ToPairs

func ToPairs[K comparable, V any](in ...map[K]V) iter.Seq2[K, V]

ToPairs transforms a map into a sequence of key/value pairs. Alias of Entries(). Play: https://go.dev/play/p/N8RbJ5t6H2k

func ToSeqPtr

func ToSeqPtr[T any](collection iter.Seq[T]) iter.Seq[*T]

ToSeqPtr returns a sequence of pointers to each value. Play: https://go.dev/play/p/Z9nA8cC3dXw

func Trim

func Trim[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I

Trim removes all the leading and trailing cutset from the collection. Will allocate a map large enough to hold all distinct cutset elements. Play: https://go.dev/play/p/4VpIM8-zu

Example
collection := slices.Values([]int{0, 1, 2, 0, 3, 0})

// Test with valid cutset
result := Trim(collection, 0)
fmt.Printf("Trim with cutset {0}: %v\n", slices.Collect(result))

// Test with string collection
words := slices.Values([]string{"  hello  ", "world", "  "})
result2 := Trim(words, " ")
fmt.Printf("Trim with string cutset: %v\n", slices.Collect(result2))

// Test with no cutset elements
result3 := Trim(collection, 5)
fmt.Printf("Trim with cutset {5} (not present): %v\n", slices.Collect(result3))
Output:

Trim with cutset {0}: [1 2 0 3]
Trim with string cutset: [  hello   world   ]
Trim with cutset {5} (not present): [0 1 2 0 3 0]

func TrimFirst

func TrimFirst[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I

TrimFirst removes all the leading cutset from the collection. Will allocate a map large enough to hold all distinct cutset elements. Play: https://go.dev/play/p/5WqJN9-zv

Example
collection := slices.Values([]int{0, 1, 2, 0, 3, 0})

// Test with valid cutset
result := TrimFirst(collection, 0)
fmt.Printf("TrimFirst with cutset {0}: %v\n", slices.Collect(result))

// Test with string collection
words := slices.Values([]string{"  hello  ", "world", "  "})
result2 := TrimFirst(words, " ")
fmt.Printf("TrimFirst with string cutset: %v\n", slices.Collect(result2))

// Test with no cutset elements
result3 := TrimFirst(collection, 5)
fmt.Printf("TrimFirst with cutset {5} (not present): %v\n", slices.Collect(result3))
Output:

TrimFirst with cutset {0}: [1 2 0 3 0]
TrimFirst with string cutset: [  hello   world   ]
TrimFirst with cutset {5} (not present): [0 1 2 0 3 0]

func TrimLast

func TrimLast[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I

TrimLast removes all the trailing cutset from the collection. Will allocate a map large enough to hold all distinct cutset elements. Play: https://go.dev/play/p/7YsLP1-zx

Example
collection := slices.Values([]int{0, 1, 2, 0, 3, 0})

// Test with valid cutset
result := TrimLast(collection, 0)
fmt.Printf("TrimLast with cutset {0}: %v\n", slices.Collect(result))

// Test with string collection
words := slices.Values([]string{"  hello  ", "world", "  "})
result2 := TrimLast(words, " ")
fmt.Printf("TrimLast with string cutset: %v\n", slices.Collect(result2))

// Test with no cutset elements
result3 := TrimLast(collection, 5)
fmt.Printf("TrimLast with cutset {5} (not present): %v\n", slices.Collect(result3))
Output:

TrimLast with cutset {0}: [0 1 2 0 3]
TrimLast with string cutset: [  hello   world   ]
TrimLast with cutset {5} (not present): [0 1 2 0 3 0]

func TrimPrefix

func TrimPrefix[T comparable, I ~func(func(T) bool)](collection I, prefix []T) I

TrimPrefix removes all the leading prefix from the collection. Play: https://go.dev/play/p/6XrKO0-zw

Example
collection := slices.Values([]int{1, 2, 1, 2, 3})

// Test with valid prefix
result := TrimPrefix(collection, []int{1, 2})
fmt.Printf("TrimPrefix with prefix {1,2}: %v\n", slices.Collect(result))

// Test with string collection
words := slices.Values([]string{"hello", "hello", "world"})
result2 := TrimPrefix(words, []string{"hello"})
fmt.Printf("TrimPrefix with string prefix: %v\n", slices.Collect(result2))

// Test with prefix not present
result3 := TrimPrefix(collection, []int{5, 6})
fmt.Printf("TrimPrefix with prefix {5,6} (not present): %v\n", slices.Collect(result3))
Output:

TrimPrefix with prefix {1,2}: [3]
TrimPrefix with string prefix: [world]
TrimPrefix with prefix {5,6} (not present): [1 2 1 2 3]

func TrimSuffix

func TrimSuffix[T comparable, I ~func(func(T) bool)](collection I, suffix []T) I

TrimSuffix removes all the trailing suffix from the collection. Play: https://go.dev/play/p/8isgTsyfL-t

Example
collection := slices.Values([]int{1, 2, 1, 2, 3})

// Test with valid suffix
result := TrimSuffix(collection, []int{1, 2})
fmt.Printf("TrimSuffix with suffix {1,2}: %v\n", slices.Collect(result))

// Test with string collection
words := slices.Values([]string{"hello", "world", "test"})
result2 := TrimSuffix(words, []string{"test"})
fmt.Printf("TrimSuffix with string suffix: %v\n", slices.Collect(result2))

// Test with suffix not present
result3 := TrimSuffix(collection, []int{5, 6})
fmt.Printf("TrimSuffix with suffix {5,6} (not present): %v\n", slices.Collect(result3))
Output:

TrimSuffix with suffix {1,2}: [1 2 1 2 3]
TrimSuffix with string suffix: [hello world]
TrimSuffix with suffix {5,6} (not present): [1 2 1 2 3]

func Union

func Union[T comparable, I ~func(func(T) bool)](lists ...I) I

Union returns all distinct elements from given collections. Will allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/ImIoFNpSUUB

func Uniq

func Uniq[T comparable, I ~func(func(T) bool)](collection I) I

Uniq returns a duplicate-free version of a sequence, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the sequence. Will allocate a map large enough to hold all distinct elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/0RlEI4-zq

Example
list := slices.Values([]int{1, 2, 2, 1})

result := Uniq(list)

fmt.Printf("%v", slices.Collect(result))
Output:

[1 2]

func UniqBy

func UniqBy[T any, U comparable, I ~func(func(T) bool)](collection I, transform func(item T) U) I

UniqBy returns a duplicate-free version of a sequence, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the sequence. A transform function is invoked for each element in the sequence to generate the criterion by which uniqueness is computed. Will allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/1SmFJ5-zr

Example
list := slices.Values([]int{0, 1, 2, 3, 4, 5})

result := UniqBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v", slices.Collect(result))
Output:

[0 1 2]

func UniqKeys

func UniqKeys[K comparable, V any](in ...map[K]V) iter.Seq[K]

UniqKeys creates a sequence of unique keys in the map. Will allocate a map large enough to hold all distinct input keys. Long input sequences with heterogeneous keys can cause excessive memory usage. Play: https://go.dev/play/p/_NicwfgAHbO

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"bar": 3}

result := slices.Collect(UniqKeys(kv, kv2))
sort.Strings(result)
fmt.Printf("%v", result)
Output:

[bar foo]

func UniqMap

func UniqMap[T any, R comparable](collection iter.Seq[T], transform func(item T) R) iter.Seq[R]

UniqMap manipulates a sequence and transforms it to a sequence of another type with unique values. Will allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/7hrfnSxfL-s

Example
type User struct {
	Name string
	Age  int
}
users := slices.Values([]User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}})

result := UniqMap(users, func(u User) string {
	return u.Name
})

fmt.Printf("%v", slices.Collect(result))
Output:

[Alex Bob Alice]

func UniqMapI

func UniqMapI[T any, R comparable](collection iter.Seq[T], transform func(item T, index int) R) iter.Seq[R]

UniqMapI manipulates a sequence and transforms it to a sequence of another type with unique values. Will allocate a map large enough to hold all distinct transformed elements. Long heterogeneous input sequences can cause excessive memory usage. Play: https://go.dev/play/p/8isgTsyfL-t

func UniqValues

func UniqValues[K, V comparable](in ...map[K]V) iter.Seq[V]

UniqValues creates a sequence of unique values in the map. Will allocate a map large enough to hold all distinct input values. Long input sequences with heterogeneous values can cause excessive memory usage. Play: https://go.dev/play/p/M7qV2xP4yG8

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 2}

result := slices.Collect(UniqValues(kv, kv2))

sort.Ints(result)
fmt.Printf("%v", result)
Output:

[1 2]

func Values

func Values[K comparable, V any](in ...map[K]V) iter.Seq[V]

Values creates a sequence of the map values. Play: https://go.dev/play/p/L9KcJ3h8E4f

Example
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}

result := slices.Collect(Values(kv, kv2))

sort.Ints(result)
fmt.Printf("%v", result)
Output:

[1 2 3]

func Without

func Without[T comparable, I ~func(func(T) bool)](collection I, exclude ...T) I

Without returns a sequence excluding all given values. Will allocate a map large enough to hold all distinct excludes. Play: https://go.dev/play/p/eAOoUsQnrZf

func WithoutBy

func WithoutBy[T any, K comparable, I ~func(func(T) bool)](collection I, transform func(item T) K, exclude ...K) I

WithoutBy filters a sequence by excluding elements whose extracted keys match any in the exclude list. Returns a sequence containing only the elements whose keys are not in the exclude list. Will allocate a map large enough to hold all distinct excludes.

Example
type User struct {
	ID   int
	Name string
}
// original users
users := values(
	User{ID: 1, Name: "Alice"},
	User{ID: 2, Name: "Bob"},
	User{ID: 3, Name: "Charlie"},
)

// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}

// extract function to get the user ID
extractID := func(user User) int {
	return user.ID
}

// filtering users
filteredUsers := WithoutBy(users, extractID, excludedIDs...)

// output the filtered users
fmt.Printf("%v", slices.Collect(filteredUsers))
Output:

[{1 Alice}]

func WithoutNth

func WithoutNth[T comparable, I ~func(func(T) bool)](collection I, nths ...int) I

WithoutNth returns a sequence excluding the nth value. Will allocate a map large enough to hold all distinct nths.

func Zip2

func Zip2[A, B any](a iter.Seq[A], b iter.Seq[B]) iter.Seq[lo.Tuple2[A, B]]

Zip2 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/U5nBWvR8eUZ

Example
result := Zip2(values("hello"), values(2))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2}]

func Zip3

func Zip3[A, B, C any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C]) iter.Seq[lo.Tuple3[A, B, C]]

Zip3 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/V5wL9xY8nQr

Example
result := Zip3(values("hello"), values(2), values(true))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true}]

func Zip4

func Zip4[A, B, C, D any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D]) iter.Seq[lo.Tuple4[A, B, C, D]]

Zip4 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/W6xM7zZ9oSt

Example
result := Zip4(values("hello"), values(2), values(true), values(foo{bar: "bar"}))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar}}]

func Zip5

func Zip5[A, B, C, D, E any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E]) iter.Seq[lo.Tuple5[A, B, C, D, E]]

Zip5 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/X7yN8aA1pUv

Example
result := Zip5(values("hello"), values(2), values(true), values(foo{bar: "bar"}), values(4.2))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar} 4.2}]

func Zip6

func Zip6[A, B, C, D, E, F any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F]) iter.Seq[lo.Tuple6[A, B, C, D, E, F]]

Zip6 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/Y4mN8bB2cXw

Example
result := Zip6(values("hello"), values(2), values(true), values(foo{bar: "bar"}), values(4.2), values("plop"))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar} 4.2 plop}]

func Zip7

func Zip7[A, B, C, D, E, F, G any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G]) iter.Seq[lo.Tuple7[A, B, C, D, E, F, G]]

Zip7 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/Z9nA8cC3dXw

Example
result := Zip7(values("hello"), values(2), values(true), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar} 4.2 plop false}]

func Zip8

func Zip8[A, B, C, D, E, F, G, H any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G], h iter.Seq[H]) iter.Seq[lo.Tuple8[A, B, C, D, E, F, G, H]]

Zip8 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/0XrQKOk-vw

Example
result := Zip8(values("hello"), values(2), values(true), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar} 4.2 plop false 42}]

func Zip9

func Zip9[A, B, C, D, E, F, G, H, I any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G], h iter.Seq[H], i iter.Seq[I]) iter.Seq[lo.Tuple9[A, B, C, D, E, F, G, H, I]]

Zip9 creates a sequence of grouped elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/1SmFJ5-zr

Example
result := Zip9(values("hello"), values(2), values(true), values(foo{bar: "bar"}), values(4.2), values("plop"), values(false), values(42), values("hello world"))
fmt.Printf("%v", slices.Collect(result))
Output:

[{hello 2 true {bar} 4.2 plop false 42 hello world}]

func ZipBy2

func ZipBy2[A, B, Out any](a iter.Seq[A], b iter.Seq[B], transform func(a A, b B) Out) iter.Seq[Out]

ZipBy2 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/2TnGK6-zs

func ZipBy3

func ZipBy3[A, B, C, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], transform func(a A, b B, c C) Out) iter.Seq[Out]

ZipBy3 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/3UoHL7-zt

func ZipBy4

func ZipBy4[A, B, C, D, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], transform func(a A, b B, c C, d D) Out) iter.Seq[Out]

ZipBy4 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/4VpIM8-zu

func ZipBy5

func ZipBy5[A, B, C, D, E, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], transform func(a A, b B, c C, d D, e E) Out) iter.Seq[Out]

ZipBy5 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/5WqJN9-zv

func ZipBy6

func ZipBy6[A, B, C, D, E, F, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], transform func(a A, b B, c C, d D, e E, f F) Out) iter.Seq[Out]

ZipBy6 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/6XrKO0-zw

func ZipBy7

func ZipBy7[A, B, C, D, E, F, G, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G], transform func(a A, b B, c C, d D, e E, f F, g G) Out) iter.Seq[Out]

ZipBy7 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/7YsLP1-zx

func ZipBy8

func ZipBy8[A, B, C, D, E, F, G, H, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G], h iter.Seq[H], transform func(a A, b B, c C, d D, e E, f F, g G, h H) Out) iter.Seq[Out]

ZipBy8 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/8isgTsyfL-t

func ZipBy9

func ZipBy9[A, B, C, D, E, F, G, H, I, Out any](a iter.Seq[A], b iter.Seq[B], c iter.Seq[C], d iter.Seq[D], e iter.Seq[E], f iter.Seq[F], g iter.Seq[G], h iter.Seq[H], i iter.Seq[I], transform func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) iter.Seq[Out]

ZipBy9 creates a sequence of transformed elements, the first of which contains the first elements of the given sequences, the second of which contains the second elements of the given sequences, and so on. When collections are different sizes, the Tuple attributes are filled with zero value. Play: https://go.dev/play/p/9jthUzgF-u

Types

This section is empty.

Jump to

Keyboard shortcuts

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