Documentation ¶
Overview ¶
Package seq provides functions for handling the iter.Seq interface in Go 1.23.
This package aims to simplify iter.Seq operations, such as range, sort, filter, and more.
Index ¶
- func AllMatch[V any](input iter.Seq[V], predicate func(V) bool) bool
- func AllMatch2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) bool
- func AnyMatch[V any](input iter.Seq[V], predicate func(V) bool) bool
- func AnyMatch2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) bool
- func Append[V any](input iter.Seq[V], items ...V) iter.Seq[V]
- func Append2[K, V any](input iter.Seq2[K, V], items ...Tuple[K, V]) iter.Seq2[K, V]
- func CenteredMovingAvg[V stats.Number](input iter.Seq[V], size int) iter.Seq[V]
- func Chan[V any](input chan V) iter.Seq[V]
- func Chan2[K, V any](input chan Tuple[K, V]) iter.Seq2[K, V]
- func Chunk[V any](input iter.Seq[V], num int) []iter.Seq[V]
- func Chunk2[K, V any](input iter.Seq2[K, V], num int) []iter.Seq2[K, V]
- func Clone[V any](input iter.Seq[V], num int) []iter.Seq[V]
- func Clone2[K, V any](input iter.Seq2[K, V], num int) []iter.Seq2[K, V]
- func Collect[V any](input iter.Seq[V], size int) []V
- func Collect2[K comparable, V any](input iter.Seq2[K, V], size int) map[K]V
- func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]
- func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func Count[V any](input iter.Seq[V]) int
- func Count2[K, V any](input iter.Seq2[K, V]) int
- func Distinct[V comparable](input iter.Seq[V]) iter.Seq[V]
- func Distinct2[K comparable, V any](input iter.Seq2[K, V]) iter.Seq2[K, V]
- func Distribute[V any](input iter.Seq[V], num int) []iter.Seq[V]
- func Distribute2[K, V any](input iter.Seq2[K, V], num int) []iter.Seq2[K, V]
- func DropWhile[V any](input iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func DropWhile2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]
- func Each[V any](input iter.Seq[V], yield func(V) bool)
- func Each2[K, V any](input iter.Seq2[K, V], yield func(K, V) bool)
- func Emit[V any](input iter.Seq[V])
- func Emit2[K, V any](input iter.Seq2[K, V])
- func Filter[V any](input iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func Filter2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]
- func First[V any](input iter.Seq[V]) (V, bool)
- func First2[K, V any](input iter.Seq2[K, V]) (K, V, bool)
- func FlatMap[S ~[]V, V any](input iter.Seq[S]) iter.Seq[V]
- func Join[V any](input iter.Seq[V], delimiter string) string
- func Keys[K, V any](input iter.Seq2[K, V]) iter.Seq[K]
- func Last[V any](input iter.Seq[V]) (V, bool)
- func Last2[K, V any](input iter.Seq2[K, V]) (K, V, bool)
- func Limit[V any](input iter.Seq[V], limit int) iter.Seq[V]
- func Limit2[K, V any](input iter.Seq2[K, V], limit int) iter.Seq2[K, V]
- func Map[I, O any](input iter.Seq[I], mapper func(I) O) iter.Seq[O]
- func Map2[IK, IV, OK, OV any](input iter.Seq2[IK, IV], mapper func(IK, IV) (OK, OV)) iter.Seq2[OK, OV]
- func Max[V cmp.Ordered](input iter.Seq[V]) (V, bool)
- func Max2[K cmp.Ordered, V any](input iter.Seq2[K, V]) (K, V, bool)
- func Merge[V any](seqs ...iter.Seq[V]) iter.Seq[V]
- func Merge2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]
- func Min[V cmp.Ordered](input iter.Seq[V]) (V, bool)
- func Min2[K cmp.Ordered, V any](input iter.Seq2[K, V]) (K, V, bool)
- func MovingAvg[V stats.Number](input iter.Seq[V], size int) iter.Seq[V]
- func NoneMatch[V any](input iter.Seq[V], predicate func(V) bool) bool
- func NoneMatch2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) bool
- func Partition[V any](input iter.Seq[V], predicate func(V) bool) (iter.Seq[V], iter.Seq[V])
- func Partition2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) (iter.Seq2[K, V], iter.Seq2[K, V])
- func Peek[V any](input iter.Seq[V], action func(V)) iter.Seq[V]
- func Peek2[K, V any](input iter.Seq2[K, V], action func(K, V)) iter.Seq2[K, V]
- func Prepend[V any](input iter.Seq[V], items ...V) iter.Seq[V]
- func Prepend2[K, V any](input iter.Seq2[K, V], items ...Tuple[K, V]) iter.Seq2[K, V]
- func Range(args ...int) iter.Seq[int]
- func Range2(args ...int) iter.Seq2[int, int]
- func Reduce[V any](input iter.Seq[V], accumulator func(V, V) V) (V, bool)
- func Reduce2[K, V any](input iter.Seq2[K, V], accumulator func(K, V, K, V) (K, V)) (K, V, bool)
- func Repeat[V any](input iter.Seq[V], count int) iter.Seq[V]
- func Repeat2[K, V any](input iter.Seq2[K, V], count int) iter.Seq2[K, V]
- func Reverse[V any](input iter.Seq[V]) iter.Seq[V]
- func Reverse2[K, V any](input iter.Seq2[K, V]) iter.Seq2[K, V]
- func Send[V any](input iter.Seq[V], output chan<- V)
- func Send2[K, V any](input iter.Seq2[K, V], output chan Tuple[K, V])
- func Seq[V any](values ...V) iter.Seq[V]
- func Seq2[K, V any](values ...Tuple[K, V]) iter.Seq2[K, V]
- func Shuffle[V any](input iter.Seq[V]) iter.Seq[V]
- func Shuffle2[K, V any](input iter.Seq2[K, V]) iter.Seq2[K, V]
- func Skip[V any](input iter.Seq[V], offset int) iter.Seq[V]
- func Skip2[K, V any](input iter.Seq2[K, V], offset int) iter.Seq2[K, V]
- func Sorted[V cmp.Ordered](input iter.Seq[V]) iter.Seq[V]
- func Sorted2[K cmp.Ordered, V any](input iter.Seq2[K, V]) iter.Seq2[K, V]
- func SortedFunc[V any](input iter.Seq[V], cmp func(V, V) int) iter.Seq[V]
- func SortedFunc2[K, V any](input iter.Seq2[K, V], cmp func(Tuple[K, V], Tuple[K, V]) int) iter.Seq2[K, V]
- func SortedStableFunc[V any](input iter.Seq[V], cmp func(V, V) int) iter.Seq[V]
- func SortedStableFunc2[K, V any](input iter.Seq2[K, V], cmp func(Tuple[K, V], Tuple[K, V]) int) iter.Seq2[K, V]
- func Sum[V cmp.Ordered](input iter.Seq[V]) V
- func TakeWhile[V any](input iter.Seq[V], predicate func(V) bool) iter.Seq[V]
- func TakeWhile2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq2[K, V]
- func ToChans[V any](input iter.Seq[V], size int) []chan V
- func ToChans2[K, V any](input iter.Seq2[K, V], size int) []chan Tuple[K, V]
- func Values[K, V any](input iter.Seq2[K, V]) iter.Seq[V]
- func Window[V any](input iter.Seq[V], size int) iter.Seq[[]V]
- func Window2[K, V any](input iter.Seq2[K, V], size int) iter.Seq[[]Tuple[K, V]]
- func Zip[K, V any](keys iter.Seq[K], values iter.Seq[V]) iter.Seq2[K, V]
- type Tuple
Examples ¶
- AllMatch
- AllMatch2
- AnyMatch
- AnyMatch2
- Append
- Append2
- CenteredMovingAvg
- Chan
- Chan2
- Chunk
- Chunk2
- Clone
- Clone2
- Collect
- Collect2
- Concat
- Concat2
- Count
- Count2
- Distinct
- Distinct2
- Distribute
- Distribute2
- DropWhile
- DropWhile2
- Each
- Each2
- Emit
- Emit2
- Filter
- Filter2
- First
- First2
- FlatMap
- Join
- Keys
- Last
- Last2
- Limit
- Limit2
- Map
- Map2
- Max
- Max2
- Merge
- Merge2
- Min
- Min2
- MovingAvg
- NoneMatch
- NoneMatch2
- Partition
- Partition2
- Peek
- Peek2
- Prepend
- Prepend2
- Range (One)
- Range (OneNegative)
- Range (Repeat)
- Range (StepDecrease)
- Range (StepWrong)
- Range (Three)
- Range (Two)
- Range (TwoDecrease)
- Range (TwoEqual)
- Range (TwoNegative)
- Range (TwoNegativeIncremental)
- Range (Zero)
- Range2 (One)
- Range2 (OneNegative)
- Range2 (Repeat)
- Range2 (StepDecrease)
- Range2 (StepWrong)
- Range2 (Three)
- Range2 (Two)
- Range2 (TwoDecrease)
- Range2 (TwoEqual)
- Range2 (TwoNegative)
- Range2 (TwoNegativeIncremental)
- Range2 (Zero)
- Reduce
- Reduce2
- Repeat
- Repeat2
- Reverse
- Reverse2
- Send
- Send2
- Seq
- Seq2
- Shuffle
- Shuffle2
- Skip
- Skip2
- Sorted
- Sorted2
- SortedFunc
- SortedFunc2
- SortedStableFunc
- SortedStableFunc2
- Sum
- T
- TakeWhile
- TakeWhile2
- ToChans
- ToChans2
- Tuples
- Values
- Window
- Window2
- Zip
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllMatch ¶
AllMatch checks if all elements in the input sequence satisfy the given predicate.
If all elements satisfy the predicate, the function returns true; otherwise, it returns false.
Play: https://go.dev/play/p/WjtXihB0DTZ
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.AllMatch(seq.Range(3), func(num int) bool { return num > 1 })) fmt.Println(seq.AllMatch(seq.Range(3), func(num int) bool { return num < 3 })) fmt.Println(seq.AllMatch(seq.Range(0), func(num int) bool { return num < 3 })) }
Output: false true false
func AllMatch2 ¶ added in v0.0.5
AllMatch2 checks if all (key, value) pairs in the sequence satisfy a predicate.
It returns true if all pairs satisfy the predicate.
Play: https://go.dev/play/p/SVvKOkEBFsR
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.AllMatch2(seq.Range2(3), func(key, _ int) bool { return key > 1 })) fmt.Println(seq.AllMatch2(seq.Range2(3), func(key, _ int) bool { return key < 3 })) fmt.Println(seq.AllMatch2(seq.Range2(0), func(key, _ int) bool { return key < 3 })) }
Output: false true false
func AnyMatch ¶
AnyMatch checks if any element in the input sequence satisfies the given predicate.
If any element satisfies the predicate, the function returns true; otherwise, it returns false.
Play: https://go.dev/play/p/3iIICGB4VsQ
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.AnyMatch(seq.Range(3), func(num int) bool { return num > 1 })) fmt.Println(seq.AnyMatch(seq.Range(3), func(num int) bool { return num > 3 })) fmt.Println(seq.AnyMatch(seq.Range(0), func(num int) bool { return num > 3 })) }
Output: true false false
func AnyMatch2 ¶ added in v0.0.5
AnyMatch2 checks if any (key, value) pair in the sequence satisfies a predicate.
It returns true if at least one pair satisfies the predicate.
Play: https://go.dev/play/p/tPbNr8MDPhp
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.AnyMatch2(seq.Range2(3), func(key, _ int) bool { return key > 1 })) fmt.Println(seq.AnyMatch2(seq.Range2(3), func(key, _ int) bool { return key > 3 })) fmt.Println(seq.AnyMatch2(seq.Range2(0), func(key, _ int) bool { return key > 3 })) }
Output: true false false
func Append ¶ added in v0.0.5
Append appends multiple elements to the end of the input sequence.
It takes an input sequence and additional elements to append.
Play: https://go.dev/play/p/MPeAjq7asWH
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Append(seq.Range(1, 3), 4, 5) { if num > 4 { break } fmt.Println(num) } for num := range seq.Append(seq.Range(1, 3), 4, 5) { if num > 1 { break } fmt.Println(num) } }
Output: 1 2 4 1
func Append2 ¶ added in v0.0.5
Append2 appends additional (key, value) pairs to the input sequence.
It returns a new sequence with the appended pairs.
Play: https://go.dev/play/p/yGCbA5v9OyG
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Append2(seq.Range2(1, 3), seq.T(4, 8), seq.T(5, 10)) { if key > 4 { break } fmt.Println(key, val) } for key, val := range seq.Append2(seq.Range2(1, 3), seq.T(4, 8), seq.T(5, 10)) { if key > 0 { break } fmt.Println(key, val) } }
Output: 0 1 1 2 4 8 0 1
func CenteredMovingAvg ¶ added in v0.0.10
CenteredMovingAvg calculates the centered moving average of a sequence.
It computes the average of `size` consecutive elements, centered when possible. The first few and last few averages are computed using available data.
Note:
Boundary effect at end.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.CenteredMovingAvg(seq.Range(0, 30, 5), 3) { if num > 10 { break } fmt.Println(num) } }
Output: 5 10
func Chan ¶
Chan converts a channel into an iterator sequence.
It takes a channel `input` and returns an iterator function that yields elements from the channel until it is closed.
Play: https://go.dev/play/p/Mi1zPueGIj5
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { cha := make(chan int, 3) cha <- 1 cha <- 2 cha <- 3 close(cha) for num := range seq.Chan(cha) { if num > 1 { break } fmt.Println(num) } }
Output: 1
func Chan2 ¶ added in v0.0.5
Chan2 converts a channel of (key, value) tuples into a sequence.
It returns a function that yields the tuples.
Play: https://go.dev/play/p/Rut7y-2vab8
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { cha := make(chan seq.Tuple[int, int], 3) cha <- seq.T(1, 1) cha <- seq.T(2, 2) cha <- seq.T(3, 3) close(cha) for key, val := range seq.Chan2(cha) { if key > 1 { break } fmt.Println(key, val) } }
Output: 1 1
func Chunk ¶ added in v0.0.6
Chunk divides the input sequence into sub-sequences of a specified size.
It returns a slice of sequences, each of the specified size or smaller for the last chunk.
Play: https://go.dev/play/p/45YB1zLCPK6
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seqs := seq.Chunk(seq.Range(5), 2) for _, items := range seqs { for num := range items { fmt.Println(num) } } }
Output: 0 1 2 3 4
func Chunk2 ¶ added in v0.0.6
Chunk2 divides the input sequence of key-value pairs into sub-sequences of a specified size.
It returns a slice of sequences, each containing key-value pairs of the specified size or smaller for the last chunk.
Play: https://go.dev/play/p/0rXdorTOEhx
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seqs := seq.Chunk2(seq.Range2(5), 2) for _, items := range seqs { for key, val := range items { if key > 2 { break } fmt.Println(key, val) } } }
Output: 0 0 1 1 2 2
func Clone ¶ added in v0.0.6
Clone creates multiple independent iterators from a single input iterator.
It returns a slice of `Seq[V]` objects, each of which is an independent iterator over the elements of the input sequence. The input sequence is only iterated once.
Play: https://go.dev/play/p/0UO3wI-MPvK
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for _, items := range seq.Clone(seq.Range(2), 3) { for num := range items { if num > 0 { break } fmt.Println(num) } } }
Output: 0 0 0
func Clone2 ¶ added in v0.0.6
Clone2 creates multiple independent iterators from a single input iterator for key-value pairs.
It returns a slice of `Seq2[K, V]` objects, each of which is an independent iterator over the elements of the input sequence. The input sequence is only iterated once.
Play: https://go.dev/play/p/HSmbV11r-9L
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for _, items := range seq.Clone2(seq.Range2(2), 3) { for key, val := range items { if key > 0 { break } fmt.Println(key, val) } } }
Output: 0 0 0 0 0 0
func Collect ¶ added in v0.0.5
Collect converts the input sequence into a slice of elements with a specified capacity.
It iterates over the sequence `input` and collects the elements into a slice. The slice has a capacity of `size`.
Play: https://go.dev/play/p/MaPnz8HNsUK
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Collect(seq.Range(4, 7), 3)) }
Output: [4 5 6]
func Collect2 ¶ added in v0.0.5
func Collect2[K comparable, V any](input iter.Seq2[K, V], size int) map[K]V
Collect2 collects (key, value) pairs from the sequence into a map.
It returns a map containing the collected pairs.
Play: https://go.dev/play/p/DOkWm2bnGgk
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Collect2(seq.Range2(4, 7), 3)) }
Output: map[0:4 1:5 2:6]
func Concat ¶ added in v0.0.14
Concat combines multiple sequence iterators into a single sequence iterator.
It returns an iter.Seq[V] that iterates over all elements from the provided sequences.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for val := range seq.Concat(seq.Seq(1, 2), seq.Seq(5, 6)) { if val > 5 { break } fmt.Println(val) } }
Output: 1 2 5
func Concat2 ¶ added in v0.0.14
Concat2 combines multiple key-value sequence iterators into a single sequence iterator.
It returns an iter.Seq2[K, V] that iterates over all key-value pairs from the provided sequences.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Concat2( seq.Seq2(seq.T(1, "one"), seq.T(2, "two")), seq.Seq2(seq.T(5, "five"), seq.T(6, "six")), ) { if key > 5 { break } fmt.Println(key, val) } }
Output: 1 one 2 two 5 five
func Count ¶
Count returns the number of elements in the input sequence.
It iterates over the sequence `input` and counts the number of elements.
Play: https://go.dev/play/p/qtsl1eaa1Sk
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Count(seq.Range(6))) }
Output: 6
func Count2 ¶ added in v0.0.5
Count2 returns the number of elements in the input sequence.
It iterates over the sequence `input` and counts the number of elements.
Play: https://go.dev/play/p/ntU3HJ1wfAX
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Count2(seq.Range2(6))) }
Output: 6
func Distinct ¶ added in v0.0.3
func Distinct[V comparable](input iter.Seq[V]) iter.Seq[V]
Distinct returns a new sequence containing only the distinct elements from the input sequence.
It iterates over the sequence `input` and filters out duplicate elements. Only the first occurrence of each element is included in the new sequence.
Play: https://go.dev/play/p/q87wdCVLrxm
Example ¶
package main import ( "fmt" "slices" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Distinct(slices.Values([]int{2, 2, 1, 3, 1})) { if num > 2 { break } fmt.Println(num) } }
Output: 2 1
func Distinct2 ¶ added in v0.0.5
Distinct2 returns a sequence that yields unique (key, value) pairs from the input sequence.
It filters out duplicate keys based on their comparability.
Play: https://go.dev/play/p/y87nHXwUK43
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Distinct2(seq.Merge2( seq.Range2(3), seq.Range2(1, 7), )) { if key > 2 { break } fmt.Println(key, val) } }
Output: 0 0 1 1 2 2
func Distribute ¶ added in v0.0.7
Distribute distributes the input sequence across multiple sequences based on the given number.
It returns the resulting slice of sequences.
Play: https://go.dev/play/p/R16UoqYOJmk
Example ¶
package main import ( "fmt" "sync" "sync/atomic" "github.com/xuender/flow/seq" ) func main() { seqs := seq.Distribute(seq.Range(100), 5) wait := sync.WaitGroup{} var count int32 wait.Add(5) for _, input := range seqs { go func() { for range input { atomic.AddInt32(&count, 1) } wait.Done() }() } wait.Wait() fmt.Println(count) }
Output: 100
func Distribute2 ¶ added in v0.0.7
Distribute2 distributes the input sequence across multiple sequences based on the given number.
It returns the resulting slice of sequences.
Play: https://go.dev/play/p/K7MnBXCMwiX
Example ¶
package main import ( "fmt" "sync" "sync/atomic" "github.com/xuender/flow/seq" ) func main() { seqs := seq.Distribute2(seq.Range2(100), 5) wait := sync.WaitGroup{} var count int32 wait.Add(5) for _, input := range seqs { go func() { for range input { atomic.AddInt32(&count, 1) } wait.Done() }() } wait.Wait() fmt.Println(count) }
Output: 100
func DropWhile ¶ added in v0.0.8
DropWhile skips elements from 'input' as long as 'predicate' returns true.
It returned function is a Seq[V] that can be used to iterate over the remaining elements.
Play: https://go.dev/play/p/fAUtx4arGB7
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.DropWhile(seq.Range(10), func(num int) bool { return num < 5 }) { if num > 8 { break } fmt.Println(num) } }
Output: 5 6 7 8
func DropWhile2 ¶ added in v0.0.8
DropWhile2 skips key-value pairs from 'input' as long as 'predicate' returns true.
Once the predicate is false, it starts yielding pairs.
It returned a Seq2[K, V] that can be used to iterate over the remaining pairs.
Play: https://go.dev/play/p/V_LQKrSZFiF
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.DropWhile2(seq.Range2(10), func(key, _ int) bool { return key < 5 }) { if key > 8 { break } fmt.Println(key, val) } }
Output: 5 5 6 6 7 7 8 8
func Each ¶
Each applies a function to each element in the input sequence.
It iterates over the sequence `input` and applies the `yield` function to each element. Iteration stops if `yield` returns false.
Play: https://go.dev/play/p/Al9R-6G1GJI
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seq.Each(seq.Range(5), func(num int) bool { if num > 2 { return false } fmt.Println(num) return true }, ) }
Output: 0 1 2
func Each2 ¶ added in v0.0.5
Each2 iterates over a sequence of (key, value) pairs and applies a callback function.
It processes each (key, value) pair in the sequence.
Play: https://go.dev/play/p/z_ZjLirSPnO
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seq.Each2(seq.Range2(5), func(key, val int) bool { if key > 2 { return false } fmt.Println(key, val) return true }, ) }
Output: 0 0 1 1 2 2
func Emit ¶
Emit consumes the input sequence by calling its iterator with a function that always returns true.
It effectively drains the sequence `input` by iterating over all its elements.
Play: https://go.dev/play/p/YmFQ4CMN5Q_u
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seq.Emit(seq.Peek( seq.Range(3), func(num int) { fmt.Println(num) }, )) }
Output: 0 1 2
func Emit2 ¶ added in v0.0.5
Emit2 iterates over a sequence of (key, value) pairs and discards each element.
It processes each (key, value) pair without returning any value.
Play: https://go.dev/play/p/Sc41jwY3k5__W
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { seq.Emit2(seq.Peek2( seq.Range2(3), func(key, val int) { fmt.Println(key, val) }, )) }
Output: 0 0 1 1 2 2
func Filter ¶
Filter returns a new sequence containing only the elements that satisfy the given predicate.
It iterates over the sequence `input` and applies the `predicate` function to each element. Only elements for which the predicate returns true are included in the new sequence.
Play: https://go.dev/play/p/5mc6R7yw-vt
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Filter( seq.Range(6), func(num int) bool { return num%2 == 0 }, ) { if num > 2 { break } fmt.Println(num) } }
Output: 0 2
func Filter2 ¶ added in v0.0.5
Filter2 filters a sequence of (key, value) pairs based on a predicate.
It returns a new sequence containing only the pairs that satisfy the predicate.
Play: https://go.dev/play/p/WXPRr6tBrtI
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Filter2( seq.Range2(6), func(key, _ int) bool { return key%2 == 0 }, ) { if key > 2 { break } fmt.Println(key, val) } }
Output: 0 0 2 2
func First ¶
First returns the first element of the input sequence or false if the sequence is empty.
It iterates over the sequence `input` and returns the first element found. If the sequence is empty, it returns the zero value of type `E` and false.
Play: https://go.dev/play/p/sGB5Xd43vM3
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.First(seq.Range(100))) fmt.Println(seq.First(seq.Range(0))) }
Output: 0 true 0 false
func First2 ¶ added in v0.0.5
First2 returns the first (key, value) pair in the sequence.
It returns the first pair and a boolean indicating if a pair was found.
Play: https://go.dev/play/p/hj0xJX0di1U
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.First2(seq.Range2(100))) fmt.Println(seq.First2(seq.Range2())) }
Output: 0 0 true 0 0 false
func FlatMap ¶
FlatMap flattens a sequence of slices into a single sequence of elements.
It takes a sequence of slices `input` and returns a new sequence that contains all the elements of the slices concatenated together.
Play: https://go.dev/play/p/Jt07i1tmBJo
Example ¶
package main import ( "fmt" "slices" "github.com/xuender/flow/seq" ) func main() { for num := range seq.FlatMap(slices.Values([][]int{{1, 2}, {3, 4}})) { if num > 2 { break } fmt.Println(num) } }
Output: 1 2
func Join ¶
Join concatenates the elements of the input sequence into a single string with a delimiter.
It takes a sequence `input` and a `delimiter` string. It concatenates the elements of the sequence into a single string, separated by the specified delimiter.
Play: https://go.dev/play/p/QmlJIBZ2-MW
Example ¶
package main import ( "fmt" "slices" "strconv" "github.com/xuender/flow/seq" ) type Num int func (p Num) String() string { return strconv.Itoa(int(p)) } func main() { fmt.Println(seq.Join(seq.Range(3), ",")) fmt.Println(seq.Join(slices.Values([]string{"a", "b", "c"}), ",")) fmt.Println(seq.Join(seq.Map(seq.Range(3), func(num int) Num { return Num(num) }), ",")) }
Output: 0,1,2 a,b,c 0,1,2
func Keys ¶ added in v0.0.5
Keys extracts keys from a sequence of (key, value) pairs.
It returns a new sequence containing only the keys.
Play: https://go.dev/play/p/IlQDB_yWQ8Z
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key := range seq.Keys(seq.Range2(3)) { if key > 1 { break } fmt.Println(key) } }
Output: 0 1
func Last ¶ added in v0.0.7
Last returns the last element of the sequence if it exists.
It returns the last element along with a boolean indicating if an element was found.
Play: https://go.dev/play/p/MWGf7kruIcX
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Last(seq.Range(10))) }
Output: 9 true
func Last2 ¶ added in v0.0.7
Last2 returns the last element of the sequence if it exists.
It returns the key and value of the last element along with a boolean indicating if an element was found.
Play: https://go.dev/play/p/Dj6k1UygyxY
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Last2(seq.Range2(10))) }
Output: 9 9 true
func Limit ¶
Limit returns a new sequence containing at most `limit` elements from the input sequence.
Play: https://go.dev/play/p/ZEzCPmRXHrs
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Limit(seq.Range(10), 3) { if num > 1 { break } fmt.Println(num) } for num := range seq.Limit(seq.Range(10), 3) { fmt.Println(num) } }
Output: 0 1 0 1 2
func Limit2 ¶ added in v0.0.5
Limit2 creates a new sequence that only includes the first N elements from the input sequence.
Play: https://go.dev/play/p/Cgz_vpTJfQv
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Limit2(seq.Range2(10), 3) { if key > 1 { break } fmt.Println(key, val) } for key, val := range seq.Limit2(seq.Range2(10), 3) { fmt.Println(key, val) } }
Output: 0 0 1 1 0 0 1 1 2 2
func Map ¶
Map applies a transformation function to each element in the input sequence and returns a new sequence.
Play: https://go.dev/play/p/sW40pxGpxvt
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Map( seq.Range(6), func(num int) float64 { return float64(num) * 1.35 }, ) { if num > 3.0 { break } fmt.Println(num) } }
Output: 0 1.35 2.7
func Map2 ¶ added in v0.0.5
func Map2[IK, IV, OK, OV any](input iter.Seq2[IK, IV], mapper func(IK, IV) (OK, OV)) iter.Seq2[OK, OV]
Map2 transforms a sequence of (key, value) pairs using a mapper function.
Play: https://go.dev/play/p/4zqyy37Vq4k
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Map2( seq.Range2(6), func(key, val int) (int, float64) { return key, float64(val) * 1.35 }, ) { if key > 2 { break } fmt.Println(key, val) } }
Output: 0 0 1 1.35 2 2.7
func Max ¶
Max returns the maximum element in the input sequence or false if the sequence is empty.
If the sequence is empty, it returns the zero value of type `E` and false.
Play: https://go.dev/play/p/1HUDnJ2ofC-
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Max(seq.Filter( seq.Range(6), func(num int) bool { return num%2 == 0 }, ))) }
Output: 4 true
func Max2 ¶ added in v0.0.5
Max2 finds the maximum (key, value) pair in the sequence.
It returns the maximum key, value, and a boolean indicating if a maximum was found.
Play: https://go.dev/play/p/eAbCubpT5mS
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Max2(seq.Filter2( seq.Range2(6), func(key, _ int) bool { return key%2 == 0 }, ))) }
Output: 4 4 true
func Merge ¶ added in v0.0.5
Merge combines multiple sequences into a single sequence.
It takes multiple sequences and merges them sequentially.
Play: https://go.dev/play/p/bOWQQiA4I6A
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Merge(seq.Range(3), seq.Range(6, 10)) { if num > 6 { break } fmt.Println(num) } }
Output: 0 1 2 6
func Merge2 ¶ added in v0.0.5
Merge2 combines multiple sequences of (key, value) pairs into a single sequence.
It merges the input sequences sequentially.
Play: https://go.dev/play/p/lfUCSSnrVtd
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Merge2(seq.Range2(3), seq.Range2(6, 10)) { if val > 6 { break } fmt.Println(key, val) } }
Output: 0 0 1 1 2 2 0 6
func Min ¶
Min returns the minimum element in the input sequence or false if the sequence is empty.
If the sequence is empty, it returns the zero value of type `E` and false.
Play: https://go.dev/play/p/Oxf000GM6Sx
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Min(seq.Skip(seq.Range(6), 3))) }
Output: 3 true
func Min2 ¶ added in v0.0.5
Min2 finds the minimum (key, value) pair in a sequence.
It returns the minimum key, value, and a boolean indicating if a minimum was found.
Play: https://go.dev/play/p/wipik9SWE6Y
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Min2(seq.Skip2(seq.Range2(6), 3))) }
Output: 3 3 true
func MovingAvg ¶ added in v0.0.10
MovingAvg calculates the moving average of a sequence.
It computes the average of every `size` consecutive elements in the input sequence.
Note:
Boundary effect at start.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.MovingAvg(seq.Range(0, 30, 5), 3) { if num > 12 { break } fmt.Println(num) } }
Output: 0 2 5 10
func NoneMatch ¶
NoneMatch checks if no elements in the input sequence satisfy the given predicate.
If no elements satisfy the predicate, the function returns true; otherwise, it returns false.
Play: https://go.dev/play/p/SulpdR0V3qQ
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.NoneMatch(seq.Range(3), func(num int) bool { return num > 1 })) fmt.Println(seq.NoneMatch(seq.Range(3), func(num int) bool { return num > 3 })) fmt.Println(seq.NoneMatch(seq.Range(0), func(num int) bool { return num > 3 })) }
Output: false true true
func NoneMatch2 ¶ added in v0.0.5
NoneMatch2 checks if no (key, value) pairs in the sequence satisfy a predicate.
It returns true if no pairs satisfy the predicate.
Play: https://go.dev/play/p/tsJ4Bz5SUSZ
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.NoneMatch2(seq.Range2(3), func(key, _ int) bool { return key > 1 })) fmt.Println(seq.NoneMatch2(seq.Range2(3), func(key, _ int) bool { return key > 3 })) fmt.Println(seq.NoneMatch2(seq.Range2(0), func(key, _ int) bool { return key > 3 })) }
Output: false true true
func Partition ¶ added in v0.0.11
Partition divides the input sequence into two sequences based on the predicate.
It returns two iter.Seq[V] the matched and unmatched sequences.
Play: https://go.dev/play/p/9fcf06hlk6j
Example ¶
package main import ( "fmt" "sync" "github.com/xuender/flow/seq" ) func main() { even, odd := seq.Partition(seq.Range(6), func(num int) bool { return num%2 == 0 }) var worker sync.WaitGroup worker.Add(2) go func() { for num := range even { fmt.Println("even", num) } worker.Done() }() go func() { for num := range odd { fmt.Println("odd", num) } worker.Done() }() worker.Wait() }
Output: even 0 odd 1 even 2 odd 3 even 4 odd 5
func Partition2 ¶ added in v0.0.11
func Partition2[K, V any](input iter.Seq2[K, V], predicate func(K, V) bool) (iter.Seq2[K, V], iter.Seq2[K, V])
Partition divides the input sequence into two sequences based on the predicate.
Returns two iter.Seq2[K, V] matched and unmatched sequences.
Play: https://go.dev/play/p/CTdz64mYGMd
Example ¶
package main import ( "fmt" "sync" "github.com/xuender/flow/seq" ) func main() { even, odd := seq.Partition2(seq.Range2(6), func(key, _ int) bool { return key%2 == 0 }) var worker sync.WaitGroup worker.Add(2) go func() { for key, val := range even { fmt.Println("even", key, val) } worker.Done() }() go func() { for key, val := range odd { fmt.Println("odd", key, val) } worker.Done() }() worker.Wait() }
Output: even 0 0 odd 1 1 even 2 2 odd 3 3 even 4 4 odd 5 5
func Peek ¶
Peek applies a given action to each element in the input sequence and returns the original sequence.
It applies the `action` to each element of the sequence and then returns the original sequence as an iterator.
Play: https://go.dev/play/p/WGvGrR3domM
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Peek(seq.Range(3), func(num int) { fmt.Println(num) }) { if num > 0 { break } } }
Output: 0 1
func Peek2 ¶ added in v0.0.5
Peek2 applies an action to each (key, value) pair in the sequence.
It returns a new sequence with the same pairs.
Play: https://go.dev/play/p/WGvGrR3domM
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Peek2(seq.Range2(3), func(key, val int) { fmt.Println(key, val) }) { if num > 0 { break } } }
Output: 0 0 1 1
func Prepend ¶ added in v0.0.8
Prepend adds items to the beginning of the sequence.
It function is useful for prepending items to a sequence in a functional manner.
Play: https://go.dev/play/p/F-H1tK8saqh
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Prepend(seq.Range(1, 3), 4, 5) { if num == 2 { break } fmt.Println(num) } for num := range seq.Prepend(seq.Range(1, 3), 7, 8) { if num < 8 { break } fmt.Println(num) } }
Output: 4 5 1
func Prepend2 ¶ added in v0.0.8
Prepend2 adds tuples to the beginning of the sequence.
It function is useful for prepending tuples to a sequence in a functional manner.
Play: https://go.dev/play/p/twjNH_C90Nt
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Prepend2(seq.Range2(1, 3), seq.T(4, 8), seq.T(5, 10)) { if key < 3 { break } fmt.Println(key, val) } for key, val := range seq.Prepend2(seq.Range2(1, 3), seq.T(4, 8), seq.T(5, 10)) { if key > 4 { break } fmt.Println(key, val) } }
Output: 4 8 5 10 4 8
func Range ¶
Range generates a sequence of integers from start to end (exclusive) with a given step. It returns a function that can be used as an iterator.
Args:
A variadic list of uint values representing the start, end, and step. 0 arguments: Empty sequence. 1 argument: Generates from 0 to args[0] (exclusive). 2 arguments: Generates from args[0] to args[1] (exclusive). 3 or more arguments: Generates from args[0] to args[1] (exclusive) with a step of args[2].
Play: https://go.dev/play/p/-FZJfetngJY
Example (One) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(3) { fmt.Println(num) } }
Output: 0 1 2
Example (OneNegative) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(-3) { fmt.Println(num) } }
Output: 0 -1 -2
Example (Repeat) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(3, 7, 0) { fmt.Println(num) } }
Output: 3 3 3 3
Example (StepDecrease) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(-1, -3, -1) { fmt.Println(num) } }
Output: -1 -2
Example (StepWrong) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(-1, -3, 1) { fmt.Println(num) } }
Output:
Example (Three) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(3, 10, 3) { fmt.Println(num) } }
Output: 3 6 9
Example (Two) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(3, 5) { fmt.Println(num) } }
Output: 3 4
Example (TwoDecrease) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(5, 3) { fmt.Println(num) } }
Output: 5 4
Example (TwoEqual) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(3, 3) { fmt.Println(num) } }
Output:
Example (TwoNegative) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(-3, -5) { fmt.Println(num) } }
Output: -3 -4
Example (TwoNegativeIncremental) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range(-5, -3) { fmt.Println(num) } }
Output: -5 -4
Example (Zero) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Range() { fmt.Println(num) } }
Output:
func Range2 ¶ added in v0.0.5
Range2 generates a sequence of integers from start to end (exclusive) with a given step. It returns a function that can be used as an iterator, yielding both the index and value.
Args:
A variadic list of int values representing the start, end, and step. 0 arguments: Empty sequence. 1 argument: Generates from 0 to args[0] (exclusive). 2 arguments: Generates from args[0] to args[1] (exclusive). 3 or more arguments: Generates from args[0] to args[1] (exclusive) with a step of args[2].
Example (One) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(3) { fmt.Println(key, val) } }
Output: 0 0 1 1 2 2
Example (OneNegative) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(-3) { fmt.Println(key, val) } }
Output: 0 0 1 -1 2 -2
Example (Repeat) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(3, 7, 0) { fmt.Println(key, val) } }
Output: 0 3 1 3 2 3 3 3
Example (StepDecrease) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(-1, -3, -1) { fmt.Println(key, val) } }
Output: 0 -1 1 -2
Example (StepWrong) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(-1, -3, 1) { fmt.Println(key, val) } }
Output:
Example (Three) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(3, 10, 3) { fmt.Println(key, val) } }
Output: 0 3 1 6 2 9
Example (Two) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(3, 5) { fmt.Println(key, val) } }
Output: 0 3 1 4
Example (TwoDecrease) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(5, 3) { fmt.Println(key, val) } }
Output: 0 5 1 4
Example (TwoEqual) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(3, 3) { fmt.Println(key, val) } }
Output:
Example (TwoNegative) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(-3, -5) { fmt.Println(key, val) } }
Output: 0 -3 1 -4
Example (TwoNegativeIncremental) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2(-5, -3) { fmt.Println(key, val) } }
Output: 0 -5 1 -4
Example (Zero) ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Range2() { fmt.Println(key, val) } }
Output:
func Reduce ¶
Reduce reduces the elements of the input sequence using the provided accumulator function.
It returns the reduced result and a boolean indicating if reduction occurred.
Play: https://go.dev/play/p/hnzytOtLAMi
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Reduce( seq.Range(5), func(num1, num2 int) int { return num1 + num2 }, )) }
Output: 10 true
func Reduce2 ¶ added in v0.0.5
Reduce2 reduces a sequence of (key, value) pairs using an accumulator function.
It returns the reduced result and a boolean indicating if reduction occurred.
Play: https://go.dev/play/p/En1xY_ONKGO
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Reduce2( seq.Range2(5), func(key1, val1, key2, val2 int) (int, int) { return key1 + key2, val1 + val2 }, )) }
Output: 10 10 true
func Repeat ¶ added in v0.0.6
Repeat creates a new sequence by repeating each element of the input sequence a specified number of times.
It returns a new sequence where each element of the input sequence is repeated `count` times.
Play: https://go.dev/play/p/BC8n9Zydld0
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Repeat(seq.Range(3), 2) { if num > 1 { break } fmt.Println(num) } }
Output: 0 0 1 1
func Repeat2 ¶ added in v0.0.6
Repeat2 creates a new sequence by repeating each element of the input sequence a specified number of times.
It returns a new sequence where each element of the input sequence is repeated `count` times.
Play: https://go.dev/play/p/hKK-TvJET1Q
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Repeat2(seq.Range2(7, 10), 2) { if key > 1 { break } fmt.Println(key, val) } }
Output: 0 7 0 7 1 8 1 8
func Reverse ¶
Reverse reverses the elements of the input sequence and returns them as an iterator.
It returns a new sequence with reversed pairs.
Play: https://go.dev/play/p/6U2htVv1-yw
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Reverse(seq.Range(5)) { if num < 3 { break } fmt.Println(num) } }
Output: 4 3
func Reverse2 ¶ added in v0.0.5
Reverse2 reverses the order of (key, value) pairs in the sequence.
It returns a new sequence with reversed pairs.
Play: https://go.dev/play/p/fBTjPo3M_ZH
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Reverse2(seq.Range2(5)) { if key < 3 { break } fmt.Println(key, val) } }
Output: 4 4 3 3
func Send ¶ added in v0.0.7
Send sends items from the input sequence to the provided output channel.
Play: https://go.dev/play/p/WTYoREBeZd3
Example ¶
package main import ( "fmt" "sync" "github.com/xuender/flow/seq" ) func main() { out := make(chan int) group := sync.WaitGroup{} group.Add(1) go func() { group.Wait() close(out) }() go func() { seq.Send(seq.Range(3), out) group.Done() }() for num := range out { fmt.Println(num) } }
Output: 0 1 2
func Send2 ¶ added in v0.0.7
Send2 sends items from the input sequence to the provided output channel.
Example ¶
package main import ( "fmt" "sync" "github.com/xuender/flow/seq" ) func main() { out := make(chan seq.Tuple[int, int]) group := sync.WaitGroup{} group.Add(1) go func() { group.Wait() close(out) }() go func() { seq.Send2(seq.Range2(3), out) group.Done() }() for item := range out { fmt.Println(item.K, item.V) } }
Output: 0 0 1 1 2 2
func Seq ¶ added in v0.0.14
Seq creates and returns a sequence iterator containing the given elements.
It returns an iter.Seq[V] that can be used to iterate over the provided elements.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for val := range seq.Seq(1, 2, 3) { fmt.Println(val) } }
Output: 1 2 3
func Seq2 ¶ added in v0.0.14
Seq2 creates and returns a sequence iterator for key-value pairs.
It returns an iter.Seq2[K, V] that can be used to iterate over the provided key-value pairs.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Seq2(seq.T(1, "one"), seq.T(2, "two"), seq.T(3, "three")) { if key > 2 { break } fmt.Println(key, val) } }
Output: 1 one 2 two
func Shuffle ¶ added in v0.0.9
Shuffle returns a new sequence with the elements of the input sequence in a random order.
It shuffles the input and yields each element in the shuffled order.
Play: https://go.dev/play/p/dVl8GzLyL6l
Example ¶
package main import ( "github.com/xuender/flow/seq" ) func main() { num := 0 for range seq.Shuffle(seq.Range(10)) { num++ if num > 3 { break } } }
Output:
func Shuffle2 ¶ added in v0.0.9
Shuffle2 returns a new sequence with the pairs of the input sequence in a random order.
It shuffles the input pairs and yields each pair in the shuffled order.
Play: https://go.dev/play/p/vvtLAWPy2Nf
Example ¶
package main import ( "github.com/xuender/flow/seq" ) func main() { num := 0 for range seq.Shuffle2(seq.Range2(10)) { num++ if num > 3 { break } } }
Output:
func Skip ¶
Skip skips the first `offset` elements of the input sequence and returns the remaining elements as an iterator.
It takes a sequence `input` and an integer `offset`, then creates a new iterator that skips the first `offset` elements from the original sequence.
Play: https://go.dev/play/p/Oxf000GM6Sx
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Skip(seq.Range(100), 8) { if num > 9 { break } fmt.Println(num) } }
Output: 8 9
func Skip2 ¶ added in v0.0.5
Skip2 skips the first 'offset' (key, value) pairs in the sequence.
It returns a new sequence with the remaining pairs.
Play: https://go.dev/play/p/wipik9SWE6Y
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Skip2(seq.Range2(100), 8) { if key > 9 { break } fmt.Println(key, val) } }
Output: 8 8 9 9
func Sorted ¶ added in v0.0.5
Sorted sorts a sequence of elements that implement the cmp.Ordered interface.
It returns a new sorted sequence.
Play: https://go.dev/play/p/5Oe9nuwtJ8x
Example ¶
package main import ( "fmt" "slices" "github.com/xuender/flow/seq" ) func main() { for num := range seq.Sorted(slices.Values([]int{3, 1, 2, 4})) { if num > 3 { break } fmt.Println(num) } }
Output: 1 2 3
func Sorted2 ¶ added in v0.0.5
Sorted2 sorts the input sequence by keys and returns a sorted sequence.
It takes a sequence of (key, value) pairs where keys are ordered.
Play: https://go.dev/play/p/_msKNYeyauI
Example ¶
package main import ( "fmt" "maps" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Sorted2(maps.All(map[int]int{1: 1, 2: 2, 3: 3, 4: 4})) { if key > 3 { break } fmt.Println(key, val) } }
Output: 1 1 2 2 3 3
func SortedFunc ¶ added in v0.0.5
SortedFunc sorts the elements of the input sequence using a custom comparison function.
It returns a new sorted sequence.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.SortedFunc( seq.Range(10), func(item1, item2 int) int { return item2 - item1 }, ) { if num < 7 { break } fmt.Println(num) } }
Output: 9 8 7
func SortedFunc2 ¶ added in v0.0.5
func SortedFunc2[K, V any](input iter.Seq2[K, V], cmp func(Tuple[K, V], Tuple[K, V]) int) iter.Seq2[K, V]
SortedFunc2 sorts (key, value) pairs in the sequence using a custom comparison function.
It returns a new sorted sequence.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.SortedFunc2( seq.Range2(10), func(item1, item2 seq.Tuple[int, int]) int { return item2.K - item1.K }, ) { if key < 7 { break } fmt.Println(key, val) } }
Output: 9 9 8 8 7 7
func SortedStableFunc ¶ added in v0.0.5
SortedStableFunc sorts the elements in the sequence using a custom comparison function with stable sorting.
It returns a new sorted sequence.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.SortedStableFunc( seq.Range(10), func(item1, item2 int) int { return item2 - item1 }, ) { if num < 7 { break } fmt.Println(num) } }
Output: 9 8 7
func SortedStableFunc2 ¶ added in v0.0.5
func SortedStableFunc2[K, V any](input iter.Seq2[K, V], cmp func(Tuple[K, V], Tuple[K, V]) int) iter.Seq2[K, V]
SortedStableFunc2 sorts (key, value) pairs using a custom comparison function with stable sorting.
It returns a new sorted sequence.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.SortedStableFunc2( seq.Range2(10), func(item1, item2 seq.Tuple[int, int]) int { return item2.K - item1.K }, ) { if key < 7 { break } fmt.Println(key, val) } }
Output: 9 9 8 8 7 7
func Sum ¶
Sum calculates the sum of all elements in the input sequence.
It iterates over the sequence `input` and calculates the sum of all elements.
Play: https://go.dev/play/p/EL_lsMnNq7u
Example ¶
package main import ( "fmt" "slices" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Sum(seq.Range(101))) fmt.Println(seq.Sum(slices.Values([]string{"a", "b", "c"}))) }
Output: 5050 abc
func TakeWhile ¶ added in v0.0.8
TakeWhile filters elements from 'input' using 'predicate'.
It yields elements until the predicate is false for the first time.
It returned is a Seq[V] that can be used to iterate over the filtered elements.
Play: https://go.dev/play/p/bNjM_sEmBVd
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for num := range seq.TakeWhile(seq.Range(10), func(num int) bool { return num < 3 }) { fmt.Println(num) } for num := range seq.TakeWhile(seq.Range(10), func(num int) bool { return num < 3 }) { if num > 1 { break } fmt.Println(num) } }
Output: 0 1 2 0 1
func TakeWhile2 ¶ added in v0.0.8
TakeWhile2 filters elements from 'input' using 'predicate'.
It yields key-value pairs until the predicate is false for the first time.
It returned a Seq2[K, V] that can be used to iterate over the filtered key-value pairs.
Play: https://go.dev/play/p/7ZvELBRdWSW
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.TakeWhile2(seq.Range2(10), func(key, _ int) bool { return key < 3 }) { fmt.Println(key, val) } for key, val := range seq.TakeWhile2(seq.Range2(10), func(key, _ int) bool { return key < 3 }) { if key > 1 { break } fmt.Println(key, val) } }
Output: 0 0 1 1 2 2 0 0 1 1
func ToChans ¶
ToChans distributes elements from the input sequence to multiple channels.
It takes a sequence `input` and an integer `size` indicating the number of channels. It distributes the elements evenly among the specified number of channels.
Play: https://go.dev/play/p/jGnmFmeCwgj
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { chans := seq.ToChans(seq.Range(5), 3) for _, cha := range chans { for item := range cha { fmt.Println(item) } } }
Output: 0 1 2 3 4
func ToChans2 ¶ added in v0.0.5
ToChans2 converts the input sequence into a slice of channels.
Each channel receives (key, value) tuples.
Play: https://go.dev/play/p/3BPhqH9_H3H
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { chans := seq.ToChans2(seq.Range2(5), 3) for _, cha := range chans { for item := range cha { fmt.Println(item.K, item.V) } } }
Output: 0 0 1 1 2 2 3 3 4 4
func Values ¶ added in v0.0.5
Values extracts values from a sequence of (key, value) pairs.
It returns a new sequence containing only the values.
Play: https://go.dev/play/p/X0ZD9Y5qxB8
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for val := range seq.Values(seq.Range2(3)) { if val > 1 { break } fmt.Println(val) } }
Output: 0 1
func Window ¶ added in v0.0.9
Window creates a sequence of sliding windows over the input sequence.
Each window contains `size` consecutive elements from the input.
Play: https://go.dev/play/p/tpADrJdzuSC
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for items := range seq.Window(seq.Range(5), 3) { if items[0] > 1 { break } fmt.Println(items) } }
Output: [0] [0 1] [0 1 2] [1 2 3]
func Window2 ¶ added in v0.0.9
Window2 creates a sequence of sliding windows over pairs in the input sequence.
Each window contains `size` consecutive pairs from the input.
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for items := range seq.Window2(seq.Range2(5), 3) { if items[0].K > 1 { break } fmt.Println(items) } }
Output: [{0 0}] [{0 0} {1 1}] [{0 0} {1 1} {2 2}] [{1 1} {2 2} {3 3}]
func Zip ¶ added in v0.0.9
Zip combines elements from two sequences into pairs.
It yields pairs of elements from `keys` and `values` until the shorter sequence is exhausted.
Play: https://go.dev/play/p/ge0QHAqIRxh
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { for key, val := range seq.Zip(seq.Range(5), seq.Range(6, 20)) { if key > 2 { break } fmt.Println(key, val) } }
Output: 0 6 1 7 2 8
Types ¶
type Tuple ¶ added in v0.0.5
type Tuple[K, V any] struct { K K V V }
Tuple represents a pair of values.
It contains two values of different types.
func T ¶ added in v0.0.5
T creates a Tuple from two values.
It returns a Tuple containing the values.
Play: https://go.dev/play/p/yGCbA5v9OyG
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.T(1, "a")) }
Output: {1 a}
func Tuples ¶ added in v0.0.5
Tuples converts a sequence of (key, value) pairs into a slice of Tuple[K, V].
It returns the slice containing the tuples.
Play: https://go.dev/play/p/t9QhSnSONUB
Example ¶
package main import ( "fmt" "github.com/xuender/flow/seq" ) func main() { fmt.Println(seq.Tuples(seq.Range2(2))) }
Output: [{0 0} {1 1}]
Source Files ¶
- append.go
- avg.go
- chan.go
- chunk.go
- clone.go
- collect.go
- concat.go
- count.go
- distinct.go
- distribute.go
- doc.go
- each.go
- emit.go
- filter.go
- first.go
- flat_map.go
- join.go
- last.go
- limit.go
- map.go
- maps.go
- match.go
- max.go
- merge.go
- min.go
- partition.go
- peek.go
- prepend.go
- range.go
- reduce.go
- repeat.go
- reverse.go
- send.go
- seq.go
- shuffle.go
- skip.go
- sorted.go
- sum.go
- tuple.go
- while.go
- window.go
- zip.go