Skip to content

Conversation

gnojus
Copy link

@gnojus gnojus commented Jan 7, 2024

One should use heap.FromSlice to specifify the backing slice.

Otherwise this will modify the expanded slice.

One should use heap.FromSlice to specifify the backing slice.
@zyedidia
Copy link
Owner

zyedidia commented Jan 8, 2024

Do you have a test case or example that demonstrates the problematic behavior?

@gnojus
Copy link
Author

gnojus commented Jan 9, 2024

https://go.dev/play/p/VssEFzSi2Qg

To be fair, another solution would be to just change the doc comment to mention that the variadic slice will be modified.

@gnojus
Copy link
Author

gnojus commented Jan 16, 2024

Also posting the snippet here as playground is having some problems with shared snippets lately:

package main

import (
        "fmt"

        "github.com/zyedidia/generic/heap"
)

func main() {
        arr := []int{1, 2, 3, 4}
        h := heap.From(func(a, b int) bool { return a < b }, arr...)
        fmt.Println(arr)
        h.Pop()
        fmt.Println(arr) // the slice has changed
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants