On Tue, Sep 1, 2015 at 3:10 AM, Matija Papec <mpapec2...@yandex.com> wrote:

> Not pretty, also you'll have to take care of -a switch,


S19 calls for -a and -F, surprised Rakudo doesn't have'em! Though from
later examples, the ".words" method is a fine substitute.

On Tue, Sep 1, 2015 at 11:03 AM, Jonathan Scott Duff <d...@pobox.com> wrote:

> If you're not married to the "key : value" format, you could use this:
>
>     scan +spam | perl6 -ne 'my %d; %d{.words[1]}++;  END { .say for sort
> %d }'
>

that format is fine. Coming from perl5 I was surprised that the "my %d"
declaration didn't clear out %d each time through the loop implied by "-n"!
In perl5, we would only get the last line in %d if the "my %d" were at the
start, have to use "state %d" instead. Later messages go into more detail
about that (I wrote this yesterday, and then, half the message got eaten)-:

Your later example can be golfed a bit using placeholders-

scan +spam|perl6 -ne 'my %d; %d{.words[1]}++;  END { for %(%d.sort).kv {
say "$^k : $^v" } }'

Still I wanted to get away from temp variables like %d altogether, to see
what features of perl6 make strict one-liners tenable. The state-like
behavior of "my" inside the -n loop helps.

I managed to write this functional-style one liner in perl5, took a while &
a reboot to get to this point:

perl -E 'sub {if (@_ == 1) {say "$_: $_[0]{$_}" for sort keys %{$_[0]}}
else {${$_[0]}{pop @_}++;goto __SUB__ }}->({},map {/\s(\S+)/} <>)'

and I still don't like it because it isn't purely functional. Do perl6's
Bag type and feed operators, or other features, open up a cleaner way? With
P6, there's more than more than one way to do things...


-y

Reply via email to