seq

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2025 License: MIT Imports: 6 Imported by: 5

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[E any](seq iter.Seq[E], elems ...E) iter.Seq[E]

Append appends elements to the end of a sequence.

Example
package main

import (
	"fmt"

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

func main() {
	initial := seq.Of(1, 2, 3)

	appended := seq.Append(initial, 4, 5, 6)

	result := seq.Collect(appended)

	fmt.Println(result)
}
Output:
[1 2 3 4 5 6]

func Chunk

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

Chunk splits the sequence into chunks of the given size.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5, 6)

	chunks := seq.Chunk(input, 3)

	result := seq.Collect(chunks)

	for _, chunk := range result {
		fmt.Println(seq.Collect(chunk))
	}
}
Output:
[1 2 3]
[4 5 6]

func Collect

func Collect[E any](seq iter.Seq[E]) []E

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

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.Of(1, 2, 3)

	result := seq.Collect(sequence)

	fmt.Println(result)
}
Output:
[1 2 3]

func Concat

func Concat[E any](sequences ...iter.Seq[E]) iter.Seq[E]

Concat concatenates multiple sequences into a single sequence.

Example
package main

import (
	"fmt"

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

func main() {
	seq1 := seq.Of(1, 2, 3)
	seq2 := seq.Of(4, 5, 6)

	concatenated := seq.Concat(seq1, seq2)

	result := seq.Collect(concatenated)

	fmt.Println(result)
}
Output:
[1 2 3 4 5 6]

func Contains

func Contains[E comparable](seq iter.Seq[E], 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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	contains := seq.Contains(input, 3)

	fmt.Println(contains)
}
Output:
true

func ContainsAll

func ContainsAll[E comparable](seq iter.Seq[E], 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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	containsAll := seq.ContainsAll(input, 2, 3)

	fmt.Println(containsAll)
}
Output:
true

func Count

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

Count returns the number of elements in the sequence.

func Cycle

func Cycle[E any](seq iter.Seq[E], count int) iter.Seq[E]

Cycle repeats the sequence count times.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3)

	cycled := seq.Cycle(input, 2)

	result := seq.Collect(cycled)

	fmt.Println(result)
}
Output:
[1 2 3 1 2 3]

func Distinct

func Distinct[E comparable](seq iter.Seq[E]) iter.Seq[E]

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

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 2, 3, 3, 3)

	distinct := seq.Distinct(input)

	result := seq.Collect(distinct)

	fmt.Println(result)
}
Output:
[1 2 3]

func Each

func Each[E any](seq iter.Seq[E], consumer Consumer[E]) iter.Seq[E]

Each is an alias for Tap.

Example
package main

import (
	"fmt"

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

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

	seq.Flush(sequence)

}
Output:
1
2
3

func Every

func Every[E any](seq iter.Seq[E], predicate Predicate[E]) bool

Every returns true if all elements satisfy the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(2, 4, 6, 8)

	every := seq.Every(input, func(v int) bool { return v%2 == 0 })

	fmt.Println(every)
}
Output:
true

func Exists

func Exists[E any](seq iter.Seq[E], predicate 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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	exists := seq.Exists(input, func(v int) bool { return v > 4 })

	fmt.Println(exists)
}
Output:
true

func Filter

func Filter[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

Filter returns a new sequence that contains only the elements that satisfy the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	filtered := seq.Filter(input, func(v int) bool {
		return v%2 == 0
	})

	result := seq.Collect(filtered)

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

func Find

func Find[E any](seq iter.Seq[E], predicate Predicate[E]) optional.Elem[E]

Find returns the first element that satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	found := seq.Find(input, func(v int) bool { return v > 3 })

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

func FindAll

func FindAll[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

FindAll returns all elements that satisfy the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	found := seq.FindAll(input, func(v int) bool { return v > 3 })

	result := seq.Collect(found)

	fmt.Println(result)
}
Output:
[4 5]

func FindLast

func FindLast[E any](seq iter.Seq[E], predicate Predicate[E]) optional.Elem[E]

FindLast returns the last element that satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	found := seq.FindLast(input, func(v int) bool { return v > 3 })

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

func FlatMap

func FlatMap[E any, R any](seq iter.Seq[E], mapper Mapper[E, iter.Seq[R]]) iter.Seq[R]

FlatMap applies a mapper function to each element of the sequence and flattens the result.

Example
package main

import (
	"fmt"
	"iter"

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

func main() {
	input := seq.Of(0, 3)

	flatMapped := seq.FlatMap(input, func(it int) iter.Seq[int] {
		return seq.Of[int](1+it, 2+it, 3+it)
	})

	result := seq.Collect(flatMapped)

	fmt.Println(result)
}
Output:
[1 2 3 4 5 6]

func Flatten

func Flatten[Seq iter.Seq[iter.Seq[E]], E any](seq Seq) iter.Seq[E]

Flatten flattens a sequence of sequences.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(seq.Of(1, 2), seq.Of(3, 4))

	flattened := seq.Flatten(input)

	result := seq.Collect(flattened)

	fmt.Println(result)
}
Output:
[1 2 3 4]

func Flush

func Flush[E any](seq iter.Seq[E])

Flush consumes all elements of the input sequence.

func Fold

func Fold[E any](seq iter.Seq[E], accumulator func(agg E, item E) E) optional.Elem[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"
)

func main() {
	input := seq.Of("a", "b", "c")

	sum := seq.Fold(input, func(agg, item string) string {
		return agg + item
	})

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

func FoldRight

func FoldRight[E any](seq iter.Seq[E], accumulator func(agg E, item E) E) optional.Elem[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"
)

func main() {
	input := seq.Of("a", "b", "c")

	sum := seq.FoldRight(input, func(agg, item string) string {
		return agg + item
	})

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

func ForEach

func ForEach[E any](seq iter.Seq[E], consumer Consumer[E])

ForEach applies consumer to each element of the input sequence.

Example
package main

import (
	"fmt"

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

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

}
Output:
1
2
3

func FromSlice

func FromSlice[Slice ~[]E, E any](slice Slice) iter.Seq[E]

FromSlice creates a new sequence from the given slice.

Example
package main

import (
	"fmt"

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

func main() {
	slice := []int{1, 2, 3}

	sequence := seq.FromSlice(slice)

	result := seq.Collect(sequence)

	fmt.Println(result)
}
Output:
[1 2 3]

func GroupBy

func GroupBy[E any, K comparable](seq iter.Seq[E], by Mapper[E, K]) iter.Seq2[K, iter.Seq[E]]

GroupBy groups the sequence by the given key.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5, 6)

	groups := seq.GroupBy(input, func(v int) int {
		return v % 2
	})

	for k, v := range groups {
		fmt.Printf("%d: %v\n", k, seq.Collect(v))
	}

}
Output:
1: [1 3 5]
0: [2 4 6]

func IsEmpty

func IsEmpty[E any](seq iter.Seq[E]) bool

IsEmpty returns true if the sequence is empty.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3)

	fmt.Println(seq.IsEmpty(input))
}
Output:
false

func IsNotEmpty

func IsNotEmpty[E any](seq iter.Seq[E]) bool

IsNotEmpty returns true if the sequence is not empty.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3)

	isNotEmpty := seq.IsNotEmpty(input)

	fmt.Println(isNotEmpty)
}
Output:
true

func Limit

func Limit[E any](seq iter.Seq[E], n int) iter.Seq[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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	limited := seq.Limit(input, 2)

	result := seq.Collect(limited)

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

func Map

func Map[E any, R any](seq iter.Seq[E], mapper Mapper[E, R]) iter.Seq[R]

Map applies a mapper function to each element of the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3)

	mapped := seq.Map(input, func(v int) string {
		return fmt.Sprintf("Number_%d", v)
	})

	result := seq.Collect(mapped)

	fmt.Println(result)
}
Output:
[Number_1 Number_2 Number_3]

func Max

func Max[E types.Ordered](seq iter.Seq[E]) optional.Elem[E]

Max returns the maximum element in the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(2, 3, 1, 5, 4)

	maxVal := seq.Max(input)

	fmt.Println(maxVal.MustGet())
}
Output:
5

func Min

func Min[E types.Ordered](seq iter.Seq[E]) optional.Elem[E]

Min returns the minimum element in the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(2, 3, 1, 5, 4)

	maxVal := seq.Min(input)

	fmt.Println(maxVal.MustGet())
}
Output:
1

func None

func None[E any](seq iter.Seq[E], predicate Predicate[E]) bool

None returns true if no element satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	none := seq.None(input, func(v int) bool { return v > 5 })

	fmt.Println(none)
}
Output:
true

func NotContains

func NotContains[E comparable](seq iter.Seq[E], 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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	contains := seq.NotContains(input, 3)

	fmt.Println(contains)
}
Output:
false

func Of

func Of[E any](elems ...E) iter.Seq[E]

Of creates a new sequence from the given elements.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.Of(1, 2, 3)

	result := seq.Collect(sequence)

	fmt.Println(result)
}
Output:
[1 2 3]

func Offset

func Offset[E any](seq iter.Seq[E], n int) iter.Seq[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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	skipped := seq.Offset(input, 2)

	result := seq.Collect(skipped)

	fmt.Printf("%v\n", result)
}
Output:
[3 4 5]

func Partition

func Partition[E any](seq iter.Seq[E], 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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5, 6)

	partitions := seq.Partition(input, 2)

	result := seq.Collect(partitions)

	for _, partition := range result {
		fmt.Println(seq.Collect(partition))
	}

}
Output:
[1 2]
[3 4]
[5 6]

func PartitionBy

func PartitionBy[E any, K comparable](seq iter.Seq[E], by Mapper[E, K]) iter.Seq[iter.Seq[E]]

PartitionBy splits the sequence into chunks based on the given key.

Example
package main

import (
	"fmt"
	"iter"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5, 6)

	partitions := seq.PartitionBy(input, func(v int) int {
		return v % 2
	})

	// Notice that the order of the partitions is not guaranteed
	sorted := seq.SortBy(partitions, func(p iter.Seq[int]) int {
		next, stop := iter.Pull(p)
		defer stop()
		v, _ := next()
		return v
	})

	for partition := range sorted {
		fmt.Println(seq.Collect(partition))
	}
}
Output:
[1 3 5]
[2 4 6]

func Prepend

func Prepend[E any](seq iter.Seq[E], elems ...E) iter.Seq[E]

Prepend prepends elements to the beginning of a sequence.

Example
package main

import (
	"fmt"

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

func main() {
	initial := seq.Of(4, 5, 6)

	prepended := seq.Prepend(initial, 1, 2, 3)

	result := seq.Collect(prepended)

	fmt.Println(result)
}
Output:
[1 2 3 4 5 6]

func Range

func Range[E types.Integer](start, end E) iter.Seq[E]

Range returns a sequence that yields integers from `start` to `end`.

Example
package main

import (
	"fmt"

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

func main() {
	ranged := seq.Range(0, 5)

	result := seq.Collect(ranged)

	fmt.Println(result)
}
Output:
[0 1 2 3 4]

func RangeTo

func RangeTo[E types.Integer](end E) iter.Seq[E]

RangeTo returns a sequence that yields integers from 0 to `end`.

Example
package main

import (
	"fmt"

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

func main() {
	ranged := seq.RangeTo(5)

	result := seq.Collect(ranged)

	fmt.Println(result)
}
Output:
[0 1 2 3 4]

func RangeWithStep

func RangeWithStep[E types.Integer](start, end, step E) iter.Seq[E]

RangeWithStep returns a sequence that yields integers from `start` to `end` with `step`.

Example
package main

import (
	"fmt"

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

func main() {
	ranged := seq.RangeWithStep(0, 10, 2)

	result := seq.Collect(ranged)

	fmt.Println(result)
}
Output:
[0 2 4 6 8]

func Reduce

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

Reduce 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"
)

func main() {
	input := seq.Of("a", "b", "c")

	concat := seq.Reduce(input, func(agg, item string) string {
		return agg + item
	}, "")

	fmt.Println(concat)
}
Output:
abc

func ReduceRight

func ReduceRight[E any, R any](seq iter.Seq[E], accumulator func(agg R, item E) R, initial R) R

ReduceRight 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"
)

func main() {
	input := seq.Of("a", "b", "c")

	concat := seq.ReduceRight(input, func(agg, item string) string {
		return agg + item
	}, "")

	fmt.Println(concat)
}
Output:
cba

func Repeat

func Repeat[E any](elem E, count int) iter.Seq[E]

Repeat returns a sequence that yields the same element `count` times.

Example
package main

import (
	"fmt"

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

func main() {
	repeated := seq.Repeat("hello", 3)

	result := seq.Collect(repeated)

	fmt.Println(result)
}
Output:
[hello hello hello]

func Reverse

func Reverse[E any](seq iter.Seq[E]) iter.Seq[E]

Reverse creates a new sequence that iterates over the elements of the given sequence in reverse order.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.Of(1, 2, 3)

	reversed := seq.Reverse(sequence)

	result := seq.Collect(reversed)
	fmt.Println(result)
}
Output:
[3 2 1]

func Select

func Select[E any, R any](seq iter.Seq[E], mapper Mapper[E, R]) iter.Seq[R]

Select applies a mapper function to each element of the sequence. SQL-like alias for Map

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3)

	mapped := seq.Select(input, func(v int) string {
		return fmt.Sprintf("Number_%d", v)
	})

	result := seq.Collect(mapped)

	fmt.Println(result)
}
Output:
[Number_1 Number_2 Number_3]

func Skip

func Skip[E any](seq iter.Seq[E], n int) iter.Seq[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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	skipped := seq.Skip(input, 2)

	result := seq.Collect(skipped)

	fmt.Printf("%v\n", result)
}
Output:
[3 4 5]

func SkipUntil

func SkipUntil[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

SkipUntil returns a new sequence that skips elements until the predicate is true.

func SkipWhile

func SkipWhile[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

SkipWhile returns a new sequence that skips elements while the predicate is true.

func Sort

func Sort[E types.Ordered](seq iter.Seq[E]) iter.Seq[E]

Sort sorts the elements of a sequence in ascending order.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)

	sorted := seq.Sort(input)

	result := seq.Collect(sorted)
	fmt.Println(result)
}
Output:
[1 1 2 3 3 4 5 5 5 6 9]

func SortBy

func SortBy[E any, K types.Ordered](seq iter.Seq[E], keyFn Mapper[E, K]) iter.Seq[E]

SortBy sorts the elements of a sequence in ascending order by the key returned by keyFn.

Example
package main

import (
	"fmt"

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

func main() {
	type Person struct {
		Name string
		Age  int
	}
	input := seq.Of(
		Person{"Alice", 30},
		Person{"Bob", 25},
		Person{"Charlie", 35},
	)

	sorted := seq.SortBy(input, func(p Person) int {
		return p.Age
	})

	for p := range sorted {
		fmt.Printf("%s (%d)\n", p.Name, p.Age)
	}
}
Output:
Bob (25)
Alice (30)
Charlie (35)

func SortComparing

func SortComparing[E any](seq iter.Seq[E], cmp func(a, b E) int) iter.Seq[E]

SortComparing sorts the elements of a sequence in ascending order using the cmp function.

Example
package main

import (
	"fmt"

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

func main() {
	type Person struct {
		Name string
		Age  int
	}
	input := seq.Of(
		Person{"Alice", 30},
		Person{"Bob", 25},
		Person{"Charlie", 35},
	)

	sorted := seq.SortComparing(input, func(a, b Person) int {
		return a.Age - b.Age
	})

	for p := range sorted {
		fmt.Printf("%s (%d)\n", p.Name, p.Age)
	}
}
Output:
Bob (25)
Alice (30)
Charlie (35)

func Take

func Take[E any](seq iter.Seq[E], n int) iter.Seq[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"
)

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	taken := seq.Take(input, 3)

	result := seq.Collect(taken)

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

func TakeUntil

func TakeUntil[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

TakeUntil returns a new sequence that contains elements until the predicate is true.

func TakeWhile

func TakeWhile[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

TakeWhile returns a new sequence that contains elements while the predicate is true.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 2, 1)

	taken := seq.TakeWhile(input, func(v int) bool {
		return v < 3
	})

	result := seq.Collect(taken)

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

func Tap

func Tap[E any](seq iter.Seq[E], consumer func(E)) iter.Seq[E]

Tap returns a sequence that applies the given consumer to each element of the input sequence and pass it further.

Example
package main

import (
	"fmt"

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

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

	seq.Flush(sequence)

}
Output:
1
2
3

func Tick

func Tick(d time.Duration) iter.Seq[time.Time]

Tick returns a sequence that yields the current time every duration.

Example
package main

import (
	"fmt"
	"time"

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

func main() {
	ticker := seq.Tick(1 * time.Millisecond)

	ticker = seq.Take(ticker, 5)

	ticker = seq.Tap(ticker, func(v time.Time) {
		fmt.Println(v.Format("15:04:05.000"))
	})

	seq.Flush(ticker)

	// Example Output:
	// 00:00:00.000
	// 00:00:00.001
	// 00:00:00.002
	// 00:00:00.003
	// 00:00:00.004
}

func ToSlice

func ToSlice[Slice ~[]E, E any](seq iter.Seq[E], slice Slice) Slice

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

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.Of(1, 2, 3)

	slice := make([]int, 0, 3)
	result := seq.ToSlice(sequence, slice)

	fmt.Println(result)
}
Output:
[1 2 3]

func Union

func Union[E types.Comparable](seq1 iter.Seq[E], seq2 iter.Seq[E]) iter.Seq[E]

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

Example
package main

import (
	"fmt"

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

func main() {
	seq1 := seq.Of(1, 2, 3)
	seq2 := seq.Of(3, 4, 5)

	union := seq.Union(seq1, seq2)

	result := seq.Collect(union)

	fmt.Println(result)
}
Output:
[1 2 3 4 5]

func UnionAll

func UnionAll[E any](seq1 iter.Seq[E], seq2 iter.Seq[E]) iter.Seq[E]

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

Example
package main

import (
	"fmt"

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

func main() {
	seq1 := seq.Of(1, 2, 3)
	seq2 := seq.Of(3, 4, 5)

	unionAll := seq.UnionAll(seq1, seq2)

	result := seq.Collect(unionAll)

	fmt.Println(result)
}
Output:
[1 2 3 3 4 5]

func Uniq

func Uniq[E comparable](seq iter.Seq[E]) iter.Seq[E]

Uniq returns a sequence with only unique elements.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 2, 3, 3, 3)

	unique := seq.Uniq(input)

	result := seq.Collect(unique)

	fmt.Println(result)
}
Output:
[1 2 3]

func UniqBy

func UniqBy[E any, K comparable](seq iter.Seq[E], mapper Mapper[E, K]) iter.Seq[E]

UniqBy returns a sequence with only unique elements based on a key.

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of("apple", "banana", "apricot", "blueberry")

	uniqueBy := seq.UniqBy(input, func(v string) string {
		return v[:1] // unique by first letter
	})

	result := seq.Collect(uniqueBy)

	fmt.Println(result)
}
Output:
[apple banana]

func Where

func Where[E any](seq iter.Seq[E], predicate Predicate[E]) iter.Seq[E]

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

Example
package main

import (
	"fmt"

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

func main() {
	input := seq.Of(1, 2, 3, 4, 5)

	filtered := seq.Where(input, func(v int) bool {
		return v%2 == 0
	})

	result := seq.Collect(filtered)

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

func Zip

func Zip[E any, R any](seq1 iter.Seq[E], seq2 iter.Seq[R]) iter.Seq2[E, R]

Zip combines two sequences into a iter.Seq2.

Example
package main

import (
	"fmt"

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

func main() {
	seq1 := seq.Of(1, 2, 3)
	seq2 := seq.Of("a", "b", "c")

	zipped := seq.Zip(seq1, seq2)

	for k, v := range zipped {
		fmt.Printf("%d: %s\n", k, v)
	}
}
Output:
1: a
2: b
3: c

Types

type Consumer

type Consumer[E any] = func(E)

Consumer is a function that consumes an element of a sequence.

type Mapper

type Mapper[T any, R any] = func(T) R

Mapper is a function that maps a value of type T to a value of type R.

type Predicate

type Predicate[E any] = Mapper[E, bool]

Predicate is a function that takes an element and returns a boolean.

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"
)

func main() {
	sequence := seq.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"
)

func main() {
	seq1 := seq.AsSequence(seq.Of(1, 2))
	seq2 := seq.AsSequence(seq.Of(3, 4))
	concatenated := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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 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"
)

func main() {
	sequence := seq.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 Predicate[E]) bool

Every returns true if all elements satisfy the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.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 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"
)

func main() {
	sequence := seq.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 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"
)

func main() {
	sequence := seq.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 Predicate[E]) optional.Elem[E]

Find returns the first element that satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.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 Predicate[E]) Sequence[E]

FindAll returns all elements that satisfy the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.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 Predicate[E]) optional.Elem[E]

FindLast returns the last element that satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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.Elem[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"
)

func main() {
	sequence := seq.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.Elem[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"
)

func main() {
	sequence := seq.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 Consumer[E])

ForEach calls the consumer for each element of the sequence.

Example
package main

import (
	"fmt"

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

func main() {
	seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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 Predicate[E]) bool

None returns true if no element satisfies the predicate.

Example
package main

import (
	"fmt"

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

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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]) Repeat

func (s Sequence[E]) Repeat(count int) Sequence[E]

Repeat repeats the sequence count times.

Example
package main

import (
	"fmt"

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

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

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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	sequence := seq.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"
)

func main() {
	seq1 := seq.AsSequence(seq.Of(1, 2, 3))
	seq2 := seq.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"
)

func main() {
	seq1 := seq.AsSequence(seq.Of(1, 2, 3))
	seq2 := seq.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"
)

func main() {
	sequence := seq.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 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"
)

func main() {
	sequence := seq.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