[Israel.pm] Perl vs. Ruby on Two Idioms

Shlomi Fish shlomif at iglu.org.il
Sun Jun 22 02:06:16 PDT 2008

On Saturday 21 June 2008, Adam Fine wrote:
> Hi Shlomi!
> I'm not sure why you chose the Array#inject solution for the first problem.
> It's one of the more obscure ways of achieving your stated goal.
> The natural Ruby equivalent to your Perl solution would be:
> h = {}
> a.map {|k| h[k] = true}

Yes, but this takes two statement. Cognitively, it is one operation and so I 
want to do it in a single statement.
> Even this is clearer than the Perl solution, of which it is a direct
> translation. 

I have no problem reading the Perl solution, and neither would any 
intermediate Perl programer. And doesn't the map call build a long list?

> But if this is a clarity contest, then here's the clearest 
> Ruby solution I could think of:
> h = {}
> for key in a
>   h[key] = true
> end

This uses a loop (which is probably slow) and is 4 lines long. It's not a 
clarity contest, and there is no problem in the clarity of the Perl program.

> It doesn't get much cleaner than that :)
> Anyone who ever programmed any modern dynamic language - Javascript, Lua,
> Python, PHP, Perl - understands immediately what this code does. On the
> other hand, one would need at least several days of specific Perl
> experience to understand what's going on here:
> my %h = (map { $_ => 1 } @a);

Right, but it's idiomatic Perl and you can ask or look it up, and it's the 
common idiom to do that.

> The direct Perl translation of my last solution is:
> my %h; for my $i (@a) { $h{$i} = 1; }
> It's pretty obvious which form is clearer :)

The former one to me, which is probably also faster.

> To sum up, we translated directly your solution to Ruby, and ended up with
> cleaner code. Then we demonstrated what must be one of the clearest
> solutions possible, implemented in Ruby. Then we translated the clear
> solution back to Perl, and clarity was degraded.
> I have nothing against Perl; it's a wonderful project that contributed a
> lot to the programming world. Perl 6 will hopefully continue to do so. But
> Perl 5 - all its merits notwithstanding - was never the cleanest or
> clearest of languages, while Ruby would be tough to beat in that category,
> even for the best of them :)

Why are people so obsessed with the "clarity" of such small scopes? While Perl 
Golf and Obfuscated Perl are very much non-clear, one can write clear code in 
Perl that will be obvious to people with minimal amount of experience. And if 
you don't have a solid amount of experience in any language, you're going to 
write buggy or even dangerous code (re: most PHP code out there).

My intention with the exercise was to show how one can idiomatically perform 
these two tasks.

However, see 
http://www.perl.org.il/pipermail/perl/2005-August/thread.html#7327 ("The Cost 
of Newlines") for a different POV.

> Hope everything is great with you, take care,

It is, yes.


	Shlomi Fish

> -Adam
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups
> "Israel.rb - the Israeli Ruby & Rails community" group. To post to this
> group, send email to israelrb at googlegroups.com
> To unsubscribe from this group, send email to
> israelrb-unsubscribe at googlegroups.com For more options, visit this group at
> http://groups.google.com/group/israelrb?hl=en
> -~----------~----~----~----~------~----~------~--~---

Shlomi Fish       http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://xrl.us/bjn8q

The bad thing about hardware is that it sometimes works and sometimes doesn't.
The good thing about software is that it's consistent: it always does not
work, and it always does not work in exactly the same way.

More information about the Perl mailing list