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

They use a <word → word frequency> mapping, and then sum the values. This gives the total number of words. The hash table is totally pointless.


Ooo, I can do that in Perl 6 too.

    #!/usr/bin/env perl6

    sub MAIN(*@filename) {
        for @filename -> $file {
            my $bag = $file.IO.slurp.words.Bag;
            say "$file: { [+] $bag.values } words";
            CATCH { default { say "Unable to count words in $file"; } }
        }
    }


The author did have a point he wanted to put across with the hash table - "Obviously, a frequency distribution was not necessary for just the counts, but I did want to highlight basic autovivification"

ref: http://www.reddit.com/r/cpp/comments/369lcn/can_c_become_you...


That doesn't feel like a good reason. The author could easily have come up with a better task (eg. count the number of words with more than one occurrence). This allows you to approach the problem in the optimal way in each language.


You're totally right.




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

Search: