From 02123df8cc15896f97c1580e49d8e2a991e37ddd Mon Sep 17 00:00:00 2001 From: sangtran Date: Thu, 3 Sep 2026 23:10:33 +0700 Subject: Moving all legacy quacker source code from github to cgit --- pipeline/example_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pipeline/example_test.go (limited to 'pipeline/example_test.go') diff --git a/pipeline/example_test.go b/pipeline/example_test.go new file mode 100644 index 0000000..e322529 --- /dev/null +++ b/pipeline/example_test.go @@ -0,0 +1,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 !] +} -- cgit v1.3.1