[golf?] list of matches

hello people,

i have a game where every opponent much play every other ones
so i implemented vs to get a list of all the matches from a list
of opponents.

i'm pretty sure that there is a shorter/more beautiful solution than
mine so i really would like to see.

mine is

  sub vs (@xs ( $head, *@tail ) ) {
    |($head X @tail),
    |(vs @tail if +@tail)
  }

  my @rounds = <jean marc quentin alexandre marie-france ludo >.&vs;

  for @rounds -> ( $a, $b )  { say "$a vs $b" };

anyone want to share?

regards,
marc
0
eiro
7/9/2019 9:36:22 PM
perl.perl6.users 1192 articles. 0 followers. Follow

6 Replies
15 Views

Similar Articles

[PageSpeed] 58

--0000000000006b3eb3058d467623
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

You might want to take a look at the cross ("X") operator, which takes two
or more lists as arguments and returns a list or all lists that can be
constructed combining the elements of each list (or, in other words, a
Cartesian product of the input lists).

Cheers,
Laurent.


Le mar. 9 juil. 2019 =C3=A0 23:36, Marc Chantreux <eiro@phear.org> a =C3=A9=
crit :

> hello people,
>
> i have a game where every opponent much play every other ones
> so i implemented vs to get a list of all the matches from a list
> of opponents.
>
> i'm pretty sure that there is a shorter/more beautiful solution than
> mine so i really would like to see.
>
> mine is
>
>   sub vs (@xs ( $head, *@tail ) ) {
>     |($head X @tail),
>     |(vs @tail if +@tail)
>   }
>
>   my @rounds =3D <jean marc quentin alexandre marie-france ludo >.&vs;
>
>   for @rounds -> ( $a, $b )  { say "$a vs $b" };
>
> anyone want to share?
>
> regards,
> marc
>

--0000000000006b3eb3058d467623
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>You might want to take a look at the cross (&quot;X&q=
uot;) operator, which takes two or more lists as arguments and returns a li=
st or all lists that can be constructed combining the elements of each list=
 (or, in other words, a Cartesian product of the input lists).</div><div><b=
r></div><div>Cheers,</div><div>Laurent.</div><div><br></div></div><br><div =
class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">Le=C2=A0mar. 9 =
juil. 2019 =C3=A0=C2=A023:36, Marc Chantreux &lt;<a href=3D"mailto:eiro@phe=
ar.org">eiro@phear.org</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquote cla=
ss=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid =
rgb(204,204,204);padding-left:1ex">hello people,<br>
<br>
i have a game where every opponent much play every other ones<br>
so i implemented vs to get a list of all the matches from a list<br>
of opponents.<br>
<br>
i&#39;m pretty sure that there is a shorter/more beautiful solution than<br=
>
mine so i really would like to see.<br>
<br>
mine is<br>
<br>
=C2=A0 sub vs (@xs ( $head, *@tail ) ) {<br>
=C2=A0 =C2=A0 |($head X @tail),<br>
=C2=A0 =C2=A0 |(vs @tail if +@tail)<br>
=C2=A0 }<br>
<br>
=C2=A0 my @rounds =3D &lt;jean marc quentin alexandre marie-france ludo &gt=
;.&amp;vs;<br>
<br>
=C2=A0 for @rounds -&gt; ( $a, $b )=C2=A0 { say &quot;$a vs $b&quot; };<br>
<br>
anyone want to share?<br>
<br>
regards,<br>
marc<br>
</blockquote></div>

--0000000000006b3eb3058d467623--
0
perl6
7/9/2019 9:44:07 PM
--000000000000c5ec67058d46a0b0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

A small example as a complement to my previous post:

my @a =3D 1, 2;
my @b =3D 3, 4;
my @c =3D @a X @b;       # -> [(1,3), (1,4), (2,3), (2,4)]



Le mar. 9 juil. 2019 =C3=A0 23:36, Marc Chantreux <eiro@phear.org> a =C3=A9=
crit :

> hello people,
>
> i have a game where every opponent much play every other ones
> so i implemented vs to get a list of all the matches from a list
> of opponents.
>
> i'm pretty sure that there is a shorter/more beautiful solution than
> mine so i really would like to see.
>
> mine is
>
>   sub vs (@xs ( $head, *@tail ) ) {
>     |($head X @tail),
>     |(vs @tail if +@tail)
>   }
>
>   my @rounds =3D <jean marc quentin alexandre marie-france ludo >.&vs;
>
>   for @rounds -> ( $a, $b )  { say "$a vs $b" };
>
> anyone want to share?
>
> regards,
> marc
>

--000000000000c5ec67058d46a0b0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>A small example as a complement to my previous post:<=
/div><div><br></div><div>my @a =3D 1, 2;<br>my @b =3D 3, 4;<br>my @c =3D @a=
 X @b; =C2=A0 =C2=A0 =C2=A0 # -&gt; [(1,3), (1,4), (2,3), (2,4)]</div><div>=
<br></div><div><br></div></div><br><div class=3D"gmail_quote"><div dir=3D"l=
tr" class=3D"gmail_attr">Le=C2=A0mar. 9 juil. 2019 =C3=A0=C2=A023:36, Marc =
Chantreux &lt;<a href=3D"mailto:eiro@phear.org">eiro@phear.org</a>&gt; a =
=C3=A9crit=C2=A0:<br></div><blockquote class=3D"gmail_quote" style=3D"margi=
n:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex=
">hello people,<br>
<br>
i have a game where every opponent much play every other ones<br>
so i implemented vs to get a list of all the matches from a list<br>
of opponents.<br>
<br>
i&#39;m pretty sure that there is a shorter/more beautiful solution than<br=
>
mine so i really would like to see.<br>
<br>
mine is<br>
<br>
=C2=A0 sub vs (@xs ( $head, *@tail ) ) {<br>
=C2=A0 =C2=A0 |($head X @tail),<br>
=C2=A0 =C2=A0 |(vs @tail if +@tail)<br>
=C2=A0 }<br>
<br>
=C2=A0 my @rounds =3D &lt;jean marc quentin alexandre marie-france ludo &gt=
;.&amp;vs;<br>
<br>
=C2=A0 for @rounds -&gt; ( $a, $b )=C2=A0 { say &quot;$a vs $b&quot; };<br>
<br>
anyone want to share?<br>
<br>
regards,<br>
marc<br>
</blockquote></div>

--000000000000c5ec67058d46a0b0--
0
perl6
7/9/2019 9:55:58 PM
On Tue, Jul 09, 2019 at 11:44:07PM +0200, Laurent Rosenfeld via perl6-users wrote:
> You might want to take a look at the cross ("X") operator, which takes two
> or more lists as arguments and returns a list or all lists that can be
> constructed combining the elements of each list (or, in other words, a
> Cartesian product of the input lists).

I used it in my first solution which was

  sub vs (@xs ( $head, *@tail ) ) {
    |($head X @tail),
    |(vs @tail if +@tail)
  }

if i do @xs X @xs, i have +@xs unexpected elements so i have to filter
the result:

  my @players = <jean marc quentin alexandre >;
  for @players X @players ==> grep({ [ne] |$_})
    -> ( $a , $b ) { say "$a vs $b" }

this is shorter but i my first solution doesn't need to be filtered.

regards
marc
0
eiro
7/9/2019 11:23:10 PM

> On Jul 9, 2019, at 4:36 PM, Marc Chantreux <eiro@phear.org> wrote:

> i'm pretty sure that there is a shorter/more beautiful solution than
> mine so i really would like to see.
=E2=80=94snip=E2=80=94

my @players =3D <jean marc quentin alexandre marie-france ludo>;

# Elegant using the "cat-ears" range modifiers in Perl 6:
for     0  ..^ @players.end -> \i {
    for i ^..  @players.end -> \j {
        say @players[i,j].join(' vs ');
    }
}
# See https://docs.perl6.org/type/Range


# But, even more elegant is to use the native method :^)
say .join(' vs ') for @players.combinations(2);

# See https://docs.perl6.org/routine/combinations

# --
# Hope this helps,
# Bruce Gray (Util of PerlMonks)
0
robertbrucegray3
7/10/2019 1:55:11 AM
hello Bruce;

> # Elegant using the "cat-ears" range modifiers in Perl 6:
> for     0  ..^ @players.end -> \i {
>     for i ^..  @players.end -> \j {
>         say @players[i,j].join(' vs ');
>     }
> }
> # See https://docs.perl6.org/type/Range

i played around it and got:

  my @players = <jean marc quentin alexandre >;
  my @rounds =
    gather for ^@players -> \p
      { take @players[p,$_] for p^..^@players };
  for @rounds -> ( $a, $b ) { "$a vs $b".say }

> say .join(' vs ') for @players.combinations(2);
> # See https://docs.perl6.org/routine/combinations

i knew there will be something like that ... but didn't find it!
thank you very much.

regards
marc
0
eiro
7/10/2019 5:17:14 PM

> On Jul 10, 2019, at 12:17 PM, Marc Chantreux <eiro@phear.org> wrote:
=E2=80=94snip--
>  my @rounds =3D
>    gather for ^@players -> \p
>      { take @players[p,$_] for p^..^@players };

Cool.
Yet another way to do it:
	my @rounds =3D @players.kv.map: -> \k1, \p1 {
	  |@players.skip(1 + k1).map: ->      \p2 { p1, p2 }
	}
I mention it because I had not though to use .skip for this purpose =
until today.

> thank you very much.

You are very welcome.

=E2=80=94=20
Bruce Gray (Util of PerlMonks)=
0
robertbrucegray3
7/10/2019 7:57:18 PM
Reply: