github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/ppln/v2/common.go (about)

     1  package ppln
     2  
     3  import (
     4  	"iter"
     5  )
     6  
     7  // SliceInput returns a function that iterates over a slice,
     8  // to be used as the input function in [Serial] and [NonSerial].
     9  func SliceInput[T any](s []T) iter.Seq2[T, error] {
    10  	return func(yield func(T, error) bool) {
    11  		for _, t := range s {
    12  			if !yield(t, nil) {
    13  				return
    14  			}
    15  		}
    16  	}
    17  }
    18  
    19  // RangeInput returns a function that iterates over a range of integers,
    20  // starting at start and ending at (and excluding) stop,
    21  // to be used as the input function in [Serial] and [NonSerial].
    22  func RangeInput(start, stop int) iter.Seq2[int, error] {
    23  	return func(yield func(int, error) bool) {
    24  		for i := start; i < stop; i++ {
    25  			if !yield(i, nil) {
    26  				return
    27  			}
    28  		}
    29  	}
    30  }