mowshon / iterium

🚀 Generic Channel-based Iterators for Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Product of two slices

nikicat opened this issue · comments

Hello, I didn't find it in the docs - how to make the product of two slices (of different types in a general case) using this library? Is it possible?
Something like:

In [4]: list(itertools.product([1,2], ['a', 'b']))
Out[4]: [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

@nikicat yes, it's possible. You need to use a slice of type any:

package main

import (
	"fmt"
	"github.com/mowshon/iterium"
)

func main() {
	chars := []any{1, 2, "a", "b"}
	data := iterium.Product(chars, 2)

	step := 0
	for value := range data.Chan() {
		fmt.Println(step, ")", value)
		step += 1
	}
}

Output:

1 ) [1 1]
2 ) [1 2]
3 ) [1 a]
4 ) [1 b]
5 ) [2 1]
6 ) [2 2]
7 ) [2 a]
8 ) [2 b]
9 ) [a 1]
10 ) [a 2]
11 ) [a a]
12 ) [a b]
13 ) [b 1]
14 ) [b 2]
15 ) [b a]
16 ) [b b]

@mowshon Thank you for the suggestion, but the result is not correct. It's a product of {1, 2, "a", "b"} x {1, 2, "a", "b"}, not {1, 2} x {"a", "b"}

@nikicat now I get it. I ran the above example in Python and it just merges the data between each other if the second parameter is not a number but a list.

I will add your question as a feature for a new version of the package.
Thanks!