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

Yitzchak Scott-Thoennes sthoenna at efn.org
Sun Jun 22 09:38:03 PDT 2008

On Sun, June 22, 2008 2:06 am, Shlomi Fish wrote:
> On Saturday 21 June 2008, Adam Fine wrote:
>> The natural Ruby equivalent to your Perl solution would be:
>> h = {}
>> a.map {|k| h[k] = true}

That's not equivalent - it's quite a different thing to be referring
to h in the implicit loop.  I'm curious to know if there really isn't
a ruby equivalent to my %h = map { $_ => 1 } @a;

> Yes, but this takes two statement. Cognitively, it is one operation and
> so I want to do it in a single statement.

Someone else commented that the declaration is a separate operation.
But I find being able to declare at point-of-first-use a really great
feature for encouraging limited variable scope.

>> 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?

Yes, if you are referring to the perl map.  But building a pairlist and
assigning it to a hash is simpler to read (once over the "hurdle" of
determining that the map is building a pairlist), IMO, than a loop that
is tweaking the hash on each iteration.

>> 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 :)
>> 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 :)

That may be clearer than the a.map above, but still less clear IMO than
the perl hash assignment.  If that is saying the ruby "for key" is
clearer than the perl "for my $i", how so? - I just don't get it.

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

Let's not get into that; any of the suggestions is fast enough
given reasonable constraints on the inputs.  And wth potentially
unbounded inputs, the iterative approaches become mandatory.

>> To sum up, we translated directly your solution to Ruby,

Nope.  See above.

>> and ended up
>> with cleaner code.

Nope.  See above.

>> Then we demonstrated what must be one of the clearest
>> solutions possible, implemented in Ruby.

Nope.  See above.

>> Then we translated the clear
>> solution back to Perl, and clarity was degraded.

Nope.  See above.

Some other perl constructs that I'd be curious to see ruby equivalents
of, where possible:

my %h; @h{@a} = (1) x @a;

my %h; @h{@a} = ();  ... if (exists $h{$foo}) { ... }

More information about the Perl mailing list