Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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;


I 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). Not to belabour a joke, but the examples there suggest that the map should be

    map( $* ** 2 )
not

    map( &_ ** 2 )


No it's quite a serious & well written module. Hopefully it will become part of 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!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: