From 949172f01b29a8fade4c864ef6666b4246f40a03 Mon Sep 17 00:00:00 2001 From: Mikhail Knyazhev Date: Sat, 25 Oct 2025 06:00:10 +0300 Subject: [PATCH] add slice convert + map convert --- map.go | 9 +++++++++ slice.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/map.go b/map.go index 2f9c89b..630bcaa 100644 --- a/map.go +++ b/map.go @@ -15,6 +15,15 @@ func EachMap[K Comparable, V any](in map[K]V, call func(key K, value V)) { } } +func ConvertMap[K1, K2 comparable, V1, V2 any](in map[K1]V1, call func(key K1, value V1) (K2, V2)) (out map[K2]V2) { + out = make(map[K2]V2, len(in)) + for k1, v1 := range in { + k2, v2 := call(k1, v1) + out[k2] = v2 + } + return +} + func FilterMap[K comparable, V any](in map[K]V, filter func(key K, value V) bool) (out map[K]V) { out = make(map[K]V, len(in)) for k, v := range in { diff --git a/slice.go b/slice.go index 68c04d5..4bf9cde 100644 --- a/slice.go +++ b/slice.go @@ -11,6 +11,14 @@ func Each[T any](in []T, call func(value T, index int)) { } } +func Convert[T, V any](in []T, call func(value T, index int) V) (out []V) { + out = make([]V, len(in)) + for i, v := range in { + out[i] = call(v, i) + } + return +} + func Join[T any](in ...[]T) (out []T) { size := 0 for _, m := range in {