aboutsummaryrefslogtreecommitdiff
path: root/util.go
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2022-12-11 14:13:07 +0000
committerGrégoire Duchêne <gduchene@awhk.org>2022-12-11 14:13:07 +0000
commit502fef431dc8999243935a2b345e658f86387e49 (patch)
tree24afa02b3f9a2e95980caf4f030aeab6e5dc688f /util.go
parentcf1bfb5f93c5c8f5b923e2ed84a7ef45f188876c (diff)
Add SliceMap
Diffstat (limited to 'util.go')
-rw-r--r--util.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/util.go b/util.go
index daf2817..ec2a3dd 100644
--- a/util.go
+++ b/util.go
@@ -11,6 +11,19 @@ func Must[T any](val T, err error) T {
return val
}
+// SliceMap applies a function to a slice and returns a new slice made
+// of the returned values.
+func SliceMap[T ~[]S, S, U any](f func(S) U, ts T) []U {
+ if len(ts) == 0 {
+ return nil
+ }
+ ret := make([]U, len(ts))
+ for i, t := range ts {
+ ret[i] = f(t)
+ }
+ return ret
+}
+
// NoCopy flags a type that embeds it as not to be copied. Go does not
// prevent values from being copied, but ‘go vet’ will pick it up and
// signal it, which can then be caught by many CI/CD pipelines.