[tpm] Dealing with nested map { grep {} }

Richard vzshzn at gmail.com
Tue Sep 26 11:45:46 PDT 2017


Does the game always involve exactly two players? If so, it might be
clearest to just define %OPPONENT without a loop or map.

On Tue, Sep 26, 2017 at 11:17 AM, Shlomi Fish <shlomif at shlomifish.org>
wrote:

> Hi Tom,
>
> On Tue, 26 Sep 2017 13:53:25 -0400
> Tom Legrady <legrady at gmail.com> wrote:
>
> > As an exercise, I'm implementing a RosettaCode challenge for a game
> > between human and computer.
> >
> >      const my $PLAYERS => qw( human computer );
> >
> > I want to automatically access the appropriate opponent for each player,
> > which allows core game logic of
> >
> >      while ( $no_one_won ) {
> >          for my $player ( @PLAYERS ) {
> >              $self->player( $player )->take_turn( $OPPONENT{$player} );
> >          }
> >      }
> >
> > But to initialize %OPPONENT, I would need to nest a grep{} inside a
> > map{}, which leads to two instances of $_. Now perhaps the specification
> > of the opponent should be part of the game object or of a player object,
> > but I'm still curious how to resolve this problem in a single stage. Now
> > obviously you can't compare $_ with $_ ...
> >
> >      const my %OPPONENT => map { $_ => grep { $_ ne $_ } @PLAYERS }
> @PLAYERS
> >
> > If it weren't a const object, we could do this in stages, though it's
> > verbose and ugly:
> >
> >      my %OPPONENT;
> >      for my $player ( @PLAYERS ) {
> >          $OPPONENT{$player} = grep { $_ ne $player } @PLAYERS
> >      }
> >
> > But in the case of 'const my %OPPONENT', it's an error to attempt to
> > vivify a key after the object has become restricted.
> >
> > It worked fine if I replaced the inner grep{} with a subroutine call,
> > not_me(), which would perform the grep away from the map{}. But then it
> > occurred to me to perform a variable assignment within the map{}. Once
> > we assign $a, we can safely redefined $_.
> >
> >      const my %OPPONENT => map { my $a = $_; $a => grep { $_ ne $a}
> > @PLAYERS } @PLAYERS
> >
>
> this seems like a good solution but you should not use $a as the name of a
> lexical var. See:
>
> http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore
>
> http://perl-begin.org/tutorials/bad-elements/#vars-a-and-b
>
>
>
> > Tom
> > _______________________________________________
> > toronto-pm mailing list
> > toronto-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/toronto-pm
>
>
>
> --
> -----------------------------------------------------------------
> Shlomi Fish       http://www.shlomifish.org/
> My Aphorisms - http://www.shlomifish.org/humour.html
>
>  * rindolf demands equal rights for years, minutes, hours and days to also
> get
>    YAAKOV’S GREAT HUGE LOVE.
>http://www.shlomifish.org/humour/fortunes/sharp-perl.html
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20170926/596bbfa9/attachment.html>


More information about the toronto-pm mailing list