Wednesday, April 3, 2013

Scala or Ruby - Which Is Most Sexy?

In computer science, the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. The syntax of a language defines its surface form.

So, when we ask, "Which has the best syntax, Scala or Ruby?," we're really asking, "Which code looks better?" Good looking code is Sexy Code.

I'll try to be objective.

I'll add to the collection of examples below over time. So, keep coming back to this blog post to see which language wins the most SEXY Award points.

Your opinions are welcome.

Some people think fat is beautiful, but that's generally not the case when it comes to code.

Let's start off with showing how each language handles converting two arrays into one a hash, i.e., an array of key/value pairs.

Convert Two Arrays to a Hash

Ruby

>> keys = [1, 2, 3] 
=> [1, 2, 3] 
>> vals = ['A', 'B', 'C'] 
=> ["A", "B", "C"] 
>> hash = Hash[keys.zip(vals)] 
=> {1=>"A", 2=>"B", 3=>"C"}

Scala

scala> val keys = Array(1, 2, 3) 
keys: Array[Int] = Array(1, 2, 3) 
scala> val values = Array("A", "B", "C") 
values: Array[java.lang.String] = Array(A, B, C) 
scala> val map = keys.zip(values).toMap 
map: scala.collection.immutable.Map[Int,java.lang.String] = Map(1 -> A, 2 -> B, 3 -> C)

Winner: Ruby

Ruby is the clear winner, based on KISS (Keep It Simple, Stupid). It takes less typing to accomplish the same goal. Less code means less to type and less to maintain.  In this example, Ruby is sexier than Scala.




Find Largest Value in Array

Ruby


[1,2,3].max
=> 3


Ruby, Similar to Scala (not great)

>> [1, 3, 2].reduce(0) {|max, value| max = value if value > max; max}
=> 3


Scala


scala> List(1, 3, 2).reduceLeft ( _ max _ )
res0: Int = 3


Winner: Ruby



Conclusion

Which ever language (Scala or Ruby) has the most, wins.

Totals
Ruby2
Scala0

Most SEXY Award



Winner: Ruby






Sponsor Ads(Please visit one if you liked this article. Thanks!)

6 comments:

  1. Very nice. Except you can find the largest item in an array with Ruby much, much more concisely:

    [1,2,3].max => 3

    ReplyDelete
  2. Yes, [1,2,3].max is clearly infinitely more sexy than List(1,2,3).max

    ReplyDelete
  3. And for that matter, this is also horribly ugly:

    val a = Array(1,2,3)
    val b = Array("A", "B", "C")

    Map(a zip b: _*)

    Clearly inferior to Ruby on an unbiased sex scale.

    ReplyDelete
  4. Wonderful article, thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here. Marijuana online

    ReplyDelete
  5. The quick paced development of innovation and how it influences our lives implies that Science Technology Engineering and Mathematics qualified individuals have an edge in the activity showcase. From coding and programming assets similarly, coding visionaries can utilize such abilities to make projects, programming and applications that could change the world, or in any event help other people.

    ReplyDelete