20 September 2009

Scala and idioms

From the article : Is Scala really more complicated than Java? The same program can be written in Scala with different idioms:

Easy way:

val name = "Dick Wall"
Happy Birthday to you
Happy Birthday to you
Happy Birthday dear """ + name + """
Happy Birthday do you

for (n <- 1 to 4) {   
print("Happy Birthday")
if (n == 3)
print(" dear XXX")
print(" to you")


(1 to 4).map {
i => "Happy Birthday %s".format(if (i == 3) "Dear XXX" else "To You")
}.foreach println


(List.make(4, "Happy birthday ")
zip (List.tabulate(4, {
case 2 => "dear friend"
case _ => "to you"}))
) map (Function.tupled(_+_)) mkString ("\n")

More freedom ? hope Scala will avoid to be like Perl ! Not everybody will be able to maintain a Scala program. But that is also true with Java or anything. The complexity can be everywhere anyway, it is just more or less visible.

Read also: It's not the languages, but their idioms that matter.
Post a Comment

Online Scientific Notebooks, Course, Visualization show and tell comics

Course, Visualization, Notebooks A long paper on the subject "The Scientific Paper Is Obsolete" (2018) https://www.theatlantic....