Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cartesian products? [Especially wrt iterations]

6 views
Skip to first unread message

Michele Dondi

unread,
Jul 13, 2004, 9:31:57 AM7/13/04
to perl6-l...@perl.org
I apologize in advance for posting yet another "suggestion" without having
full knowledge of all apocalypses, and I fear (for a very positive
meaning of "fear") that the answer will be: "but that is already
available".

Well, the point is that I wonder wether Perl6 will support cartesian
products between lists, i.e. an infix operator that will return a list of
arrayrefs "containing" ordered pairs from the originating lists.

Now, one possible difficulty with such an operator would be wrt what to do
with applying it twice or even more times. One would reasonably want it to
be associative in the obvious sense, but this won't happen (because
mathematically speaking (AxA)xA and Ax(AxA) are associative "only" up to
a *natural* isomorphism, which doesn't make much difference,
mathematically speaking, but indeed it does in a computer programming
context).

However I already know that I'll be told that I can cook up something like
that myself. But to be fair I think that the context in which such a beast
would be most useful would be that of iterating over multiple variables,
so one (well, I for one!) could be content with a special syntax for that,
instead.

Put more clearly, it is now common to see things like:

for my $x (1..10) {
for my $y (5..20) {
for my $text (qw/foo bar baz/) {
do_stgh_with $x, $y, $text;
}
}
}

and it would be very much in the phylosophy of Perl to allow for a very
concise syntax to obtain the same effect. I am aware of the zip operator,
but AFAICT it solves an equally common but completely different problem...


Michele
--
The amount of repetition repetition isn't that great.
- Ignacy Sawicki in comp.text.tex
thread "Bug in FeynMF's Queues?"

Austin Hastings

unread,
Jul 13, 2004, 10:08:06 AM7/13/04
to Michele Dondi, perl6-l...@perl.org
--- Michele Dondi <bla...@pcteor1.mi.infn.it> wrote:
> I apologize in advance for posting yet another "suggestion" without
> having full knowledge of all apocalypses, and I fear (for a very
> positive meaning of "fear") that the answer will be: "but that is
> already available".

Using google(+perl6 +"cartesian product") would have led you to the
conclusion that this is already included. I hope this is horribly
wrong, since the syntax is a little bewildering.

> Well, the point is that I wonder wether Perl6 will support cartesian
> products between lists, i.e. an infix operator that will return a
> list of arrayrefs "containing" ordered pairs from the originating
> lists.

See Luke Palmer's "Outer product considered useful" post:
http://www.mail-archive.com/perl6-l...@perl.org/msg15513.html

=Austin

Jonathan Scott Duff

unread,
Jul 13, 2004, 10:10:05 AM7/13/04
to Michele Dondi, perl6-l...@perl.org
On Tue, Jul 13, 2004 at 03:31:57PM +0200, Michele Dondi wrote:
> Put more clearly, it is now common to see things like:
>
> for my $x (1..10) {
> for my $y (5..20) {
> for my $text (qw/foo bar baz/) {
> do_stgh_with $x, $y, $text;
> }
> }
> }
>
> and it would be very much in the phylosophy of Perl to allow for a very
> concise syntax to obtain the same effect. I am aware of the zip operator,
> but AFAICT it solves an equally common but completely different problem...

Are you sure?

for zip(1..10, 5..20, <<foo bar baz>>) -> $x, $y, $text {
do_something_with $x,$y,$text;
}

-Scott
--
Jonathan Scott Duff
du...@pobox.com

Michele Dondi

unread,
Jul 13, 2004, 11:13:31 AM7/13/04
to Austin Hastings, Jonathan Scott Duff, perl6-l...@perl.org
On Tue, 13 Jul 2004, Austin Hastings wrote:

> Using google(+perl6 +"cartesian product") would have led you to the
> conclusion that this is already included. I hope this is horribly
> wrong, since the syntax is a little bewildering.

[...]


> See Luke Palmer's "Outer product considered useful" post:
> http://www.mail-archive.com/perl6-l...@perl.org/msg15513.html

That's exactly the point! I wish too there were a more intuitive syntax,
possibly even employing a predefined array variable if none is
explicitly specified...

On Tue, 13 Jul 2004, Jonathan Scott Duff wrote:

> > and it would be very much in the phylosophy of Perl to allow for a very
> > concise syntax to obtain the same effect. I am aware of the zip operator,
> > but AFAICT it solves an equally common but completely different problem...
>
> Are you sure?
>
> for zip(1..10, 5..20, <<foo bar baz>>) -> $x, $y, $text {
> do_something_with $x,$y,$text;
> }

Not sure at all: admittedly I may well be one of the less informed ones
about Perl6 here. Though as far as I can understand zip() is for iterating
*in parallel*, and both other replies here and discussion previously held
here seem to indicate that it is so.


Michele
--
> Una gran americanata quello della cottura dell'Hot-dog con i gas di scarico
> della turbina
Una strunzata in piu' tanto gia' mangiano delle schifezze
in piu' condite con kerosene...
- Natale Novello su it.hobby.modellismo, "Re: Video spettacolare in volo"

Luke Palmer

unread,
Jul 13, 2004, 12:28:50 PM7/13/04
to Michele Dondi, perl6-l...@perl.org

That does a different thing entirely. Allow me to demonstrate, using
C<outer> that I proposed:

for zip(1..3, 4..6) -> $x, $y {
say "$x,$y";
}
1,4
2,5
3,6

for outer(1..3, 4..6) -> $x, $y {
say "$x,$y";
}
1,4
1,5
1,6
2,4
2,5
2,6
3,4
3,5
3,6

The essential difference between the lists that zip and outer generate
is the essential difference between the outer and inner products. outer
expands an index while zip collapses an index.

Note also that all arguments to zip ought to be the same length (and if
not, they are finessed into bein g so), where this restriction desn't
apply to outer.

Luke

Austin Hastings

unread,
Jul 13, 2004, 1:15:27 PM7/13/04
to Michele Dondi, perl6-l...@perl.org
--- Michele Dondi <bla...@pcteor1.mi.infn.it> wrote:
> On Tue, 13 Jul 2004, Austin Hastings wrote:
>
> > Using google(+perl6 +"cartesian product") would have led you to the
> > conclusion that this is already included. I hope this is horribly
> > wrong, since the syntax is a little bewildering.
> [...]
> > See Luke Palmer's "Outer product considered useful" post:
> > http://www.mail-archive.com/perl6-l...@perl.org/msg15513.html
>
> That's exactly the point! I wish too there were a more intuitive
> syntax, possibly even employing a predefined array variable if none
> is explicitly specified...

Boggle! While C<outer> may not be totally intuitive, it's not far off.
Likewise, the latin-1 version is pretty good:

for @x × @y × @z -> $x, $y, $z {
...
}

Is there some even more intuitive way than this?

> On Tue, 13 Jul 2004, Jonathan Scott Duff wrote:
>
> > Are you sure?
> >
> > for zip(1..10, 5..20, <<foo bar baz>>) -> $x, $y, $text {
> > do_something_with $x,$y,$text;
> > }
>
> Not sure at all: admittedly I may well be one of the less informed
> ones about Perl6 here. Though as far as I can understand zip() is for
> iterating *in parallel*, and both other replies here and discussion
> previously held here seem to indicate that it is so.

No, ¥ (C<zip>) is wrong for this. (It's the inner product, so it really
ought to be '·' (C<inner>) except for the wierd origin.)

=Austin

Juerd

unread,
Jul 13, 2004, 3:40:04 PM7/13/04
to Luke Palmer, perl6-l...@perl.org
Luke Palmer skribis 2004-07-13 10:28 (-0600):

> for outer(1..3, 4..6) -> $x, $y {
> say "$x,$y";
> }
> 1,4
> 1,5
> 1,6
> 2,4
> 2,5
> 2,6
> 3,4
> 3,5
> 3,6

So outer is somewhat like {} in shell globs?

perl -le'print for glob "{1,2,3},{4,5,6}"'


Juerd

0 new messages