From f387a7123040d9414350bf783bdeff17ac0572c7 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Thu, 22 Sep 2022 09:41:06 +0200 Subject: Add generic Map implementation (#108) Instead of waiting for a good time to switch the `Map` implementation, I've decided to just offer two implementations: one for Go versions before 1.18, and one for 1.18 and onwards. This is achieved using build tags in the source files. This is obviously a breaking change for consumers of this library that use Go 1.18 and onwards. See #88--- gomponents_generic.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 gomponents_generic.go (limited to 'gomponents_generic.go') diff --git a/gomponents_generic.go b/gomponents_generic.go new file mode 100644 index 0000000..6e6500b --- /dev/null +++ b/gomponents_generic.go @@ -0,0 +1,13 @@ +//go:build go1.18 +// +build go1.18 + +package gomponents + +// Map a slice of anything to a slice of Nodes. +func Map[T any](ts []T, cb func(T) Node) []Node { + var nodes []Node + for _, t := range ts { + nodes = append(nodes, cb(t)) + } + return nodes +} -- cgit 1.4.1