Skip to content

Commit

Permalink
Added Iter() to the set implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
deckarep committed Dec 27, 2013
1 parent ae1e77f commit f6be605
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
12 changes: 12 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 47 additions & 29 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ SOFTWARE.
package mapset

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -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"))
*/
}

0 comments on commit f6be605

Please sign in to comment.