modash/moslice/foreach.go
Leon Mika a20530ddfd Initial commit of modash
This was taken from github.com/lmika/gopkgs/fp
2025-01-27 13:19:52 +11:00

14 lines
364 B
Go

package moslice
// ForEachWithError runs the passed in function for each element of T. If an error
// is encountered, the error is returned immediately and any subsequence elements
// will not be processed.
func ForEachWithError[T any](ts []T, fn func(t T) error) error {
for _, t := range ts {
if err := fn(t); err != nil {
return err
}
}
return nil
}