github.com/enetx/g@v1.0.80/examples/sets.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  	"github.com/enetx/g/f"
     8  )
     9  
    10  func main() {
    11  	sl := g.SliceOf(1, 2, 3, 4, 4, 2, 5, 0)
    12  	s := g.SetOf(sl...) // convert Slice to Set
    13  
    14  	s.Iter().ForEach(func(i int) {
    15  		fmt.Println(i)
    16  	})
    17  
    18  	even := s.Iter().Filter(func(val int) bool { return val%2 == 0 }).Collect()
    19  
    20  	s.Iter().
    21  		Filter(func(val int) bool { return val%2 == 0 }).
    22  		Exclude(f.Zero).
    23  		Inspect(func(i int) {
    24  			fmt.Println(i)
    25  		}).
    26  		Collect().
    27  		Print()
    28  
    29  	s2 := g.SetOf(4, 5, 6, 7, 8)
    30  	s.SymmetricDifference(s2).Collect().Print()
    31  
    32  	set5 := g.SetOf(1, 2)
    33  	set6 := g.SetOf(2, 3, 4, 9)
    34  
    35  	s.Iter().Chain(set5.Iter(), set6.Iter()).Map(func(i int) int { return i + i }).Collect().Print()
    36  
    37  	set7 := set5.Difference(set6).Collect()
    38  	set7.Print()
    39  
    40  	s = g.SetOf(1, 2, 3, 4, 5)
    41  	even = s.Iter().Filter(f.Even).Collect()
    42  	even.Print()
    43  
    44  	s = s.Remove(1)
    45  	s.Print()
    46  
    47  	// iterate over set
    48  	for value := range s {
    49  		fmt.Println(value)
    50  	}
    51  
    52  	s = g.SetOf(1, 2, 3)
    53  	g.SetMap(s, g.NewInt) // g.Set[g.Int]
    54  }