Skip to content

Commit

Permalink
perf(slice): compact method reduces the creation of one slice,add a f…
Browse files Browse the repository at this point in the history
…ew deprecation comments
  • Loading branch information
cannian1 committed Apr 11, 2024
1 parent 6853d62 commit 57b0629
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
4 changes: 3 additions & 1 deletion maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func HasKey[K comparable, V any](m map[K]V, key K) bool {
}

// MapToStruct converts map to struct
// Play: todo
// Play: https://go.dev/play/p/2i04QYbFk9-
func MapToStruct(m map[string]any, structObj any) error {
for k, v := range m {
err := setStructField(structObj, k, v)
Expand Down Expand Up @@ -388,6 +388,7 @@ func getFieldNameByJsonTag(structObj any, jsonTag string) string {
}

// ToSortedSlicesDefault converts a map to two slices sorted by key: one for the keys and another for the values.
// Play: todo
func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V) {
keys := make([]K, 0, len(m))

Expand All @@ -412,6 +413,7 @@ func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V) {

// ToSortedSlicesWithComparator converts a map to two slices sorted by key and using a custom comparison function:
// one for the keys and another for the values.
// Play: todo
func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V) {
keys := make([]K, 0, len(m))

Expand Down
39 changes: 12 additions & 27 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,30 +97,27 @@ func Chunk[T any](slice []T, size int) [][]T {
func Compact[T comparable](slice []T) []T {
var zero T

result := make([]T, 0, len(slice))

i := 0
for _, v := range slice {
if v != zero {
result = append(result, v)
slice[i] = v
i++
}
}
return result[:len(result):len(result)]
return slice[:i]
}

// Concat creates a new slice concatenating slice with any additional slices.
// Play: https://go.dev/play/p/gPt-q7zr5mk
func Concat[T any](slice []T, slices ...[]T) []T {
totalLen := len(slice)

func Concat[T any](slices ...[]T) []T {
totalLen := 0
for _, v := range slices {
totalLen += len(v)
}

result := make([]T, 0, totalLen)

result = append(result, slice...)
for _, s := range slices {
result = append(result, s...)
for _, v := range slices {
result = append(result, v...)
}

return result
Expand Down Expand Up @@ -695,10 +692,7 @@ func DropRight[T any](slice []T, n int) []T {
if n <= 0 {
return slice
}

result := make([]T, 0, size-n)

return append(result, slice[:size-n]...)
return slice[:size-n]
}

// DropWhile drop n elements from the start of a slice while predicate function returns true.
Expand Down Expand Up @@ -840,20 +834,11 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
return result
}

// Deprecated: use Concat instead.
// Merge all given slices into one slice.
// Play: https://go.dev/play/p/lbjFp784r9N
func Merge[T any](slices ...[]T) []T {
totalLen := 0
for _, v := range slices {
totalLen += len(v)
}
result := make([]T, 0, totalLen)

for _, v := range slices {
result = append(result, v...)
}

return result
return Concat(slices...)
}

// Intersection creates a slice of unique elements that included by all slices.
Expand Down Expand Up @@ -1017,7 +1002,7 @@ func SortBy[T any](slice []T, less func(a, b T) bool) {
quickSortBy(slice, 0, len(slice)-1, less)
}

// SortByField return sorted slice by field
// Deprecated: SortByField return sorted slice by field
// slice element should be struct, field type should be int, uint, string, or bool
// default sortType is ascending (asc), if descending order, set sortType to desc
// This function is deprecated, use Sort and SortBy for replacement.
Expand Down
2 changes: 2 additions & 0 deletions strutil/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ func HammingDistance(a, b string) (int, error) {
// Concat uses the strings.Builder to concatenate the input strings.
// - `length` is the expected length of the concatenated string.
// - if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number.
//
// Play: todo
func Concat(length int, str ...string) string {
if len(str) == 0 {
return ""
Expand Down

0 comments on commit 57b0629

Please sign in to comment.