github.com/jwowillo/pipe@v1.2.0/example_produce_and_process_test.go (about)

     1  package pipe_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jwowillo/pipe"
     7  )
     8  
     9  func Example_produceAndProcess() {
    10  	p := pipe.New(
    11  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    12  			return x.(string) + "a"
    13  		}),
    14  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    15  			return x.(string) + "b"
    16  		}),
    17  		pipe.StageFunc(func(x pipe.Item) pipe.Item {
    18  			return x.(string) + "c"
    19  		}),
    20  	)
    21  	var isDone bool
    22  	f := pipe.ProducerFunc(func() (pipe.Item, bool) {
    23  		if isDone {
    24  			return "", false
    25  		}
    26  		isDone = true
    27  		return "", true
    28  	})
    29  	fmt.Println(pipe.ProduceAndProcess(p, f)[0])
    30  	// Output:
    31  	// abc
    32  }