From f6be605df2f53e04b8d284cfa1fbc90951ffbee7 Mon Sep 17 00:00:00 2001 From: Ralph Caraveo III Date: Fri, 27 Dec 2013 13:59:47 -0800 Subject: [PATCH] Added Iter() to the set implementation. --- set.go | 12 +++++++++ set_test.go | 76 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/set.go b/set.go index dc3889f..f2f29da 100644 --- a/set.go +++ b/set.go @@ -154,6 +154,18 @@ func (set Set) Cardinality() int { return len(set) } +func (set Set) Iter() <-chan interface{} { + ch := make(chan interface{}) + go func() { + for elem := range set { + ch <- elem + } + close(ch) + }() + + return ch +} + // Equal determines if two sets are equal to each other. // If they both are the same size and have the same items they are considered equal. // Order of items is not relevent for sets to be equal. diff --git a/set_test.go b/set_test.go index b2c5acd..cf6c1b1 100644 --- a/set_test.go +++ b/set_test.go @@ -26,7 +26,6 @@ SOFTWARE. package mapset import ( - "fmt" "testing" ) @@ -375,42 +374,61 @@ func Test_SetClone(t *testing.T) { } } -func Test_Example(t *testing.T) { +func Test_Iterator(t *testing.T) { + a := NewSet() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet() + for val := range a.Iter() { + b.Add(val) + } - requiredClasses := NewSet() - requiredClasses.Add("Cooking") - requiredClasses.Add("English") - requiredClasses.Add("Math") - requiredClasses.Add("Biology") + if !a.Equal(b) { + t.Error("The sets are not equal after iterating through the first set") + } +} + +func Test_Example(t *testing.T) { + /* + requiredClasses := NewSet() + requiredClasses.Add("Cooking") + requiredClasses.Add("English") + requiredClasses.Add("Math") + requiredClasses.Add("Biology") - scienceSlice := []interface{}{"Biology", "Chemistry"} - scienceClasses := NewSetFromSlice(scienceSlice) + scienceSlice := []interface{}{"Biology", "Chemistry"} + scienceClasses := NewSetFromSlice(scienceSlice) - electiveClasses := NewSet() - electiveClasses.Add("Welding") - electiveClasses.Add("Music") - electiveClasses.Add("Automotive") + electiveClasses := NewSet() + electiveClasses.Add("Welding") + electiveClasses.Add("Music") + electiveClasses.Add("Automotive") - bonusClasses := NewSet() - bonusClasses.Add("Go Programming") - bonusClasses.Add("Python Programming") + bonusClasses := NewSet() + bonusClasses.Add("Go Programming") + bonusClasses.Add("Python Programming") - //Show me all the available classes I can take - allClasses := requiredClasses.Union(scienceClasses).Union(electiveClasses).Union(bonusClasses) - fmt.Println(allClasses) //Set{English, Chemistry, Automotive, Cooking, Math, Biology, Welding, Music, Go Programming} + //Show me all the available classes I can take + allClasses := requiredClasses.Union(scienceClasses).Union(electiveClasses).Union(bonusClasses) + fmt.Println(allClasses) //Set{English, Chemistry, Automotive, Cooking, Math, Biology, Welding, Music, Go Programming} - //Is cooking considered a science class? - fmt.Println(scienceClasses.Contains("Cooking")) //false + //Is cooking considered a science class? + fmt.Println(scienceClasses.Contains("Cooking")) //false - //Show me all classes that are not science classes, since I hate science. - fmt.Println(allClasses.Difference(scienceClasses)) //Set{English, Automotive, Cooking, Math, Welding, Music, Go Programming} + //Show me all classes that are not science classes, since I hate science. + fmt.Println(allClasses.Difference(scienceClasses)) //Set{English, Automotive, Cooking, Math, Welding, Music, Go Programming} - //Which science classes are also required classes? - fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology} + //Which science classes are also required classes? + fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology} - //How many bonus classes do you offer? - fmt.Println(bonusClasses.Cardinality()) //2 + //How many bonus classes do you offer? + fmt.Println(bonusClasses.Cardinality()) //2 - //Do you have the following classes? Welding, Automotive and English? - fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English")) + //Do you have the following classes? Welding, Automotive and English? + fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English")) + */ }