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

Or in perl:

  join "\n", map { $_->{description} } grep { $_->{description} ne "" } @mylist;
sorry, every time i see something like this, i just can't resist to post code in Perl. =)


I'll see your Perl5 and raise you some Perl6:

    @list.map(*.description).grep(?*).join("\n")


That can't be Perl, I actually understood it.


That is starting to look more like J or K than Perl. Is the Perl 6 team still considering syntax changes? If so, any thought on making map/grep/join operators?


map, grep and join are subs as well as methods. Perl6 is a multi-paradigm language and of course there's more than one way to do it - even when using the same operations.

Instead of chaining methods, one could also use

    # p5 style
    join "\n", grep ?*, map *.description, @list;

    # dataflow style
    @list ==> map *.description ==> grep ?* ==> join "\n";
    join "\n" <== grep ?* <== map *.description <== @list;


Perl6 allows you to define your own operators:

  $ perl6

  > sub infix:<J>   ($sep, @list) { @list.join($sep) }
  > sub prefix:<J,> (@list)       { @list.join(',')  }

  > ':' J <one two three four>;
  one:two:three:four

  > J,<five six seven eight>;
  five,six,seven,eight




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: