- // standard if
- if(1 > 2) -1 else 0
- // since blocks return a value you can use a block within the if statement
- // (not sure when you would want to but...)
- if ({ val x = 1
- val y = 2
- x == y }) 1 else 2
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.
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).
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).
- object State extends Enumeration {
- final val DIRTY, CLEAN, CONFLICT = Value
- }
- if( !pom.file.exists ){
- DIRTY;
- }else if (checksum.file.exists) {
- val sha = new String(MessageDigest.getInstance("SHA").digest( pom.slurp.getBytes ))
- if( sha == checksum.slurp ) CLEAN
- else DIRTY
- } else {
- CONFLICT
- }
- pomFile(massFile) match {
- case (pom, checksum) if (!pom.file.exists) => DIRTY
- case (pom, checksum) if (!checksum.file.exists) => CONFLICT
- case (pom, checksum) => {
- val sha = new String(MessageDigest.getInstance("SHA").digest( pom.slurp.getBytes ))
- if( sha == checksum.slurp ) CLEAN
- else DIRTY
- }
- }
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.
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.
- scala> var i = 0;
- i: Int = 0
- scala> while( i<3 ){
- | println( i )
- | i += 1
- | }
- 0
- 1
- 2
- scala> i = 0
- i: Int = 0
- scala> do {
- | println( i )
- | i += 1
- | } while (i<3)
- 0
- 1
- 2
- scala> for(j <- 0 until 3) println (j)
- 0
- 1
- 2
- scala> if (i<3)
- more
- scala> val result = if (i<3)
- result: Int = 10
- scala> println (result)
- 10
- scala> if (i>10) println(1)
- scala> if (i<10)
- 1
- // Note that the return value is (). You can only get a meaningful return value if there is an else-clause.
- scala> val r = if (i<10)>
- r: Unit = ()
- scala> println(r)
- ()
Monday, August 17, 2009
Return values
As with most functional languages, most control structures ( if, for, try ) return values. The common java idiom:
can be replaced by
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:
- String name=null;
- if( xxx ) name="yyy";
- else name="zzz";
can be replaced by
- 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:
- scala> val name = if( 1==2 ) "Jesse" else "Mauricio"
- name: java.lang.String = Mauricio
- scala> println(name)
- Mauricio
- scala> val collection = for( i <- 1 to 100; if(i%20 == 3) ) yield i
- collection: Seq.Projection[Int] = RangeFM(3, 23, 43, 63, 83)
- scala> collection.foreach( i => print( i +" ") )
- 3 23 43 63 83
- scala> val someObj:AnyRef = "Hello"
- someObj: AnyRef = Hello
- scala> val choice = someObj match {
- | case _:java.io.File => "File"
- | case _:String => "String"
- | case _ => "Dunno"
- | }
- choice: java.lang.String = String
- scala> val result = try {
- | "two".toInt
- | }catch{
- | case e:NumberFormatException => -1
- | case _ => 0
- | }
- result: Int = -1
- scala> var i=0
- i: Int = 0
- // while and do-while do not have return values
- scala> while( i<4 ){
- | "22"
- | i += 2
- | }
- scala> println( if(i>0) "great" else "less" )
- great
- // code blocks return the last statement
- scala> val m = {
- | val x = 1
- | x + 2
- | }
- m: Int = 3
Subscribe to:
Posts (Atom)