Right time to broaden the horizons. It's 2013 and I am going to start blogging about Scala which I am trying learn. I am going to start with for loops.
for(i <- 1 to 5){ println(i); }It is quite easy to figure out what is going on here, without even mentioning the word Scala. Hey it's just a for loop and yeah it iterates from 1 to 5 and there's probably some type inference going on - since Scala is statically typed. That's all fine, but, I find it useful when trying to learn a new language, to learn the language of that language. For anyone coming from a Java background the '<-' is certaily warrants some noun. This is called a generator. Why? Because it generates individual vales from a range which in this case is the 1 to 5 part. There is not much else interesting in this example except the intent of 2 spaces - Java programmers will be use to 4. So what else? Well there are two styles of for loops in Scala:foreach and for. The former is intended for a functional approach and I'll cover this is another post. The later is suited for imperative style. In fact the
for
and foreach
constructs are an excellent example of how Scala facilitates both imperative and functional progamming.
Give me more?
Sure, let's have at look at more tricks with for loops using the imperative approach.class ForLoopExample { def forExampleWithTo() { println(">>forExampleWithTo()"); for (i <-1 to 5) println("Iteration " + i) } def forExampleWithUntil() { println(">>forExampleWithUntil()"); for (i <-1 until 5) println("Iteration " + i) } def forExampleWithMultipleRanges() { println(">>forExampleWithMultipleRanges()"); for (i <- 1 to 2; j <- 4 to 5) { println("Value of i=" + i); println("Value of j=" + j); } } def forExampleWithFilter() { println(">>forExampleWithFilter"); for (i <- 1 to 5 if i % 2 == 0) { println("Filtered i=" + i); } } //storing results from for loop. def forExampleStoreValues() { println(">>forExampleStoreValues"); val retVal = for{i <- 1 to 5 if i % 2 == 0} yield i; println("retVal=" + retVal); retVal; } }So some points:
- There is no need to explictly make the class
ForLoopExample
public. This is because public is default access level in Scala. Where you said public in Java you say nothing in Scala. - The only difference between
forExampleWithTo
andforExampleWithUntil
is that one usesto
in its range and the other usesuntil
. In these examplesto
means 1,2,3,4,5 anduntil
means 1,2,3,4 - i.e. the last element is not included. forExampleWithMultipleRanges
shows how to iterate over multiple ranges. In addition, note the statement in the for loop are enclosed in{}
. The{}
are required when multiple statements are in each iteration. If there is only one statement they can be omitted.forExampleWithFilter
shows how to filter out values from the list.forExampleStoreValues
shows how to store the result of the iteration values from a for list.
Now how about invoking these for examples?
Sure.object MainRunner { def main(args: Array[String]){ println("Scala stuff!"); // println comes from Predef which definitions for anything inside a Scala compilation unit. runForExamples(); } def runForExamples() { val forLE = new ForLoopExample(); // No need to declare type. println("forExampleWith()=" + forLE.forExampleWithTo()); // println("forExampleWithUntil=" +forLE.forExampleWithUntil); //() brackets for method invocation not needed. println("forExampleWithFilter=" +forLE.forExampleWithFilter) // semi colans not needed to end lines println("forExampleWithMultipleRanges=" +forLE.forExampleWithMultipleRanges); println("forExampleStoreValues=" +forLE.forExampleStoreValues) } }And for some more salient points:
- Rather than MainRunner being declared as a class, it is declared as an Object. This means it is singleton.
- The main() method is similar to Java's
public static void main
. Except there is no need for public (it's the default). There is no need for static (we are in a singleton) and there is no need to declare the return type. You see Scala you get more code with less typing. - In some cases I omit the () from the method invocation. In cases when a method has no arguments Scala allows the omissions of the (). However, this notation should only be used when the method has no side effects i.e. the method does not change the state of anything - so I am only using it here to for purposed of illustration.
No comments:
Post a Comment