mirror of
https://github.com/golang/go
synced 2024-11-15 05:00:31 -07:00
slices: reduce code nesting depth for Compact and CompactFunc
To make it easier to read. Change-Id: I2fa1eb78d879b9d86b4dc839be7ede37c7c864f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/581976 Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joedian Reid <joedian@google.com>
This commit is contained in:
parent
9e0685f9c2
commit
cf164403d1
@ -355,7 +355,9 @@ func Clone[S ~[]E, E any](s S) S {
|
||||
// which may have a smaller length.
|
||||
// Compact zeroes the elements between the new length and the original length.
|
||||
func Compact[S ~[]E, E comparable](s S) S {
|
||||
if len(s) > 1 {
|
||||
if len(s) < 2 {
|
||||
return s
|
||||
}
|
||||
for k := 1; k < len(s); k++ {
|
||||
if s[k] == s[k-1] {
|
||||
s2 := s[k:]
|
||||
@ -370,7 +372,6 @@ func Compact[S ~[]E, E comparable](s S) S {
|
||||
return s[:k]
|
||||
}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
@ -378,7 +379,9 @@ func Compact[S ~[]E, E comparable](s S) S {
|
||||
// For runs of elements that compare equal, CompactFunc keeps the first one.
|
||||
// CompactFunc zeroes the elements between the new length and the original length.
|
||||
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
|
||||
if len(s) > 1 {
|
||||
if len(s) < 2 {
|
||||
return s
|
||||
}
|
||||
for k := 1; k < len(s); k++ {
|
||||
if eq(s[k], s[k-1]) {
|
||||
s2 := s[k:]
|
||||
@ -393,7 +396,6 @@ func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
|
||||
return s[:k]
|
||||
}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user