fREWdiculous!
27 Jan
First off let me say that I love ruby. Ruby more or less taught me functional programming, which I love. But I do think that perl6 (which you may think is vaporware) is better. I only post about features which I can use right now in rakudo. With that said we shall move onward.
Update: the rest of this post, although still correct, is flawed. See comments for the Correct Ruby solution
Fjord asked me about how to iterate over two lists at the same time in perl6. I have only had to do this a couple of times and I usually just end up doing a ghetto c-style for loop. In perl6 there is a better way. Check it!
Perl6:
1 2 3 | my @a = 1,2,3; my @b = 4,5,6; for @a Z @b { say "$^a $^b" } |
prints:
1 4 2 5 3 6
You may think, “fREW, ruby can do this and it does it exactly the same, if not better!”
Ruby1.8:
1 2 3 | a = [1,2,3] b = [4,5,6] a.zip(b).each { puts "#{x[0]} #{x[1]}" } |
Do you notice the subtle difference? In ruby we get [[1,4],[2,5],[3,6]] vs perl’s (1,4,2,5,3,6).
That may not seem like a big deal, but what if you want to iterate over three lists? Here’s perl6:
1 2 3 4 | my @a = 1,2,3; my @b = 4,5,6; my @c = 7,8,9; for @a Z @b Z @c { say "$^a $^b $^c" } |
prints:
1 4 7 2 5 8 3 6 9
and Ruby:
1 2 3 4 5 6 | a = [1,2,3] b = [4,5,6] c = [7,8,9] a.zip(b).zip(c).each { puts "#{x[0][0]} #{x[0][1]} #{x[1]}" } |
That’s a drag! Anyway, I once read that there is a way to do this nicely in ruby, but I never could figure it out. I’d say the perl6 solution here is much nicer. Can someone prove me wrong?
3 Responses for "Ruby1.8 vs. Perl6"
I just looked at this real fast. Maybe I misunderstand what you are after, but I think this may be what you are looking for in ruby:
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
a.zip(b,c) do |x,y,z|
puts “#{x} #{y} #{y}”
end
## OR
a.zip(b,c) do |group|
puts “#{group[0]} #{group[1]} #{group[2]}”
end
I am interested to see what Perl6 can do
@John: I stand corrected! Your first solution is perfect!
> I am interested to see what Perl6 can do
same thing …
my @a = 1, 2, 3;
my @b = 4, 5, 6;
my @c = 7, 8, 9;
for @a Z @b Z @c -> $x, $y, $z { say “$x $y $z }
Leave a reply