-
Notifications
You must be signed in to change notification settings - Fork 11
/
example123_test.go
68 lines (54 loc) · 1.44 KB
/
example123_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build go1.23
package rill_test
import (
"fmt"
"slices"
"github.com/destel/rill"
)
func ExampleFromSeq() {
// Start with an iterator that yields numbers from 1 to 10
numbersSeq := slices.Values([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
// Convert the iterator into a stream
numbers := rill.FromSeq(numbersSeq, nil)
// Transform each number
// Concurrency = 3
squares := rill.Map(numbers, 3, func(x int) (int, error) {
return square(x), nil
})
printStream(squares)
}
func ExampleFromSeq2() {
// Create an iter.Seq2 iterator that yields numbers from 1 to 10
numberSeq := func(yield func(int, error) bool) {
for i := 1; i <= 10; i++ {
if !yield(i, nil) {
return
}
}
}
// Convert the iterator into a stream
numbers := rill.FromSeq2(numberSeq)
// Transform each number
// Concurrency = 3
squares := rill.Map(numbers, 3, func(x int) (int, error) {
return square(x), nil
})
printStream(squares)
}
func ExampleToSeq2() {
// Convert a slice of numbers into a stream
numbers := rill.FromSlice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil)
// Transform each number
// Concurrency = 3
squares := rill.Map(numbers, 3, func(x int) (int, error) {
return square(x), nil
})
// Convert the stream into an iterator and use for-range to print the results
for val, err := range rill.ToSeq2(squares) {
if err != nil {
fmt.Println("Error:", err)
break // cleanup is done regardless of early exit
}
fmt.Printf("%+v\n", val)
}
}