From 51a1e352bcfce498dae35ba001014b3cc0ddb70b Mon Sep 17 00:00:00 2001 From: mountain Date: Fri, 1 Dec 2023 19:34:49 +0900 Subject: [PATCH] =?UTF-8?q?slice=20shrink=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- misc.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/misc.go b/misc.go index a37d40e..f787f08 100644 --- a/misc.go +++ b/misc.go @@ -103,3 +103,15 @@ func SerializeInterface(w io.Writer, val interface{}) (err error) { return } + +func ShrinkSlice[T any](in []T, compare func(elem T) bool) []T { + cursor := 0 + for i := 0; i < len(in); i++ { + if compare(in[i]) { + continue + } + in[cursor] = in[i] + cursor++ + } + return in[:cursor] +}