in reply to Module Bloat and the Best Solution

Something nobody else seems to have picked up on yet is your assertion that both examples are 2 lines. It's true, but only until you want to uniquify more than one list!

Compare

my %tmp = map { $_ => 1} @foo; my @uniq_foo = sort keys %tmp; %tmp = map { $_ => 1} @bar; my @uniq_bar = sort keys %tmp; %tmp = map { $_ => 1} @baz; my @uniq_baz = sort keys %tmp;

with

use List::MoreUtils; my @uniq_foo = uniq @foo; my @uniq_bar = uniq @bar; my @uniq_baz = uniq @baz;

Four lines instead of six, and it's getting a lot harder to argue that the two approaches are equally readable.

Yes, you could also define your own uniq function, but why bother when it's been done for you already? If you're worried about people who don't have List::MoreUtils, then just copy the existing uniq function out of that into your own code (license permitting). You'll still be reusing ready-made, ready-tested CPAN code, and that's still usually better than reinventing the wheel.

Replies are listed 'Best First'.
Re^2: Module Bloat and the Best Solution
by BrowserUk (Patriarch) on Nov 10, 2007 at 11:49 UTC
Re^2: Module Bloat and the Best Solution
by blazar (Canon) on Nov 10, 2007 at 12:42 UTC
    my %tmp = map { $_ => 1} @foo; my @uniq_foo = sort keys %tmp;

    I personally believe that you can avoid the intermediate step:

    my @uniq_foo = sort keys %{ {map { $_ => 1} @foo} };

    Yes, it's a dirty trick! :)

    And as far as your example with three arrays and the respective uniq'd incarnations is concerned, whichever way you choose, I would go with a HoA instead:

    my %uniq = map { my %tmp = map { $_ => 1} @$_; $_ => [sort keys %tmp]; } keys %arrays;

    or

    my %uniq = map { $_ => [sort keys %{ {map { $_ => 1} @$_} }]; } keys %arrays;

    or

    my %uniq = map { my %saw; $_ => [map !$saw{$_}++, @$_]; } keys %arrays;

    or

    my %uniq = map { $_ => [uniq @$_] } keys %arrays;
Re^2: Module Bloat and the Best Solution
by KurtSchwind (Chaplain) on Nov 15, 2007 at 17:39 UTC

    Not to pick-nits, but if you really want to do a line count compare shouldn't you include the lines in the module? After all, you are including them.

    Also, you can just write your own sub and then every subsequent need would only be 1 line as well.

    Why write your own? Because it's a trivial solution and you don't have to download yet another module for it. Then again, that's why I started this thread. To discuss when it makes sense to download another module.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.