-
Notifications
You must be signed in to change notification settings - Fork 275
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
Best way to remove element from Set #53
Comments
So, it's typically always a bad idea to iterate a data-structure and mutate it during iteration as in your first example. The reason is because it pretty much invalidates the iterator. Your second example is actually the proper way to do it, especially when dealing with sets. You start off with your original set called: Then you just take the difference. In fact the example works for me without hanging at all. package main
import (
"bufio"
"fmt"
"os"
mapset "github.com/deckarep/golang-set"
)
func main() {
mySet := mapset.NewSet()
mySet.Add("a")
mySet.Add("b")
mySet.Add("c")
diffSet := mapset.NewSet()
//fill set with stuff
myIt := mySet.Iterator()
scanner := bufio.NewScanner(os.Stdin)
for stuff := range myIt.C {
fmt.Println("Found " + stuff.(string) + " remove it?[Y/n]")
scanner.Scan()
text := scanner.Text()
if text == "y" || text == "Y" {
fmt.Println("removing")
diffSet.Add(stuff) //this will hang too
fmt.Println("done")
}
}
mySet = mySet.Difference(diffSet)
fmt.Printf("result: %+v\n", mySet)
} |
Here is the output of the run above: Found a remove it?[Y/n]
y
removing
done
Found b remove it?[Y/n]
n
Found c remove it?[Y/n]
n
result: Set{b, c} |
Yea seems it's working, no idea why it hanged... well if that's the only way i will use it, thanks |
Let's say i'v a Set, i want to cycle all the elements in this set and ask the user to select which element remove from the set, the problem now is that if you try to remove the element with
myset.Remove(element)
in the cycle it will hang (probably because you cant iterate the elements and remove them in the same time).One solution is to create another Set and filling it with the value from the cycle and after the end of cycle use
myset.Difference(OtherSet)
but i don't want to create another set just for this, do you have another solution more efficient than thisEDIT: as i can see, you can't add stuff to a set while you are iterating another set, still will hang
The text was updated successfully, but these errors were encountered: