Coder Social home page Coder Social logo

Comments (3)

deckarep avatar deckarep commented on August 19, 2024 1

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: mySet which has some data. You create a different set that is empty. This represents the stuff "taken" out of the original set. But instead of actually taking anything out, you add what is to be removed into diffSet.

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)
}

from golang-set.

deckarep avatar deckarep commented on August 19, 2024

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}

from golang-set.

Nhoya avatar Nhoya commented on August 19, 2024

Yea seems it's working, no idea why it hanged... well if that's the only way i will use it, thanks

from golang-set.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.