github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/over/api.go (about)

     1  // Package over provides helpers for rangefunc feature introduced in go 1.22.
     2  package over
     3  
     4  // Filtered creates a rangefunc that iterates only those elements for which the 'filter' function returns true.
     5  func Filtered[T any](all func(func(T) bool), filter func(T) bool) func(func(T) bool) {
     6  	return func(consumer func(T) bool) {
     7  		all(func(e T) bool {
     8  			if filter(e) {
     9  				return consumer(e)
    10  			}
    11  			return true
    12  		})
    13  	}
    14  }
    15  
    16  // Converted creates a rangefunc that applies the 'converter' function to each iterable element.
    17  func Converted[From, To any](all func(func(From) bool), converter func(From) To) func(func(To) bool) {
    18  	return func(consumer func(To) bool) {
    19  		all(func(from From) bool {
    20  			return consumer(converter(from))
    21  		})
    22  	}
    23  }