Dynamic Languages and joining arrays
I’ve been spending a fair amount of time recently trying to choose my Language of the year for 2009. I’ve always been a dynamic language fan (yes, I know this means I should be looking further afield for the next one) and I was surprised at how different even such a common task as joining all the elements of an array together, using a given separator, looks between them.
First let’s look at the big three, including perl, my current favourite.
# perl $ perl -d -e 1; DB<1> my @names = qw( A B C); DB<2> print join(" : ", @names), "\n"; A : B : C # python >>> names = ['A', 'B', 'C' ] >>> " : ".join(names) 'A : B : C' # ruby irb(main):009:0> names = [ 'A', 'B', 'C' ] => ["A", "B", "C"] irb(main):010:0> names.join(' : ') => "A : B : C"
The perl approach is very procedural (ignore the use of the debugger as perl doesn’t come with an excellent REPL in the core like the other two) and is the one I’m most familiar with so it’s hard for me to be too critical about it. If you like OO then it’s not for you.
Next we have Python, which is really growing on me as a language - apart from in this case. Putting the separator first and passing the list in as a parameter just feels very wrong and is the exact opposite of the ruby version, which I much prefer. To me the ruby approach of operating on the array is the most natural version and sits well in my head. As a small ‘bonus’ I also looked at the PHP equivalent -
# PHP $array = array('A', 'B', 'C'); echo implode(" : ", $array);
This is close enough to the perl version that I can’t really object to it, other than to (rhetorically) ask why the hell it’s called ‘implode’?
I guess all I can say in summary is round one to ruby.