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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func main() {
    10  	// Create a slice of integers
    11  	slice := g.Slice[int]{0, 1, 2, 3}
    12  
    13  	// Get an iterator for the slice, generate combinations, and collect the result
    14  	perms := slice.Iter().Combinations(3).Collect()
    15  
    16  	// Iterate over the combinations and print each one
    17  	// 012 013 023 123
    18  	for _, perm := range perms {
    19  		fmt.Println(perm)
    20  	}
    21  
    22  	// Create two slices of strings
    23  	p1 := g.SliceOf[g.String]("a", "b")
    24  	p2 := g.SliceOf[g.String]("c", "d")
    25  
    26  	// Chain the two slices, convert to uppercase, generate combinations, and collect the result
    27  	pp := p1.
    28  		Iter().
    29  		Chain(p2.Iter()).
    30  		Map(g.String.Upper).
    31  		Combinations(2).
    32  		Collect()
    33  
    34  	// Iterate over the combinations and print each one
    35  	// AB AC AD BC BD CD
    36  	for _, v := range pp {
    37  		v.Print()
    38  	}
    39  }