github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/slice/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[TS ~[]T, T any](elements TS, filter func(T) bool) func(func(int, T) bool) { 6 return func(consumer func(int, T) bool) { 7 for i, e := range elements { 8 if filter(e) { 9 if !consumer(i, e) { 10 return 11 } 12 } 13 } 14 } 15 } 16 17 // Converted creates a rangefunc that applies the 'converter' function to each iterable element. 18 func Converted[FS ~[]From, From, To any](elements FS, converter func(From) To) func(func(int, To) bool) { 19 return func(consumer func(int, To) bool) { 20 for i, e := range elements { 21 if !consumer(i, converter(e)) { 22 return 23 } 24 } 25 } 26 }