Documentation
¶
Index ¶
- func Contains[T comparable](s T, in iter.Seq[T]) bool
- func Count[V any](s iter.Seq[V]) int
- func Count2[K, V any](s iter.Seq2[K, V]) int
- func CountFunc[V any](s iter.Seq[V], f func(V) bool) int
- func CountFunc2[K, V any](s iter.Seq2[K, V], f func(K, V) bool) int
- func Equal[T comparable](s1, s2 iter.Seq[T]) bool
- func EqualFunc[T1, T2 any](s1 iter.Seq[T1], s2 iter.Seq[T2], equal func(T1, T2) bool) bool
- func Exponential[D constraints.Float | constraints.Integer](start, max D, factor float64) iter.Seq[D]
- func Filter[V any](seq iter.Seq[V], f func(item V) bool) iter.Seq[V]
- func Filter2[K, V any](seq iter.Seq2[K, V], f func(k K, v V) bool) iter.Seq2[K, V]
- func Fold[V comparable](seq iter.Seq[V]) iter.Seq[V]
- func Fold2[K comparable, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func Fold2Func[F comparable, K, V any](seq iter.Seq2[K, V], foldKey func(K, V) F) iter.Seq2[K, V]
- func FoldFunc[K comparable, V any](seq iter.Seq[V], foldKey func(V) K) iter.Seq[V]
- func Group[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V
- func GroupFunc[K comparable, V any](seq iter.Seq[V], key func(V) K) map[K][]V
- func Jitter[T constraints.Float | constraints.Integer](vv iter.Seq[T], factor float64) iter.Seq[T]
- func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]
- func Map[V1, V2 any](seq iter.Seq[V1], f func(v V1) V2) iter.Seq[V2]
- func Map2[K1, K2, V1, V2 any](seq iter.Seq2[K1, V1], f func(k K1, v V1) (K2, V2)) iter.Seq2[K2, V2]
- func MapKeys[K1, K2, V any](seq iter.Seq2[K1, V], f func(item K1) K2) iter.Seq2[K2, V]
- func MapValues[K, V1, V2 any](seq iter.Seq2[K, V1], f func(item V1) V2) iter.Seq2[K, V2]
- func MaxElapsedTime[T any](seq iter.Seq[T], max time.Duration) iter.Seq[T]
- 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 NotEmpty[V comparable](seq iter.Seq[V]) iter.Seq[V]
- func NotEmptyValues[K any, V comparable](seq iter.Seq2[K, V]) iter.Seq2[K, V]
- func NotNil[V any, P *V](seq iter.Seq[P]) iter.Seq[P]
- func NotNilValues[K, V any, P *V](seq iter.Seq2[K, P]) iter.Seq2[K, P]
- func Of[V any](vv ...V) iter.Seq[V]
- func Pointers[V any](vv []V) iter.Seq[*V]
- func Reduce[T, R any](seq iter.Seq[T], initializer R, f func(R, T) R) R
- func Repeat[V any](v V) iter.Seq[V]
- func Retry[R int, D time.Duration](ctx context.Context, delays iter.Seq[time.Duration]) iter.Seq2[int, time.Duration]
- func RetryAfterDelay[R int, D time.Duration](ctx context.Context, delays iter.Seq[time.Duration]) iter.Seq2[int, time.Duration]
- func ToSeq2[T, K, V any](seq iter.Seq[T], f func(item T) (K, V)) iter.Seq2[K, V]
- func Trim[V any](s iter.Seq[V], count int) iter.Seq[V]
- func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]
- func WithIndex[V any](seq iter.Seq[V]) iter.Seq2[int, V]
- func WithKeys[K, V any](seq iter.Seq[V], f func(item V) K) iter.Seq2[K, V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s T, in iter.Seq[T]) bool
Contains checks that the sequence contains the specified value.
Example ¶
s := slices.Values([]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90})
fmt.Println(Contains(50, s))
fmt.Println(Contains(31, s))
Output: true false
func Count ¶
Count values.
Example ¶
s := slices.Values([]int{1, 2, 3, 4, 5})
fmt.Println(Count(s))
fmt.Println(CountFunc(s, func(v int) bool { return v%2 == 0 }))
Output: 5 2
func Count2 ¶
Count2 values.
Example ¶
s := slices.All([]int{2, 4, 5, 8, 10})
fmt.Println(Count2(s))
fmt.Println(CountFunc2(s, func(k, v int) bool { return k%2 == 0 && v%2 == 0 }))
Output: 5 2
func CountFunc2 ¶
CountFunc2 counts values.
func Equal ¶
func Equal[T comparable](s1, s2 iter.Seq[T]) bool
Equal compare two sequences. Slow.
Example ¶
s1 := slices.Values([]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90})
s2 := slices.Values([]int{0, 10, 20, 30, 140, 50, 60, 70, 80, 90})
s3 := slices.Values([]int{0, 10, 20})
s4 := slices.Values([]int{})
s5 := slices.Values([]int(nil))
fmt.Println(Equal(s1, s1))
fmt.Println(Equal(s1, s2))
fmt.Println(Equal(s1, s3))
fmt.Println(Equal(s3, s3))
fmt.Println(Equal(s4, s4))
fmt.Println(Equal(s5, s5))
fmt.Println(Equal(s5, s1))
Output: true false false true true true false
func Exponential ¶
func Exponential[D constraints.Float | constraints.Integer](start, max D, factor float64) iter.Seq[D]
Exponential generate exponential sequence of values. The first value will be start, each following v = v * factor, but not greater than max.
Example ¶
start := 1.0 max := 100.0 factor := 2.0 fmt.Println(slices.Collect(Trim(Exponential(start, max, factor), 10)))
Output: [1 2 4 8 16 32 64 100 100 100]
func Filter ¶
Filter filters values from the the sequence ov values using a filter function.
Example ¶
printSeq(Filter(
slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) bool { return i%2 == 0 },
))
Output: 0 2 4 6 8
func Filter2 ¶
Filter2 filters values from the the sequence ov key-values pairs using a filter function.
Example ¶
printSeq2(Filter2(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(_, v int) bool { return v%2 == 0 },
))
Output: 0 0 2 2 4 4 6 6 8 8
func Fold ¶
func Fold[V comparable](seq iter.Seq[V]) iter.Seq[V]
Fold push items from one sequence to another sequence skipping duplicates.
Example ¶
printSeq(Fold(
slices.Values([]int{10, 0, 1, 1, 2, 2, 5, 5, 7, 9, 9, 10}),
))
Output: 10 0 1 2 5 7 9
func Fold2 ¶
Fold2 push items from one sequence to another sequence skipping duplicates.
Example ¶
printSeq2(Fold2(
Map2(
slices.All([]int{10, 0, 1, 1, 2, 2, 5, 5, 7, 9, 9, 10}),
func(k, v int) (int, int) { return v, k }, // Swap kes and values.
),
))
Output: 10 0 0 1 1 2 2 4 5 6 7 8 9 9
func Fold2Func ¶
Fold2Func push items from one sequence to another sequence skipping duplicates.
Example ¶
printSeq2(Fold2Func(
slices.All([]int{10, 0, 1, 1, 2, 2, 5, 5, 7, 9, 9, 10}),
func(_, v int) int { return v },
),
)
Output: 0 10 1 0 2 1 4 2 6 5 8 7 9 9
func Group ¶
func Group[K comparable, V any](seq iter.Seq2[K, V]) map[K][]V
Group group sequence by key.
Example ¶
var testSeq iter.Seq2[string, int] = func(yield func(string, int) bool) {
yield("a", 1)
yield("b", 2)
yield("a", 3)
yield("c", 4)
yield("b", 5)
}
result := Group(testSeq)
fmt.Println(result["a"])
fmt.Println(result["b"])
fmt.Println(result["c"])
Output: [1 3] [2 5] [4]
func GroupFunc ¶
func GroupFunc[K comparable, V any](seq iter.Seq[V], key func(V) K) map[K][]V
GroupFunc group sequence by key.
Example ¶
var testSeq iter.Seq[int] = func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(5)
}
result := GroupFunc(testSeq, func(v int) string {
if v%2 == 0 {
return "even"
}
return "odd"
})
fmt.Println("Even:", result["even"])
fmt.Println("Odd:", result["odd"])
Output: Even: [2 4] Odd: [1 3 5]
func Jitter ¶
func Jitter[T constraints.Float | constraints.Integer](vv iter.Seq[T], factor float64) iter.Seq[T]
Jitter returns sequence with added jitter to the values. value = value * (random value in range [1 - Jitter, 1 + Jitter]). Example: jitter of 10 with factor 0.1 will returns values in range [9, 11].
func Keys ¶
Keys convert Seq2 to a Seq by returning the keys of the sequence.
Example ¶
printSeq(Keys(
slices.All([]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}),
))
Output: 0 1 2 3 4 5 6 7 8 9
func Map ¶
Map converts the sequence of values to the sequence of values another type using a mapping function.
Example ¶
printSeq(Map(
slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) int { return i * 10 },
))
Output: 0 10 20 30 40 50 60 70 80 90
func Map2 ¶
Map2 converts the sequence of values to the sequence of values another type using a mapping function.
Example ¶
printSeq2(Map2(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i, v int) (int, int) { return i * 10, v * 100 },
))
Output: 0 0 10 100 20 200 30 300 40 400 50 500 60 600 70 700 80 800 90 900
func MapKeys ¶
MapKeys converts the sequence of key-value pairs using a keys mapping function.
Example ¶
printSeq2(MapKeys(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) int { return i * 10 },
))
Output: 0 0 10 1 20 2 30 3 40 4 50 5 60 6 70 7 80 8 90 9
func MapValues ¶
MapValues converts the sequence of key-value pairs using a values mapping function.
Example ¶
printSeq2(MapValues(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(v int) int { return v * 10 },
))
Output: 0 0 1 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90
func MaxElapsedTime ¶
MaxElapsedTime stops sequence processing after the specified time has elapsed.
Example ¶
start := time.Now()
for range MaxElapsedTime(Repeat(10), time.Millisecond*100) {
time.Sleep(time.Millisecond)
}
fmt.Println(time.Since(start) > time.Millisecond*100)
Output: true
func Merge ¶
Merge sequences of values into one.
Example ¶
s1 := []int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}
s2 := []int{90, 80, 70, 60, 50, 40, 30, 20, 10, 0}
seq1 := slices.Values(s1)
seq2 := slices.Values(s2)
fmt.Println(Equal(Merge(seq1), seq1))
fmt.Println(Equal(Merge(seq1, seq2), slices.Values(append(s1, s2...))))
printSeq(Merge[int]())
Output: true true
func Merge2 ¶
Merge2 sequences of key-value pairs into one.
Example ¶
s1 := slices.All([]int{0, 10, 20, 30, 40})
s2 := slices.All([]int{50, 60, 70, 80, 90})
printSeq2(Merge2[int, int]())
printSeq2(Merge2(s1))
printSeq2(Merge2(s1, s2))
Output: 0 0 1 10 2 20 3 30 4 40 0 0 1 10 2 20 3 30 4 40 0 50 1 60 2 70 3 80 4 90
func NotEmpty ¶
func NotEmpty[V comparable](seq iter.Seq[V]) iter.Seq[V]
NotEmpty skips zero values in the sequence.
Example ¶
printSeq(
NotEmpty(
Map(
slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) int {
if i%2 == 0 {
return i
}
return 0
},
),
),
)
Output: 2 4 6 8
func NotEmptyValues ¶
NotEmptyValues skips zero values in the sequence.
Example ¶
printSeq2(
NotEmptyValues(
MapValues(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) int {
if i%2 == 0 {
return i
}
return 0
},
),
),
)
Output: 2 2 4 4 6 6 8 8
func NotNil ¶
NotNil skips nil values in the sequence.
Example ¶
printSeq(
Map(
NotNil(
Map(
slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) *int {
if i%2 == 0 {
return &i
}
return nil
},
),
),
func(i *int) int { return *i },
),
)
Output: 0 2 4 6 8
func NotNilValues ¶
NotNilValues skips nil values in the sequence.
Example ¶
printSeq2(
MapValues(
NotNilValues(
MapValues(
slices.All([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(i int) *int {
if i%2 == 0 {
return &i
}
return nil
},
),
),
func(i *int) int { return *i },
),
)
Output: 0 0 2 2 4 4 6 6 8 8
func Of ¶
Of creates a sequence of members.
Example ¶
fmt.Println(slices.Collect(Of(1, 2, 4, 8, 16, 32, 64)))
Output: [1 2 4 8 16 32 64]
func Pointers ¶
Pointers returns a sequence of pointers to the elements of the given slice.
Example ¶
for v := range Pointers([]int{5, 7, 1}) {
fmt.Println(*v)
}
for v := range Pointers([]int(nil)) {
fmt.Println(*v)
}
Output: 5 7 1
func Reduce ¶
Reduce reduces a sequence to a single value using a reduction function.
Example ¶
fmt.Println(Reduce(
slices.Values([]int{0, 1, 0, 1, 0, 1, 0, 1, 0, 1}),
100,
func(r int, v int) int { return r + v },
))
Output: 105
func Repeat ¶
Repeat repeats a value infinitely.
Example ¶
fmt.Println(slices.Collect(Trim(Repeat(11), 10)))
Output: [11 11 11 11 11 11 11 11 11 11]
func Retry ¶
func Retry[R int, D time.Duration](ctx context.Context, delays iter.Seq[time.Duration]) iter.Seq2[int, time.Duration]
Retry returns sequence that allows to retry with specified delays. The first iteration occurs immediately (retry, delay, retry, delay, retry).
Example ¶
start := time.Now()
for attempt, delay := range Retry(context.Background(), Trim(Repeat(time.Millisecond*100), 10)) {
fmt.Println(attempt, delay)
}
fmt.Println(time.Since(start) > time.Second)
Output: 0 0s 1 100ms 2 100ms 3 100ms 4 100ms 5 100ms 6 100ms 7 100ms 8 100ms 9 100ms 10 100ms true
Example (Break) ¶
for attempt, delay := range Retry(context.Background(), Exponential(time.Millisecond, time.Second, 2)) {
fmt.Println(attempt, delay)
if attempt == 5 {
break
}
}
Output: 0 0s 1 1ms 2 2ms 3 4ms 4 8ms 5 16ms
Example (Ctx) ¶
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
for range Retry(ctx, Repeat(time.Millisecond*100)) {
fmt.Print()
}
fmt.Println("stopped")
close(done)
}()
cancel()
<-done
Output: stopped
func RetryAfterDelay ¶
func RetryAfterDelay[R int, D time.Duration](ctx context.Context, delays iter.Seq[time.Duration]) iter.Seq2[int, time.Duration]
RetryAfterDelay returns sequence that allows to retry with specified delays. Started from delay (delay, retry, delay, retry).
func ToSeq2 ¶
ToSeq2 converts the sequence of of individual values to the sequence of key-value pairs.
Example ¶
printSeq2(ToSeq2(
slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
func(v int) (int, int) { return v * 10, v * 100 },
))
Output: 0 0 10 100 20 200 30 300 40 400 50 500 60 600 70 700 80 800 90 900
func Values ¶
Values convert Seq2 to a Seq by returning the values of the sequence.
Example ¶
printSeq(Values(
slices.All([]int{0, 10, 20, 30, 40, 50, 60, 70, 80, 90}),
))
Output: 0 10 20 30 40 50 60 70 80 90
func WithIndex ¶
WithIndex converts a sequence of values into a key-value sequence, where key is an index starting with 0.
Types ¶
This section is empty.