xseq

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sequence

type Sequence[E comparable] struct {
	// contains filtered or unexported fields
}

Sequence is a monad representing a sequence of elements.

func AsSequence

func AsSequence[E comparable](seq iter.Seq[E]) Sequence[E]

AsSequence wraps an iter.Seq to provide a possibility to pipe several method calls.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	result := sequence.Collect()
	fmt.Println(result)
}
Output:

[1 2 3]

func ConcatSequences

func ConcatSequences[E comparable](sequences ...Sequence[E]) Sequence[E]

ConcatSequences concatenates multiple sequences into a single sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	seq1 := xseq.AsSequence(seq.Of(1, 2))
	seq2 := xseq.AsSequence(seq.Of(3, 4))
	concatenated := xseq.ConcatSequences(seq1, seq2)
	result := concatenated.Collect()
	fmt.Println(result)
}
Output:

[1 2 3 4]

func (Sequence[E]) Append

func (s Sequence[E]) Append(elems ...E) Sequence[E]

Append appends elements to the end of a sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	appended := sequence.Append(4, 5)
	result := appended.Collect()
	fmt.Println(result)
}
Output:

[1 2 3 4 5]

func (Sequence[E]) Collect

func (s Sequence[E]) Collect() []E

Collect collects the elements of the sequence into a slice.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	result := sequence.Collect()
	fmt.Println(result)
}
Output:

[1 2 3]

func (Sequence[E]) Contains

func (s Sequence[E]) Contains(elem E) bool

Contains returns true if the element is in the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	contains := sequence.Contains(3)
	fmt.Println(contains)
}
Output:

true

func (Sequence[E]) ContainsAll

func (s Sequence[E]) ContainsAll(elements ...E) bool

ContainsAll returns true if all elements are in the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	containsAll := sequence.ContainsAll(2, 3, 4)
	fmt.Println(containsAll)
}
Output:

true

func (Sequence[E]) Count

func (s Sequence[E]) Count() int

Count returns the number of elements in the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	count := sequence.Count()
	fmt.Println(count)
}
Output:

3

func (Sequence[E]) Distinct

func (s Sequence[E]) Distinct() Sequence[E]

Distinct returns a new sequence with only unique elements. SQL-like alias for Uniq

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 2, 3, 3, 3))
	distinct := sequence.Distinct()
	result := distinct.Collect()
	fmt.Println(result)
}
Output:

[1 2 3]

func (Sequence[E]) Each

func (s Sequence[E]) Each(consumer seq.Consumer[E]) Sequence[E]

Each returns a new sequence that calls the consumer for each element of the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3)).Each(func(v int) {
		fmt.Println(v)
	})
	sequence.Flush()
}
Output:

1
2
3

func (Sequence[E]) Every

func (s Sequence[E]) Every(predicate seq.Predicate[E]) bool

Every returns true if all elements satisfy the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	every := sequence.Every(func(v int) bool {
		return v > 0
	})
	fmt.Println(every)
}
Output:

true

func (Sequence[E]) Exists

func (s Sequence[E]) Exists(predicate seq.Predicate[E]) bool

Exists returns true if there is at least one element that satisfies the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	exists := sequence.Exists(func(v int) bool {
		return v > 3
	})
	fmt.Println(exists)
}
Output:

true

func (Sequence[E]) Filter

func (s Sequence[E]) Filter(predicate seq.Predicate[E]) Sequence[E]

Filter returns a new sequence with elements that satisfy the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	filtered := sequence.Filter(func(v int) bool {
		return v%2 == 0
	})
	result := filtered.Collect()
	fmt.Println(result)
}
Output:

[2 4]

func (Sequence[E]) Find

func (s Sequence[E]) Find(predicate seq.Predicate[E]) optional.Value[E]

Find returns the first element that satisfies the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	found := sequence.Find(func(v int) bool {
		return v > 3
	})

	fmt.Println(found.MustGet())
}
Output:

4

func (Sequence[E]) FindAll

func (s Sequence[E]) FindAll(predicate seq.Predicate[E]) Sequence[E]

FindAll returns all elements that satisfy the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	foundAll := sequence.FindAll(func(v int) bool {
		return v > 3
	})
	result := foundAll.Collect()
	fmt.Println(result)
}
Output:

[4 5]

func (Sequence[E]) FindLast

func (s Sequence[E]) FindLast(predicate seq.Predicate[E]) optional.Value[E]

FindLast returns the last element that satisfies the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	found := sequence.FindLast(func(v int) bool {
		return v > 3
	})

	fmt.Println(found.MustGet())
}
Output:

5

func (Sequence[E]) Flush

func (s Sequence[E]) Flush()

Flush consumes all elements of the input sequence.

Example
package main

import (
	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	sequence.Flush()
	// No output expected
}

func (Sequence[E]) Fold

func (s Sequence[E]) Fold(accumulator func(agg E, item E) E) optional.Value[E]

Fold applies a function against an accumulator and each element in the sequence (from left to right) to reduce it to a single value.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	sum := sequence.Fold(func(agg, item int) int {
		return agg + item
	})

	fmt.Println(sum.MustGet())
}
Output:

15

func (Sequence[E]) FoldRight

func (s Sequence[E]) FoldRight(accumulator func(agg E, item E) E) optional.Value[E]

FoldRight applies a function against an accumulator and each element in the sequence (from right to left) to reduce it to a single value.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of("a", "b", "c"))
	concat := sequence.FoldRight(func(agg, item string) string {
		return agg + item
	})

	fmt.Println(concat.MustGet())
}
Output:

cba

func (Sequence[E]) ForEach

func (s Sequence[E]) ForEach(consumer seq.Consumer[E])

ForEach calls the consumer for each element of the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	xseq.AsSequence(seq.Of(1, 2, 3)).ForEach(func(v int) {
		fmt.Println(v)
	})
}
Output:

1
2
3

func (Sequence[E]) IsEmpty

func (s Sequence[E]) IsEmpty() bool

IsEmpty returns true if the sequence is empty.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of[int]())
	isEmpty := sequence.IsEmpty()
	fmt.Println(isEmpty)
}
Output:

true

func (Sequence[E]) IsNotEmpty

func (s Sequence[E]) IsNotEmpty() bool

IsNotEmpty returns true if the sequence is not empty.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	isNotEmpty := sequence.IsNotEmpty()
	fmt.Println(isNotEmpty)
}
Output:

true

func (Sequence[E]) Limit

func (s Sequence[E]) Limit(n int) Sequence[E]

Limit returns a new sequence that contains only the first n elements of the given sequence. SQL-like alias for Take

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	limited := sequence.Limit(2)
	result := limited.Collect()
	fmt.Println(result)
}
Output:

[1 2]

func (Sequence[E]) None

func (s Sequence[E]) None(predicate seq.Predicate[E]) bool

None returns true if no element satisfies the predicate.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	none := sequence.None(func(v int) bool {
		return v > 5
	})
	fmt.Println(none)
}
Output:

true

func (Sequence[E]) NotContains

func (s Sequence[E]) NotContains(elem E) bool

NotContains returns true if the element is not in the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	notContains := sequence.NotContains(6)
	fmt.Println(notContains)
}
Output:

true

func (Sequence[E]) Offset

func (s Sequence[E]) Offset(n int) Sequence[E]

Offset returns a new sequence that skips the first n elements of the given sequence. SQL-like alias for Skip

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	offset := sequence.Offset(2)
	result := offset.Collect()
	fmt.Println(result)
}
Output:

[3 4 5]

func (Sequence[E]) Partition

func (s Sequence[E]) Partition(size int) iter.Seq[iter.Seq[E]]

Partition splits the sequence into chunks of the given size.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	partitions := sequence.Partition(2)
	for partition := range partitions {
		fmt.Println(seq.Collect(partition))
	}
}
Output:

[1 2]
[3 4]
[5]

func (Sequence[E]) Prepend

func (s Sequence[E]) Prepend(elems ...E) Sequence[E]

Prepend prepends elements to the beginning of a sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(3, 4, 5))
	prepended := sequence.Prepend(1, 2)
	result := prepended.Collect()
	fmt.Println(result)
}
Output:

[1 2 3 4 5]

func (Sequence[E]) Reverse

func (s Sequence[E]) Reverse() Sequence[E]

Reverse returns a new sequence with elements in reverse order.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	reversed := sequence.Reverse()
	result := reversed.Collect()
	fmt.Println(result)
}
Output:

[3 2 1]

func (Sequence[E]) Skip

func (s Sequence[E]) Skip(n int) Sequence[E]

Skip returns a new sequence that skips the first n elements of the given sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	skipped := sequence.Skip(2)
	result := skipped.Collect()
	fmt.Println(result)
}
Output:

[3 4 5]

func (Sequence[E]) Take

func (s Sequence[E]) Take(n int) Sequence[E]

Take returns a new sequence that contains only the first n elements of the given sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	taken := sequence.Take(3)
	result := taken.Collect()
	fmt.Println(result)
}
Output:

[1 2 3]

func (Sequence[E]) Tap

func (s Sequence[E]) Tap(consumer func(E)) Sequence[E]

Tap returns a new sequence that calls the consumer for each element of the sequence.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3)).Tap(func(v int) {
		fmt.Println(v)
	})
	sequence.Flush()
}
Output:

1
2
3

func (Sequence[E]) ToSlice

func (s Sequence[E]) ToSlice(slice []E) []E

ToSlice collects the elements of the sequence into a given slice.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3))
	var slice []int
	result := sequence.ToSlice(slice)
	fmt.Println(result)
}
Output:

[1 2 3]

func (Sequence[E]) Union

func (s Sequence[E]) Union(other Sequence[E]) Sequence[E]

Union returns a new sequence that contains all distinct elements from both input sequences.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	seq1 := xseq.AsSequence(seq.Of(1, 2, 3))
	seq2 := xseq.AsSequence(seq.Of(3, 4, 5))
	union := seq1.Union(seq2)
	result := union.Collect()
	fmt.Println(result)
}
Output:

[1 2 3 4 5]

func (Sequence[E]) UnionAll

func (s Sequence[E]) UnionAll(other Sequence[E]) Sequence[E]

UnionAll returns a new sequence that contains all elements from both input sequences.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	seq1 := xseq.AsSequence(seq.Of(1, 2, 3))
	seq2 := xseq.AsSequence(seq.Of(3, 4, 5))
	unionAll := seq1.UnionAll(seq2)
	result := unionAll.Collect()
	fmt.Println(result)
}
Output:

[1 2 3 3 4 5]

func (Sequence[E]) Uniq

func (s Sequence[E]) Uniq() Sequence[E]

Uniq returns a new sequence with only unique elements.

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 2, 3, 3, 3))
	unique := sequence.Uniq()
	result := unique.Collect()
	fmt.Println(result)
}
Output:

[1 2 3]

func (Sequence[E]) Where

func (s Sequence[E]) Where(predicate seq.Predicate[E]) Sequence[E]

Where returns a new sequence with elements that satisfy the predicate. SQL-like alias for Filter

Example
package main

import (
	"fmt"

	"github.com/go-softwarelab/common/pkg/seq"
	"github.com/go-softwarelab/common/x/xseq"
)

func main() {
	sequence := xseq.AsSequence(seq.Of(1, 2, 3, 4, 5))
	filtered := sequence.Where(func(v int) bool {
		return v%2 == 0
	})
	result := filtered.Collect()
	fmt.Println(result)
}
Output:

[2 4]

Jump to

Keyboard shortcuts

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