iters

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

README

CI State Go Doc

iters

Go iterators.

Installation

go get github.com/gotidy/iters

General Iterators

The iters library provides a set of general-purpose iterators that simplify the process of transformation, creation and using collections.

Examples:

Filter: Filtering sequence elements.

seq := Filter(
    slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
    func(i int) bool { return i%2 == 0 },
)   

Map: Transforming sequence elements

for v := range Map(slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}), func(i int) string { return strconv.Itoa(i) }) {
    ...
}

Other examples can be found in tests.

Retry

The Retry iterator is allowed to iterate over sequence of delays, with the specified delays. The Retry waits for the specified delay before retrying, except cases when context is cancelled.

The Repeat, Trim, Of, Exponential, Jitter, MaxElapsedTime functions can be used to define delays suppliers. It is also possible to define your own iterator for special behavior.

for attempt, delay := range Retry(context.Background(), Jitter(Trim(Exponential(time.Millisecond, time.Second, 2), 5), 0.5)) {
    if err = doSomething(); err == nil {
        return nil
    }
    fmt.Println(attempt, delay)
}
return err
for range MaxElapsedTime(Retry(ctx, Repeat(time.Second)), time.Minute) {
    ...
}

Documentation

Index

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

func Count[V any](s iter.Seq[V]) int

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

func Count2[K, V any](s iter.Seq2[K, V]) int

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 CountFunc

func CountFunc[V any](s iter.Seq[V], f func(V) bool) int

CountFunc values.

func CountFunc2

func CountFunc2[K, V any](s iter.Seq2[K, V], f func(K, V) bool) int

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 EqualFunc

func EqualFunc[T1, T2 any](s1 iter.Seq[T1], s2 iter.Seq[T2], equal func(T1, T2) bool) bool

EqualFunc compare two sequences. Slow.

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

func Filter[V any](seq iter.Seq[V], f func(item V) bool) iter.Seq[V]

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

func Filter2[K, V any](seq iter.Seq2[K, V], f func(k K, v V) bool) iter.Seq2[K, V]

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

func Fold2[K comparable, V any](seq iter.Seq2[K, V]) iter.Seq2[K, V]

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

func Fold2Func[F comparable, K, V any](seq iter.Seq2[K, V], foldKey func(K, V) F) iter.Seq2[K, V]

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 FoldFunc

func FoldFunc[K comparable, V any](seq iter.Seq[V], foldKey func(V) K) iter.Seq[V]

FoldFunc push items from one sequence to another sequence skipping duplicates.

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

func Keys[K, V any](seq iter.Seq2[K, V]) iter.Seq[K]

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

func Map[V1, V2 any](seq iter.Seq[V1], f func(v V1) V2) iter.Seq[V2]

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

func Map2[K1, K2, V1, V2 any](seq iter.Seq2[K1, V1], f func(k K1, v V1) (K2, V2)) iter.Seq2[K2, V2]

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

func MapKeys[K1, K2, V any](seq iter.Seq2[K1, V], f func(item K1) K2) iter.Seq2[K2, V]

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

func MapValues[K, V1, V2 any](seq iter.Seq2[K, V1], f func(item V1) V2) iter.Seq2[K, V2]

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

func MaxElapsedTime[T any](seq iter.Seq[T], max time.Duration) iter.Seq[T]

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

func Merge[V any](seqs ...iter.Seq[V]) iter.Seq[V]

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

func Merge2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]

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

func NotEmptyValues[K any, V comparable](seq iter.Seq2[K, V]) iter.Seq2[K, V]

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

func NotNil[V any, P *V](seq iter.Seq[P]) iter.Seq[P]

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

func NotNilValues[K, V any, P *V](seq iter.Seq2[K, P]) iter.Seq2[K, P]

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

func Of[V any](vv ...V) iter.Seq[V]

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

func Pointers[V any](vv []V) iter.Seq[*V]

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

func Reduce[T, R any](seq iter.Seq[T], initializer R, f func(R, T) R) R

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

func Repeat[V any](v V) iter.Seq[V]

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

func ToSeq2[T, K, V any](seq iter.Seq[T], f func(item T) (K, V)) iter.Seq2[K, V]

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 Trim

func Trim[V any](s iter.Seq[V], count int) iter.Seq[V]

Trim trims a sequence by count.

func Values

func Values[K, V any](seq iter.Seq2[K, V]) iter.Seq[V]

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

func WithIndex[V any](seq iter.Seq[V]) iter.Seq2[int, V]

WithIndex converts a sequence of values into a key-value sequence, where key is an index starting with 0.

func WithKeys

func WithKeys[K, V any](seq iter.Seq[V], f func(item V) K) iter.Seq2[K, V]

WithKeys converts the sequence of values to the sequence of key-value pairs by adding key to the sequence.

Example
printSeq2(WithKeys(
	slices.Values([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}),
	func(v int) int { return v * 10 },
))
Output:

0 0
10 1
20 2
30 3
40 4
50 5
60 6
70 7
80 8
90 9

Types

This section is empty.

Jump to

Keyboard shortcuts

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