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'}];

24 comments:

  1. js

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

    -m

    ReplyDelete
  2. Clojure:

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

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. 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"))

    ReplyDelete
  5. 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).

    ReplyDelete
  6. 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?)

    ReplyDelete
  7. What is the color of the first MacBook of Clojure example ?

    ReplyDelete
  8. 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]];

    ReplyDelete
  9. I accidentally left out the color of MacBook.

    ReplyDelete
  10. F# :

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

    ReplyDelete
  11. Groovy:

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

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

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

    ReplyDelete
  14. nice comparison, gives feel of a unified world :)

    ReplyDelete
  15. Python,

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

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

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

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

    ReplyDelete
  19. 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.

    ReplyDelete
  20. 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.

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

    ReplyDelete
  22. @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 :)

    @Rob: I also think that the releases are unnecessary and could lead to a BAD ACCESS error.

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. I'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.)


    self.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

    ReplyDelete