From 7a3b4c33e0d3c7c20517d39c26b223642669e1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Tue, 3 Dec 2024 09:15:07 +0000 Subject: [PATCH] feat: add len and reverse slice --- conv/conv.go | 2 ++ slices/slices.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/conv/conv.go b/conv/conv.go index 892eb02..fabfe46 100644 --- a/conv/conv.go +++ b/conv/conv.go @@ -15,6 +15,8 @@ func String(val any) string { return fmt.Sprintf("%d", val) case float32, float64: return fmt.Sprintf("%f", val) + case []byte: + return string(val) case string: return val case nil: diff --git a/slices/slices.go b/slices/slices.go index d7a29b1..86ec913 100644 --- a/slices/slices.go +++ b/slices/slices.go @@ -156,3 +156,18 @@ func Size[T any](size int, slice ...T) bool { func Append[T any](slice []T, elements ...T) []T { return append(slice, elements...) } + +// Len returns the length of a slice. +func Len[T any](slice []T) int { + return len(slice) +} + +// Reverse reverses the order of elements in a slice. +func Reverse[T any](slice []T) []T { + for i := len(slice)/2 - 1; i >= 0; i-- { + opp := len(slice) - 1 - i + slice[i], slice[opp] = slice[opp], slice[i] + } + + return slice +}