Monday, June 01, 2009

Comedy: Scala vs. Ruby vs. Objective C

 
Please submit your favorite (or least favorite) language!

Added: Java, Clojure, JavaScript

Scala


this.computers = Array(
Map("Name" -> "MacBook", "Color"->"White"),
Map("Name" -> "MacBook Pro", "Color"->"Silver"),
Map("Name" -> "iMac", "Color"->"White"))

Ruby


self.computers = [
{:Name=>"MacBook", :Color=>"White"},
{:Name=>"MacBook Pro", :Color=>"Silver"},
{:Name=>"iMac", :Color=>"White"}]

Objective C


NSDictionary *row1 =
[[NSDictionary alloc] initWithObjectsAndKeys:
@"MacBook", @"Name", @"White", @"Color", nil];
NSDictionary *row2 =
[[NSDictionary alloc] initWithObjectsAndKeys:
@"MacBook Pro", @"Name", @"Silver", @"Color", nil];
NSDictionary *row3 =
[[NSDictionary alloc] initWithObjectsAndKeys:
@"iMac", @"Name", @"White", @"Color", nil];

NSArray *array =
[[NSArray alloc] initWithObjects: row1, row2, row3, nil];

self.computers = array;

[row1 release];
[row2 release];
[row3 release];
[array release];

Java


Assuming this field:

Map<String, String>[] computers;

Then:

Map[] maps = {
new HashMap(){{
put("Name", "MacBook");
put("Color", "White");
}},
new HashMap(){{
put("Name", "MacBook Pro");
put("Color", "Silver");
}},
new HashMap(){{
put("Name", "iMac");
put("Color", "White");
}}
};
this.computers = maps;

Notice you can't use generics with the array creation, and, 4 space indenting! Yeah!

Clojure


(def computers [
{:Name "MacBook"}
{:Name "MacBook Pro" :Color "Silver"}
{:Name "iMac" :Color "White"}])

JavaScript


this.computers =
[{'name':'MacBook', 'color':'White'},
{'name':'MacBook Pro', 'color':'Silver'},
{'name':'iMac', 'color':'White'}];

21 comments:

fogus said...

js

this.computers =
[{'name':'MacBook', 'color':'White'},
{'name':'MacBook Pro', 'color':'Silver'},
{'name':'iMac', 'color':'White'}];

-m

Anonymous said...

Clojure:

(def computers [{:Name "MacBook"} {:Name "MacBook Pro" :Color "Silver"} {:Name "iMac" :Color "White"}])

Quintesse said...
This post has been removed by the author.
Quintesse said...

Actually I think the Ruby version is more similar to this Scala version, am I right?

this.computers = Array(
Map('Name -> "MacBook", 'Color->"White"),
Map('Name -> "MacBook Pro", 'Color->"Silver"),
Map('Name -> "iMac", 'Color->"White"))

Jack Cough said...

You're not wrong. The original version was actually the objective c, which inspired me to write this because I found it comical.

In Ruby, symbols for hash keys are far more common than in Scala, so I didn't use symbols for Scala. However, if you feel strongly about it, I'll change it (since I don't).

Quintesse said...

No no! Not at all :)

I was just wondering, my Ruby knowledge is close to zero and I've always wondered myself why and where to use symbols in Scala. This seems like a good example.... (although the advantages of using symbols here are not really vlear... maybe because this way you make it explicit that the keys all refer to the same thing?)

Anonymous said...

What is the color of the first MacBook of Clojure example ?

Martin Pilkington said...

Actually you could change the Obj-C to:

NSDictionary *row1 = [NSDictionary dictionaryWithObjectsAndKeys: @"MacBook", @"Name", @"White", @"Color", nil];
NSDictionary *row2 = [NSDictionary dictionaryWithObjectsAndKeys: @"MacBook Pro", @"Name", @"Silver", @"Color", nil];
NSDictionary *row3 = [NSDictionary dictionaryWithObjectsAndKeys: @"iMac", @"Name", @"White", @"Color", nil];

[self setComputers:[NSArray arrayWithObjects: row1, row2, row3, nil]];

Anonymous said...

I accidentally left out the color of MacBook.

mehdi.asgari said...

F# :

let this.computers <- [|
Map[("Name", "MacBook"); ("Color","White")];
Map[("Name", "MacBook Pro"); ("Color","Silver")];
Map[("Name", "iMac"); ("Color","White")] |]

Scot said...

Groovy:

this.computers = [
[Name: "MacBook", Color: "White"],
[Name: "MacBook Pro", Color: "Silver"],
[Name: "iMac", Color: "White"]]

Anonymous said...

The hip languages all use the same ammount of lines. Cool!

Jack Cough said...

F# uses semi-colons for delimiters and not commas?

Monis Iqbal said...

nice comparison, gives feel of a unified world :)

Victor Hogemann said...

Python,

computers = [
{"Name":"MacBook","Color":"White"},
{"Name":"MacBook Pro","Color":"Silver"},
{"Name":"iMac","Color":"White"}]

Victor Hogemann said...

of course you have to correctly indent the python code :-D

mehdi.asgari said...

>> F# uses semi-colons for delimiters and not commas?
Yes. Commas for tuples, semi-colons for list and array items

Anonymous said...

Haskell:
import Data.Map
computers = [fromList [("name", "MacBook"), ("color", "white")],
fromList [("name", "MacBook Pro"), ("color", "silver")],
fromList [("name", "iMac"), ("color", "white")]]

Anonymous said...

or why not change the obj-c to
self.computers = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys: @"MacBook", @"Name", @"White", @"Color", nil],
[NSDictionary dictionaryWithObjectsAndKeys: @"MacBook Pro", @"Name", @"Silver", @"Color", nil],
[NSDictionary dictionaryWithObjectsAndKeys: @"iMac", @"Name", @"White", @"Color", nil], nil]];

oh wait, because then this page would be pointless.

Jack Cough said...

its not pointless. first, its supposed to be funny. since its funny to me, point achieved.

second, i took that code directly from Beginning IPhone Development, which brings up a few questions. since there are clearly easier ways to do it in obj-c, why would they use the horrifying code instead? are they trying to scare people away? maybe obj-c just needs a good publicist.

Rob said...

dictionaryWithObjectsAndKeys: and arrayWithObjects: both return autoreleased objects. Releasing them would mess up the retain counts, correct?

Post a Comment