aboutsummaryrefslogtreecommitdiff
path: root/pipeline/example_test.go
blob: e322529ed19cff6bd685ea2b4a459db783530d87 (plain)
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
package pipeline_test

import (
	"context"
	"fmt"
	"sort"

	"git.sangtran.io.vn/quacker.git/pipeline"
)

func ExampleRun() {
	input := make(chan int, 5)
	for i := 1; i <= 5; i++ {
		input <- i
	}
	close(input)

	out := pipeline.Run(context.Background(), input,
		pipeline.Filter(func(v int) bool { return v%2 == 0 }),
		pipeline.Map(1, func(_ context.Context, v int) (int, bool) {
			return v * 10, true
		}),
	)

	var results []int
	for v := range out {
		results = append(results, v)
	}
	sort.Ints(results)
	fmt.Println(results)
	// Output: [20 40]
}

func ExampleSink() {
	input := make(chan string, 3)
	input <- "hello"
	input <- "world"
	input <- "!"
	close(input)

	var collected []string
	out := pipeline.Run(context.Background(), input,
		pipeline.Sink(1, func(_ context.Context, v string) {
			collected = append(collected, v)
		}),
	)

	for range out {
	}

	fmt.Println(collected)
	// Output: [hello world !]
}