It’s just data

Perl vs Python vs Ruby

Taking a look at Ruby (the language), and I find that there are a number of familiar concepts in unfamiliar places.  Following are equivalent snippets in Perl, Python, and Ruby, respectively:

print join(' ',map {uc} @ARGV) . "\n";
print ' '.join(map(string.upper,sys.argv[1:]))
puts ARGV.map{|arg| arg.upcase}.join(' ')

Sam, IE 6 really doesn't like this post (and perhaps others?) -- could you please take a look?

Posted by Ziv Caspi at

Perhaps because it doesn't validate?  PRE can not contain UL.

Posted by Mark at

I'm seeing oddness as well - The text disappears unless I select it.  I get this all the time on this site:

http://www.prospect.org/weblog/

Maybe there's some kind of similarity in HTML and/or script usage?

Message from James Robertson at


That's funny, I'm viewing this from lynx right now and it looks fine.

Posted by Gordon Weakliem at

print join(' ',map uc, @ARGV), "\n";

Posted by anonymous at

I removed the list, and the page now validates.  It still displays oddly with IE6.

Here is a list of other pages where I use similar constructs.

Posted by Sam Ruby at

Amusingly, every time I Google for XML/SOAP/RDF/etc related stuff for Ruby, I find I have to put "-sam" in the query.

Posted by Charles Miller at

print strtoupper(join(' ', $argv)) . "\n";

PHP doesn't really map, but luckily this example doesn't need it. (Yea. PHP!)

Posted by Adam Trachtenberg at

PHP does map, and in any case, I think uppercasing was incidental.

Posted by Keith at

IE should be forbidden. It's just crap. I had the same problem with some special formatting for code tags. Worked perfectly in Firebird, but looked like shit in IE.

My blog looks perfect in Firebird (fixed top DIV, transparent PNG etc), but it's a mess in IE. As long as the content is still readable, I don't care. I guess this way I can encourage people to switch from IE to [any-other-browser].

Posted by Jonas Galvez at

Charles: sorry about that.

Adam: PHP would also require an array-shift to be equivalent.

Posted by Sam Ruby at

If you want to golf the perl version...

print uc join$",@ARGV,"\n";

No need to map, just uc the output from join. Use $", which defaults to a space. Remove unnecessary spaces and brackets. There might be an even shorter way, who knows!

Posted by Brian at

more golf...

print uc"@ARGV\n";

ah yes, array interpolation.

Posted by Brian at

Sam, I'd slice rather than shift:

echo join(' ', array_map(strtoupper, array_slice($argv, 1))), "\n";

This will be hard to beat for Brian's golf:

print "\U@ARGV\n";

Anyway... If I'm not mistaken Sam's point was about the concepts, not the particular syntax used.

Posted by Arien at

Yes, PRE can not contain UL.

Posted by Andrew Zubritsky at

print ' '.join(sys.argv[1:]).upper()

Posted by Mark at

Arien: Hey, I didn't start it. :)

Nice technique, but you didn't gain any strokes on me (tied at 18 chars a piece). =D

Posted by Brian at

The first time I heard about map and join was in Lisp stuff, a lot of time ago, both in scheme and Common Lisp variants.

Smalltalk came (to my awareness) later with the same concepts.

I think this is a pattern that will emerge in any language in which you have functions as first order objects.

Speaking about commonalities, parrot is being designed as a VM to run the languages you quote plus TCL (perl primarily), and it already has partial support for Scheme code.  ;-)

Posted by Santiago Gala at

In my Common Lisp implementation:

(princ (join " " (mapcar #'string-upcase args)))

Posted by Patrick Collison at

The problem this site has with displaying on IE6 is due to the static navigation bar on the right. IE6 does not support the position “fixed” tag. IE7 on the other hand does support it so upgrading to IE7 should solve your problems viewing this page.

Posted by anonymous at

Add your comment