Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: A better (ie.more concise) way to write this?

by johngg (Canon)
on Dec 14, 2013 at 12:47 UTC ( [id://1067134]=note: print w/replies, xml ) Need Help??


in reply to A better (ie.more concise) way to write this?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1067134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found