Crystal: Array#product is quite confusing.

Created on 27 Dec 2016  路  4Comments  路  Source: crystal-lang/crystal

Enumerable#product and #product(initial : Numeric) each will try to multiply the contents of whatever enumerable it is. There is also Array#product(ary : Array(U)) which will produce the set theoretic product of the two arrays.

This is confusing for several reasons:
1: product does totally different things depending on input.
2: There is no way to do a product between actual Sets without converting them to an array.

I'd suggest that at the very least one the second of these issues need to go away. Possibly by moving Array#product into Enumerable (perhaps renamed).

Perhaps it would also be nice to make product (and zip etc) into taking multiple arguments - being able to get flat structures like s1 xs2 x s3 without having to flatten any step would be very convenient in some cases.

feature discussion stdlib

Most helpful comment

https://en.wikipedia.org/wiki/Cartesian_product

so perhaps a Enuerable#cartesian_product instead of Array#product?

All 4 comments

What is a product between sets?

https://en.wikipedia.org/wiki/Cartesian_product

so perhaps a Enuerable#cartesian_product instead of Array#product?

Should this even be a Array instance method? Couldn't a Math.cartesian_product(ary, other_ary) serve the same purpose without polluting Array's namespace?

Any updates? @asterite

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  60Comments

ezrast picture ezrast  路  84Comments

RX14 picture RX14  路  62Comments

asterite picture asterite  路  139Comments

malte-v picture malte-v  路  77Comments