GithubHelp home page GithubHelp logo

go-cartesian-product's People

Contributors

schwarmco avatar soypat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-cartesian-product's Issues

Seeing problems when 4 slices are used, the 4th slice values do not vary.

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]

Sufficiently large product will hang

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))
	}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.