Not as succinct as some of the other answers but possibly of interest.
$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&@); @a = transform { ( ++ $_ ) % 10 } @a; say qq{@a}; sub transform (&@) { map $_[ 0 ]->( $_ ), @_[ 1 .. $#_ ] }' 2 3 4 5 6 7 8 9 0 1 $
Update: Much more concise if you make the transform subroutine operate on $_ by default.
$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&); transform { ( ++ $_ ) % 10 } for @a; say qq{@a}; sub transform (&) { $_[ 0 ]->( $_ ) }' 2 3 4 5 6 7 8 9 10 11 $
Update 2: Just noticed that isn't working properly so will have to investigate why :-(
Update 3: Got it!
$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&); transform { $_ = ( ++ $_ ) % 10 } for @a; say qq{@a}; sub transform (&) { $_[ 0 ]->( $_ ) }' 2 3 4 5 6 7 8 9 0 1 $
Cheers,
JohnGG
In reply to Re: A better (ie.more concise) way to write this?
by johngg
in thread A better (ie.more concise) way to write this?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |