Seven Languages in Seven Weeks: Scala Day 2

I missed the opportunity to give my thoughts on Scala in my first post. A good friend of mine reminded me of this in a comment (btw thanks for the comments) so before giving today’s answers, I will briefly talk about Scala.

First of all Scala is a language that bridges two paradigms, OOP and functional programming. In this way it’s kind of like C++, which carried all C (a structured programming language) while adding object oriented concepts in the mix. You could write C code in a cpp file, compile it with a C++ compiler and for the most part everything was good.

You could say that Scala is a middle ground between object oriented languages like Java and purely functional languages like Haskell or Erlang.

From my point of view, the biggest thing about Scala is that it runs on the JVM and that with it, you can use existing Java libraries. The JVM is already installed on millions of machine and is very mature. As for reusing existing Java libraries, this is a big draw. New languages often suffer from an initial lack of libraries, plus having the possibility to use your company’s existing Java libraries might help with enterprise adoption.

I’m not in a good position to judge the language as my experience with it is limited. What I can give you is my initial impressions. These were pretty mixed. I found some parts of the syntax, for example arrays, to be a little kludgy. I also found the official documentation to be lacking. While the online documentation has a wide coverage, it often wasn’t deep enough, plus the web interface was confusing at first.

With this short introduction out of the way, here are the questions for Scala day 2.

Using foldLeft, compute the size of a list of strings.

val list = List("test", "tesla", "toasty")

val result = list.foldLeft(0)((sum, value) => sum + value.size)
println(result)

Write a trait named Censor that will replace the curse words “Shoot” and “Darn” with “Pucky” and “Beans”. The curse words and their censored version must be stored in a map.

trait Censor 
{
	val curseWords = Map("Shoot" -> "Pucky", "Darn" -> "Beans")

	def replace(input: List[String])
	{
		input.foreach(word => if (curseWords.contains(word))
								{
									print(curseWords(word))
								}
							  else
								{
									print(word)
								})
	}
}

class MyClass extends Censor

val my = new MyClass()
val phrase = List("Hot", "Darn", "Shoot", "Me", "Down")

my.replace(phrase)
println

For this one I coded a method that replaces words from a list of string. I assumed the method receives the string already tokenised.

The final question is to change the censor trait so that it can load it’s curse words and their replacements from a file. So here it is:

trait Censor 
{
	var curseWords = Map[String, String]()

	def loadCurseWords(fileName: String)
	{
		val source = io.Source.fromFile(fileName) 		
		val lines = source.getLines
		
		lines.foreach 
		{ 
			line =>
			val Array(key, value) = line.split(",")
			curseWords += key -> value.take(value.length - 1)
		}		 
	}
	
	def replace(input: List[String])
	{
		input.foreach(word => if (curseWords.contains(word))
								{
									print(curseWords(word))
								}
							  else
								{
									print(word)
								})
	}
}

class MyClass extends Censor

val my = new MyClass()
val phrase = List("Hot", "Darn", "Shoot", "Me", "Down")

my.loadCurseWords("censor.txt")
my.replace(phrase)
println

And finally here is the file:

Shoot,Yips
Darn,Drazzt

A trait has a similar role to an interface in Java or C#. The big difference with interfaces is that you can give an implementation for the methods defined in the trait.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s