Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(slice): compact method reduces the creation of one slice,add a few deprecation comments #212

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading