My Scala Days [Week 2] and [Week 3] Condensed

Most of the materials here are excerpts from vaious resources that I have come across online as well as from ebooks. Some of the sentences are described as they are. I will try to keep all the references but please excuse me if I have missed some soruces.

I realized that I have come up with a soup of random excerpts. I will try to clean this mess up

On Auxillary Constructors #

Every constructor invocation in Scala will end up eventually calling the primary constructor of the class. The primary constructor is thus the single point of entry of a class.

More Learning Resources #

The Neophyte’s Guide to Scala is a pretty good one to start looking into not-so-beginner-level text

Good things to take from it as far as I have learnt:

I started reading Scala by example. It is such a great companion for Odersky’s course on Coursera.

If you are doing Coursera, I suggest you do each lesson twice. Things get clearer on the second time.

One thing that I got to know is that I cannot make sbt to work with Java8 for sbt. It sucks and well I have to change back to Java7. The way to do is:

java -version
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
java -version

One thing to learn case classes is to do Case Class by Example

The greatest compilation of Scala libraries. Awesome Scala

The best thing that I did during this week was filling up blanks in Scala Exercises. You will get it when you see it. It is pretty cool to learn in this way.

I have to revisit Scala Exercises for:

val xs1 = Set(3, 2, 1, 4, 5, 6, 7)
val ys1 = Set(7, 2, 1, 4, 5, 6, 3)
(xs1 sameElements ys1) //true

val xs1 = Set(1, 2, 3)
val ys1 = Set(3, 2, 1)
(xs1 sameElements ys1) //false
val s = 90
"%d days left to be awesome" format s-10 // "80 days left to be awesome"
"%d days left to be %s" format(s, "hoola") // "80 days left to be hoola"
def patternEquals(i: Int, j: Int) = j match {
  case `i` => true
  case _ => false
}
patternEquals(3, 3) should be()
patternEquals(7, 9) should be()
patternEquals(9, 9) should be()

I have to look into Scalania

This tutorial explains Akka in very simple terms and give good examples where you can learn a lot.
Concurrency and Fault Tolerance Made Easy: An Akka Tutorial with Examples

This shows a way to write a simple chat server with actors
This

While I am doing Scala Exercives, I realize that I have to learn more about:
Learn more about

One take for me for this week is that I have to be aware of the fact that vals and vars and ruin can ruin my day. I was so much into functional programming and immutables states that I even make val to variables that I have to mutate.

Implicit Conversions #

If you have defined Rational class and then you implemented some overloaded methods for additions and subtractions with Rationals and Integerss, you can make something like: (provided that you have a auxillary constructor which passes the denominator as 1 when there is just a numerator when the object is instantiated)

def +(that: Rational):Rational =
new Rational(this.numer * that.denom + this.denom * that.numer, this.denom * that. numer)
def +(that: Int):Rational =
this + new Rational(that)

val a = new Rational(3, 8)
a + 2 //You will get a Raional as a result

But you can’t do this 2 + a. Since it is a method call on the number 2, which is an Integer. But the Int class contains no multiplicatoin method which takes a Rational argument.

implicit def intToRational(x: Int) = new Rational(x)

Now you can do 2 * a.

Control Structures #

if-else expression in a more functional style:

val filename =
  if (!args.isEmpty)
  args(0)
else
  "default.txt"

I might have already said this before or you might have heard enough of this but I have to emphasize on the importance of using vals over vars.

Advanatages are:

Exercises #

Here are some exercises that I want you guys to try out:

Provided that you have

val tokens = List("The", "Program", "Halted")
val tags = List("DT", "NN", "VB")

Make a String which is:
“The/DT Program/NN Halted/VB”

On Type inference #

Why can’t Scala infer the method argument types?

The type inference algorithm does local type inference, which means it doesn’t work globally overy the whole program, but locally within certain scopes. Here, it can'te tell what types the arguments must have, but it is able to infer the type of the method’s returned value in most cases, because it see the whole function body. Recursive functions are one exception where the execution scope extends beyond the scope of the body, so the return type must be declared.

 
0
Kudos
 
0
Kudos

Now read this

[You give this a name yourself]

Why is the value of a bean sprout a lot less than that of a diamond? You might think that I am nuts for such a comparison or you might think that I am going to say something bat-shit insane by starting out like this or you might think... Continue →