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:
d-enk
2026-02-25 22:59:44 +03:00
committed by GitHub
parent b9c84bc078
commit c49f84658a
+8 -2
View File
@@ -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.