Created
July 21, 2022 13:41
-
-
Save milanboers/466b74ad62bb92e12d32c1abe5ff8f95 to your computer and use it in GitHub Desktop.
Map function over arguments golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func mapF[T any, R any](f func(T) (R, error), values ...T) ([]R, error) { | |
result := make([]R, len(values)) | |
for _, value := range values { | |
mappedValue, err := f(value) | |
if err != nil { | |
return nil, fmt.Errorf("mapF failure: %w", err) | |
} | |
result = append(result, mappedValue) | |
} | |
return result, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment