mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
perf: lazy it.Reverse iteration instead of in-place mutation (#814)
Replace mutable.Reverse() with lazy iteration from end. This avoids unnecessary array mutation and reduces overhead.
This commit is contained in:
@@ -473,8 +473,14 @@ func Shuffle[T any, I ~func(func(T) bool)](collection I) I {
|
||||
// Play: https://go.dev/play/p/9jthUzgF-u
|
||||
func Reverse[T any, I ~func(func(T) bool)](collection I) I {
|
||||
slice := slices.Collect(iter.Seq[T](collection))
|
||||
mutable.Reverse(slice)
|
||||
return I(slices.Values(slice))
|
||||
|
||||
return I(func(yield func(T) bool) {
|
||||
for i := len(slice) - 1; i >= 0; i-- {
|
||||
if !yield(slice[i]) {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Fill replaces elements of a sequence with `initial` value.
|
||||
|
||||
Reference in New Issue
Block a user