From 8d8fe03598d93c1dca714b5ab820e28130804fa9 Mon Sep 17 00:00:00 2001 From: mstar Date: Sun, 1 Dec 2024 10:31:46 +0100 Subject: [PATCH] Add map compactor --- maputils/mapUtils.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 +}