schwarmco / go-cartesian-product Goto Github PK
View Code? Open in Web Editor NEWa package for building cartesian products in golang
License: MIT License
a package for building cartesian products in golang
License: MIT License
The document show that results will not be "in order"
, but the current implementation seems to be in order
.
Using: go version go1.8.3 linux/amd64
Seeing issue when trying to mix more than 4 lists of numbers (tested up to 7 interface lists and see issues with the 4th, 6th, and 7th lists not getting set as expect).
Test function to reproduce issue:
func main() {
a := []interface{}{1,2,3}
c := cartesian.Iter(a, a, a, a)
for product := range c {
fmt.Println(product)
}
}
Output of test (Note issue with 4th column, it is not the a mix of values {1,2,3}):
[1 1 1 3]
[1 1 1 3]
[1 1 1 3]
[1 1 2 3]
[1 1 2 3]
[1 1 2 3]
[1 1 3 3]
[1 1 3 3]
[1 1 3 3]
[1 2 1 3]
[1 2 1 3]
[1 2 1 3]
[1 2 2 3]
[1 2 2 3]
[1 2 2 3]
[1 2 3 3]
[1 2 3 3]
[1 2 3 3]
[1 3 1 3]
[1 3 1 3]
[1 3 1 3]
[1 3 2 3]
[1 3 2 3]
[1 3 2 3]
[1 3 3 3]
[1 3 3 3]
[1 3 3 3]
[2 1 1 3]
[2 1 1 3]
[2 1 1 3]
[2 1 2 3]
[2 1 2 3]
[2 1 2 3]
[2 1 3 3]
[2 1 3 3]
[2 1 3 3]
[2 2 1 3]
[2 2 1 3]
[2 2 1 3]
[2 2 2 3]
[2 2 2 3]
[2 2 2 3]
[2 2 3 3]
[2 2 3 3]
[2 2 3 3]
[2 3 1 3]
[2 3 1 3]
[2 3 1 3]
[2 3 2 3]
[2 3 2 3]
[2 3 2 3]
[2 3 3 3]
[2 3 3 3]
[2 3 3 3]
[3 1 1 3]
[3 1 1 3]
[3 1 1 3]
[3 1 2 3]
[3 1 2 3]
[3 1 2 3]
[3 1 3 3]
[3 1 3 3]
[3 1 3 3]
[3 2 1 3]
[3 2 1 3]
[3 2 1 3]
[3 2 2 3]
[3 2 2 3]
[3 2 2 3]
[3 2 3 3]
[3 2 3 3]
[3 2 3 3]
[3 3 1 3]
[3 3 1 3]
[3 3 1 3]
[3 3 2 3]
[3 3 2 3]
[3 3 2 3]
[3 3 3 3]
[3 3 3 3]
[3 3 3 3]
Here's an example, with go-cartesian-product code included. Note that we're just adding in this bitproduct
function that uses a variadic function to give us functionality like Python's itertools.product("01", repeat=int(sys.argv[1]))
:
This will spin up between 6500 and 8000 goroutines, which leads me to believe that something isn't being cleaned up when there is a sufficient degree of recursion happening.
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"sync"
)
// takes interface-slices and returns a channel, receiving cartesian products
func Iter(params ...[]interface{}) chan []interface{} {
// create channel
c := make(chan []interface{})
// create waitgroup
var wg sync.WaitGroup
// call iterator
wg.Add(1)
iterate(&wg, c, []interface{}{}, params...)
// call channel-closing go-func
go func() { wg.Wait(); close(c) }()
// return channel
return c
}
// private, recursive Iteration-Function
func iterate(wg *sync.WaitGroup, channel chan []interface{}, result []interface{}, params ...[]interface{}) {
// dec WaitGroup when finished
defer wg.Done()
// no more params left?
if len(params) == 0 {
// send result to channel
channel <- result
return
}
// shift first param
p, params := params[0], params[1:]
// iterate over it
for i := 0; i < len(p); i++ {
// inc WaitGroup
wg.Add(1)
// call self with remaining params
go iterate(wg, channel, append(result, p[i]), params...)
}
}
func bitproduct(repeat int) chan []interface{} {
arr := make([][]interface{}, repeat)
for i := range arr {
arr[i] = []interface{}{"0", "1"}
}
return Iter(arr...)
}
func main() {
i, err := strconv.Atoi(os.Args[1])
if err != nil {
panic(err)
}
for j := range bitproduct(i) {
fmt.Println(runtime.NumGoroutine())
fmt.Println(fmt.Sprintf("%v", j))
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.