Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

Saturday, March 6, 2010

Blocks within if statements

his is another topic that examines the consistency of Scala. This topic examines blocks in if statements. It is related to Blocks within for comprehensions and Temporary Variables during object creation.
  1. // standard if
  2. if(1 > 2) -1 else 0
  3. // since blocks return a value you can use a block within the if statement 
  4. // (not sure when you would want to but...)
  5. if ({ val x = 1
  6.       val y = 2
  7.       x == y }) 1 else 2

Tuesday, September 1, 2009

Refactoring if to match

One of the refactorings I find myself doing quite often in Scala is converting large branching if trees into an easy to read match statement. The reasoning is that it is much easier to read a match statement because it is like reading bullet points rather than trying to follow the flow of information through a tree of if statements.

Before you complain, I realize there are other ways to refactor a tree of if-statements but I find the match statements the easiest to reason about and the quickest to write with the least amount of boiler plate.

Here is an example where I need to see if the file is dirty and needs to be regenerated, if it is clean or if some one has modified the generated file (which results in a CONFLICT).

  1. object State extends Enumeration {
  2.   final val DIRTY, CLEAN, CONFLICT = Value
  3. }
  4. if( !pom.file.exists ){
  5.   DIRTY;
  6. }elseif (checksum.file.exists) {
  7.   val sha = new String(MessageDigest.getInstance("SHA").digest( pom.slurp.getBytes ))
  8.   if( sha == checksum.slurp ) CLEAN
  9.   else DIRTY
  10. } else {
  11.   CONFLICT
  12. }
  13. pomFile(massFile) match {
  14.   case (pom, checksum) if (!pom.file.exists) => DIRTY
  15.   case (pom, checksum) if (!checksum.file.exists) => CONFLICT
  16.   case (pom, checksum) => {
  17.     val sha = new String(MessageDigest.getInstance("SHA").digest( pom.slurp.getBytes ))
  18.     if( sha == checksum.slurp ) CLEAN
  19.     else DIRTY
  20.   }
  21. }

Monday, August 31, 2009

Java vs Scala Control Structures

This topic is mainly for completeness. We will quickly cover the standard control structures you find in Java and see how they are the same or different in Scala.

The first thing to note is that in Scala 2.7 there is no break keyword. In Scala 2.8 there is a break control structure but it is slightly different than the Java break keyword. We will encounter that topic in a later lesson. The control structures I will quickly cover are: do-while, while, for and if.

For information about the Java case statement take a look at the several matching topics covered now and in the future.

Note: The Java ternary if statement does not exist in Scala instead the standard if statement is to be used. It is slightly more verbose but returns a value in the same way as a ternary if statement.
  1. scala>var i = 0;
  2. i: Int = 0
  3. scala>while( i<3 ){
  4.      | println( i )
  5.      | i += 1
  6.      | }
  7. 0
  8. 1
  9. 2
  10. scala> i = 0
  11. i: Int = 0
  12. scala> do {
  13.      | println( i )
  14.      | i += 1
  15.      | } while (i<3)
  16. 0
  17. 1
  18. 2
  19. scala>for(j <- 0 until 3) println (j)  
  20. 0
  21. 1
  22. 2
  23. scala>if (i<3)
  24. more
  25. scala>val result = if (i<3)
  26. result: Int = 10
  27. scala> println (result)
  28. 10
  29. scala>if (i>10) println(1)
  30. scala>if (i<10)
  31. 1
  32. // Note that the return value is ().  You can only get a meaningful return value if there is an else-clause.
  33. scala>val r = if (i<10)>
  34. r: Unit = ()
  35. scala> println(r)
  36. ()

Monday, August 17, 2009

Return values

As with most functional languages, most control structures ( if, for, try ) return values. The common java idiom:
  1. String name=null;
  2. if( xxx ) name="yyy";
  3. else name="zzz";

can be replaced by
  1. val name = if( xxx ) "yyy"; else"zzz";

The benefit (other than less boiler plate code) is that name can now be a val instead of a var.

Another other point about returns: The return keyword is not required when returning a value from methods or control structures. The last value is always the return value. This is why you will get an error if the last line in a method or control structure is an assignment.

Examples:
  1. scala>val name = if( 1==2 ) "Jesse"else"Mauricio"
  2. name: java.lang.String = Mauricio
  3. scala> println(name)
  4. Mauricio
  5. scala>val collection = for( i <- 1 to 100; if(i%20 == 3) ) yield i
  6. collection: Seq.Projection[Int] = RangeFM(3, 23, 43, 63, 83)
  7. scala> collection.foreach( i => print( i +" ") )
  8. 3 23 43 63 83
  9. scala>val someObj:AnyRef = "Hello"
  10. someObj: AnyRef = Hello
  11. scala>val choice = someObj match {
  12.      | case _:java.io.File => "File"
  13.      | case _:String => "String"
  14.      | case _ => "Dunno"
  15.      | }
  16. choice: java.lang.String = String
  17. scala>val result = try {
  18.      | "two".toInt
  19.      | }catch{
  20.      | case e:NumberFormatException => -1
  21.      | case _ => 0
  22.      | }
  23. result: Int = -1
  24. scala>var i=0
  25. i: Int = 0
  26. // while and do-while do not have return values
  27. scala>while( i<4 ){
  28.      | "22"
  29.      | i += 2
  30.      | }
  31. scala> println( if(i>0) "great"else"less" )
  32. great
  33. // code blocks return the last statement
  34. scala>val m = {
  35.      | val x = 1
  36.      | x + 2
  37.      | }
  38. m: Int = 3