diff --git a/maputils/mapUtils.go b/maputils/mapUtils.go index 0c8a6f0..93bba2a 100644 --- a/maputils/mapUtils.go +++ b/maputils/mapUtils.go @@ -71,3 +71,15 @@ func CompareMap[K, V comparable](a, b map[K]V) bool { } return true } + +func Compact[K comparable, V any]( + m map[K]V, + compactor func(accK K, accV V, nextK K, nextV V) (K, V), +) (K, V) { + var accK K + var accV V + for k, v := range m { + accK, accV = compactor(accK, accV, k, v) + } + return accK, accV +}