4 ms·
No need to wait for Perl 6 Absolutely :) use autobox::Core; # or perl5i::2 [1..10]->map(sub{ $_ ** 2 })->sum->say; Or if you like a double helping of s
by draegtun 15y ago
No need to wait for Perl 6
Absolutely :)
use autobox::Core; # or perl5i::2
[1..10]->map(sub{ $_ ** 2 })->sum->say;
Or if you like a double helping of sugar in your tea!
use Whatever;
[1..10]->map( &_ ** 2 )->sum->say;
- JadeNB 15y agoI thought `Whatever` was a joke, and had to go look it up on CPAN (http://search.cpan.org/~asg/Whatever-0.21/lib/Whatever.pm http://search.cpan.org/~asg/Whatever-0.21/lib/Whatever.pm). Not to belabour a joke, but the examples there suggest that the map should be map( $* ** 2 ) not map( &_ ** 2 )
- draegtun 15y agoNo it's quite a serious & well written module. Hopefully it will become part of perl5i - https://metacpan.org/module/perl5i https://metacpan.org/module/perl5i &_ is an explicit $_ You use $* or &* when you need to check $_[0] first. So &_ is more correct with general map but $* (and &*) will work also in this autobox example (in fact i had used it in my first edit but preferred aesthetics of &_ especially with previous usage of $* only being deprecated in 5.10). NB. Above in code just for aesthetics!