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'}];
js
ReplyDeletethis.computers =
[{'name':'MacBook', 'color':'White'},
{'name':'MacBook Pro', 'color':'Silver'},
{'name':'iMac', 'color':'White'}];
-m
Clojure:
ReplyDelete(def computers [{:Name "MacBook"} {:Name "MacBook Pro" :Color "Silver"} {:Name "iMac" :Color "White"}])
This comment has been removed by the author.
ReplyDeleteActually I think the Ruby version is more similar to this Scala version, am I right?
ReplyDeletethis.computers = Array(
Map('Name -> "MacBook", 'Color->"White"),
Map('Name -> "MacBook Pro", 'Color->"Silver"),
Map('Name -> "iMac", 'Color->"White"))
You're not wrong. The original version was actually the objective c, which inspired me to write this because I found it comical.
ReplyDeleteIn 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).
No no! Not at all :)
ReplyDeleteI 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?)
What is the color of the first MacBook of Clojure example ?
ReplyDeleteActually you could change the Obj-C to:
ReplyDeleteNSDictionary *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]];
I accidentally left out the color of MacBook.
ReplyDeleteF# :
ReplyDeletelet this.computers <- [|
Map[("Name", "MacBook"); ("Color","White")];
Map[("Name", "MacBook Pro"); ("Color","Silver")];
Map[("Name", "iMac"); ("Color","White")] |]
Groovy:
ReplyDeletethis.computers = [
[Name: "MacBook", Color: "White"],
[Name: "MacBook Pro", Color: "Silver"],
[Name: "iMac", Color: "White"]]
The hip languages all use the same ammount of lines. Cool!
ReplyDeleteF# uses semi-colons for delimiters and not commas?
ReplyDeletenice comparison, gives feel of a unified world :)
ReplyDeletePython,
ReplyDeletecomputers = [
{"Name":"MacBook","Color":"White"},
{"Name":"MacBook Pro","Color":"Silver"},
{"Name":"iMac","Color":"White"}]
of course you have to correctly indent the python code :-D
ReplyDelete>> F# uses semi-colons for delimiters and not commas?
ReplyDeleteYes. Commas for tuples, semi-colons for list and array items
Haskell:
ReplyDeleteimport Data.Map
computers = [fromList [("name", "MacBook"), ("color", "white")],
fromList [("name", "MacBook Pro"), ("color", "silver")],
fromList [("name", "iMac"), ("color", "white")]]
or why not change the obj-c to
ReplyDeleteself.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.
its not pointless. first, its supposed to be funny. since its funny to me, point achieved.
ReplyDeletesecond, 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.
dictionaryWithObjectsAndKeys: and arrayWithObjects: both return autoreleased objects. Releasing them would mess up the retain counts, correct?
ReplyDelete@Jack: I think they are professional technical writers who have a shallow knowledge of the language and write books with broken code. And yes Objective-C is verbose :)
ReplyDelete@Rob: I also think that the releases are unnecessary and could lead to a BAD ACCESS error.
This comment has been removed by the author.
ReplyDeleteI'll be necromancer of the year for this. But anyway, with Apple now using LLVM 4.0 and introducing what they call "Modern Objective-C", this is how the example looks like in Objective-C instead: (@[] array literal, @{key :value} dictionary literal.)
ReplyDeleteself.computers = @[
@{ @"Name" : @"MacBook", @"Color" : @"White" },
@{ @"Name" : @"MacBook Pro", @"Color" : @"Silver" },
@{ @"Name" : @"iMac", @"Color" : @"White" }];
As many lines as Ruby/Scala example ;) // ObjC and Ruby developer