Showing posts with label control structure. Show all posts
Showing posts with label control structure. Show all posts

Tuesday, April 20, 2010

Breaks

Scala 2.8 added the break control flow option. It is not implemented as a special language feature. Rather it is simply implemented as an object/trait using standard Scala mechanisms. If you are interested in creating a control flow object similar to this look at the Defining Custom Control Structures post.

The Break functionality is works basically how you would expect:
  1. // Import the control flow methodsmethods
  2. scala> import util.control.Breaks._
  3. import util.control.Breaks._
  4. // pass a function to the breakable method
  5. scala> breakable {
  6.      | for (i <- 1 to 10 ) {
  7.      | if(i > 5) break  // call break when done
  8.      | println(i)
  9.      | }
  10.      | }
  11. 1
  12. 2
  13. 3
  14. 4
  15. 5

Pretty intuitive but beware, break only breaks out to the first enclosing breakable. Here is an example of the issue:
  1. scala> def loop(f : Int => Boolean) = breakable {
  2.      | for (i <- 1 to 300) if (f(i)) break else println(i)
  3.      | }
  4. loop: (f: (Int) => Boolean)Unit
  5. // This never ends because break is caught by breakable in the loop method
  6. scala> breakable {
  7.      | while(true) {
  8.      | loop{ i => break; true }
  9.      | }
  10.      | }

Fortunately the implementers provide an elegant way to handle these sorts of cases. The Breaks object extends the Breaks class. By instantiating other instances of Breaks it is possible to control which breaks capture
  1. scala> import scala.util.control._
  2. import scala.util.control._
  3. scala> 
  4. scala> def loop(f : Int => Boolean) = {
  5.      |   val Inner = new Breaks
  6.      |   Inner.breakable {
  7.      |     for (i <- 1 to 4) if (f(i)) Inner.break else println(i)
  8.      |   }
  9.      | }
  10. loop: (f: (Int) => Boolean)Unit
  11. scala> 
  12. scala> val Outer = new Breaks
  13. Outer: scala.util.control.Breaks = scala.util.control.Breaks@1ba4806
  14. scala> Outer.breakable {
  15.      |   while(true) {
  16.      |     loop{ i => if(i==4) Outer.break; false}
  17.      |   }
  18.      | }
  19. 1
  20. 2
  21. 3

Friday, February 5, 2010

Introducing Structural Types

Structural types allows one to declare types based on the methods the type has. For example you could define a method that takes a class containing a close method. This is fairy analogous to duck-typing in dynamic languages. Except that it is statically enforced.

The main example used here was from a comment on the Code Monkeyism Blog. The commenter further explains that this example is in fact from Beginning Scala chapter 4 (which I would like to read but have not yet had the time.)
  1. /*
  2. A can be any object that has a close method.  
  3. This is statically typed which makes some restrictions which are explained later
  4. */
  5. scala> def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B = 
  6.      | try {
  7.      |     f(param)
  8.      |   } finally {
  9.      |     try {
  10.      |        param.close()
  11.      |     } catch { case _ => () }
  12.      |   }
  13. using: [A <: AnyRef{def close(): Unit},B](param: => A)(f: (A) => B)B
  14. scala> using(new java.io.ByteArrayInputStream("hello world".getBytes)){ in => 
  15.      | io.Source.fromInputStream(in) foreach (print)                          
  16.      | }
  17. hello world
  18. scala> using(new java.io.ByteArrayInputStream("hello world".getBytes)){ in => 
  19.      | io.Source.fromInputStream(in) mkString ""                              
  20.      | }
  21. res8: String = hello world

That is extremely powerful and the consequences will be visited more in the future. But because structural typing is statically enforced it is not quite as flexible as dynamic language's version of duck typing. For example you cannot do:
  1. scala> val i:Any = new java.io.ByteArrayInputStream("hello world".getBytes)
  2. i: Any = java.io.ByteArrayInputStream@145a25f3
  3. /*
  4. This does not work because 'i' is defined as type Any.  Not a type that is closeable.  
  5. Casting can be used to get around this issue.  I will address that in its own post
  6. */
  7. scala> using(i){ in => 
  8.      | io.Source.fromInputStream(in) mkString ""
  9.      | }
  10. < console>:8: error: inferred type arguments [Any,B] do not conform to method using's type parameter bounds [A <: AnyRef{def close(): Unit},B]
  11.        using(i){ in =>

An alternative to the first using example is to use call by name to construct the closeable. The reason one might want to do that is because it allows currying of the method:
  1. scala> def using[A <: {def close(): Unit}, B](param: =>A)(f: A => B): B = 
  2.      | val closeable = param  // notice param is only referenced once
  3.      | try {
  4.      |     f(closeable)
  5.      |   } finally {
  6.      |     try {
  7.      |        closeable.close()
  8.      |     } catch { case _ => () }
  9.      |   }
  10. using: [A <: AnyRef{def close(): Unit},B](param: => A)(f: (A) => B)B
  11. /*
  12. if this was accessing a database a new connection would be made automatically each time this function was used
  13. */
  14. scala> val usingTheWorld = using[BStream,Int](new java.io.ByteArrayInputStream("hello world".getBytes))_
  15. usingTheWorld: ((java.io.ByteArrayInputStream) => Int) => Int = < function1>
  16. scala> usingTheWorld { s => io.Source.fromInputStream(s).length}  
  17. res3: Int = 11
  18. scala> usingTheWorld { s => io.Source.fromInputStream(s).getLines().length}
  19. res5: Int = 0

Wednesday, January 13, 2010

Comparing Manifests

Manifests are Scala's solution to Java's type erasure. It is not a complete solution as of yet but it does have several useful applications. Manifests were originally added to Scala 2.7 in an extremely limited form but have been improved a great deal for Scala 2.8. Now more powerful comparisons of manifests are possible. For another introduction to Manifests (2.7 manifests) see Manifests.

This post looks at a few ways to create manifests as well as how to compare manifests. The goal is to create a method that can be used in testing to require that a certain exception is thrown during a code block:
  1. scala> intercepts[Exception] {println("hi :)")}                                            
  2. hi :)
  3. Expected java.lang.Exception but instead no exception was raised

The code snippet above is a failure because println does not throw an exception. In addition to requiring manifests this code snippet also is a custom control structure, for more information on those see Control Structure Tag

But before writing the intercepts methods a short inspection of the new manifest comparison operators:
  1. scala> import scala.reflect.{
  2.      |   Manifest, ClassManifest
  3.      | }
  4. import scala.reflect.{Manifest, ClassManifest}
  5. // from class creates a manifest object given a class object
  6. scala> import ClassManifest.fromClass
  7. import ClassManifest.fromClass
  8. // several comparisons using <:<
  9. scala> fromClass(classOf[Exception]) <:< fromClass(classOf[Exception])
  10. res4: Boolean = true
  11. scala> fromClass(classOf[Exception]) <:< fromClass(classOf[RuntimeException])
  12. res5: Boolean = false
  13. scala> fromClass(classOf[Exception]) <:< fromClass(classOf[AssertionError])         
  14. res6: Boolean = false
  15. // now the opposite operator >:>
  16. scala> fromClass(classOf[Exception]) >:> fromClass(classOf[AssertionError])
  17. res7: Boolean = false
  18. scala> fromClass(classOf[Exception]) >:> fromClass(classOf[RuntimeException])
  19. res8: Boolean = true
  20. scala> fromClass(classOf[Exception]) >:> fromClass(classOf[Error])           
  21. res9: Boolean = false
  22. scala> fromClass(classOf[Exception]) >:> fromClass(classOf[Throwable])
  23. res10: Boolean = false
  24. // the method singleType creates a manifest given an object
  25. scala> ClassManifest.singleType(new RuntimeException())
  26. res12: scala.reflect.Manifest[Nothing] = java.lang.RuntimeException.type
  27. scala> ClassManifest.singleType(new RuntimeException()) <:< fromClass(classOf[Throwable])
  28. res13: Boolean = true
  29. scala> fromClass(classOf[Exception]) <:< fromClass(classOf[Throwable])                   
  30. res14: Boolean = true

Now the implementation of intercepts:
  1. scala> import scala.reflect.{
  2.      |   Manifest, ClassManifest
  3.      | }
  4. import scala.reflect.{Manifest, ClassManifest}
  5. scala> 
  6. scala> import ClassManifest.singleType
  7. import ClassManifest.singleType
  8. scala>  def intercepts[E <: Throwable](test : => Unit)(implicit m:Manifest[E]) : Unit = {
  9.      |     import Predef.{println => fail}
  10.      |     try {
  11.      |       test
  12.      |       // this is a failure because test is expected to throw an exception
  13.      |       fail("Expected "+m.toString+" but instead no exception was raised")
  14.      |     }catch{
  15.      |       // this checks that the expected type (m) is a superclass of the class of e
  16.      |       case e if (m >:> singleType(e)) => ()
  17.      |       // any other error is handled here
  18.      |       case e => fail("Expected "+m.toString+" but instead got "+e.getClass)
  19.      |     }
  20.      |   }
  21. intercepts: [E <: Throwable](test: => Unit)(implicit m: scala.reflect.Manifest[E])Unit
  22. scala> intercepts[Exception] {println("hi :)")}                                            
  23. hi :)
  24. Expected java.lang.Exception but instead no exception was raised
  25. scala> intercepts[Exception] { throw new IllegalArgumentException("why not throw this :)")}
  26. scala> intercepts[Exception] { throw new AssertionError("The method should complain")}     
  27. Expected java.lang.Exception but instead got class java.lang.AssertionError

Wednesday, December 16, 2009

By-name-parameter to Function

Today's topic is related to Defining Custom Control Structures. By name parameters are not function objects in the same way as other functions. Normally you need to invoke a function:
  1. scala> def exec(func: () => Int) = func()
  2. exec: (() => Int)Int
  3. // exec invokes the function so 10 is the result
  4. scala> exec (() => 10)  
  5. res1: Int = 10
  6. scala> def exec(func: () => Int) = func
  7. exec: (() => Int)() => Int
  8. // here exec returns the function:
  9. scala> exec (() => 10)                 
  10. res2: () => Int = <function>

Compare that to a by-name-parameter function:
  1. scala> def exec(func: => Int) = func   
  2. exec: (=> Int)Int
  3. // by-name-parameters are executed when they are referenced
  4. // so the result is 10
  5. scala> exec {10}                    
  6. res3: Int = 10
  7. // This is not legal because by-name-parameters
  8. // are not normal functions
  9. scala> def exec(func: => Int) = func()
  10. <console>:4: error: func of type Int does not take parameters
  11.        def exec(func: => Int) = func()

So the issue is how can you pass a by-name-parameter to a method that takes a function as a parameter without having to do:
  1. scala> def takesFunc(func: () => Int) = println(func())
  2. takesFunc: (() => Int)Unit
  3. scala> def send(x: => Int) = takesFunc(() => x) 
  4. send: (=> Int)Unit
  5. scala> send{2}
  6. 2

the alternative syntax is:
  1. scala> def send(x: => Int) = takesFunc (x _)   
  2. send: (=> Int)Unit
  3. scala> send {2}
  4. 2

Friday, November 27, 2009

Defining Custom Control Structures

Scala has only a handful of built-in control structures: for, while, try-catch, if, etc... However it is quite simple to define custom control structures. There are several good examples in the Scala 2.8 library. For some examples look at the classes in the scala.util.control package.

For this topic, we will develop a simplified version of the Exception class. First a demonstration of the 2.8 version.
  1. scala> import scala.util.control.Exception._
  2. import scala.util.control.Exception._
  3. scala> val numberFormat = catching(classOf[NumberFormatException])
  4. numberFormat: util.control.Exception.Catch[Nothing] = Catch(java.lang.NumberFormatException)
  5. scala> val result = numberFormat opt { "10".toInt }               
  6. result: Option[Int] = Some(10)
  7. scala> result match {
  8.      | case Some(n) => // do something
  9.      | case None => // handle this situation
  10.      |


A question that often arises when presented with the Exception control flow is why not use try-catch. There are two main reasons in my opinion.

First, people coming from certain functional style languages find that form more comfortable (so I have heard.) So in this case it is a style issue.

The other reason (and the one more pertinent to Java developers), is that it provides a nice way to handle common exceptions. Why do I say nice? First it declares when exceptions are handled before the code and that provides more context while reading the code. In addition, if you create the Catch object and assign it to a nicely named variable then it is clear what the intent is. For example there may be several reasons to catch a runtime exception and they should be handled differently. A few well-named variables can really assist in readability. A final point is that several of these variables can be defined in on object and shared throughout a project adding some consistency to a project.

Back to the topic at hand.

We will develop a simplified version: a catching method that will return Some(...) if no exception occurs or None if one of the declared exceptions has occurred or throw exception if the exception is not one we declare as handling. Now this is not what I would necessarily call a custom control flow because it does not offer a choice of execution flows. To do that I would add more components like int the Exceptions object. However, this is a good demonstration of the parts required to create a custom control struction. The key is the => in def method(arg: => T) This is a special type construct which means that a no param function is passed in but is not executed until called within the method. A few comparisons:
  1. scala> def method(arg: =>Int) = println(arg)
  2. method: (arg: => Int)Unit
  3. scala> def method2(arg: =>Int) = println("not calling arg")
  4. method2: (arg: => Int)Unit
  5. scala> var x = 1
  6. x: Int = 1
  7. // when method is called x is incremented because method calls the argument
  8. scala> method { x += 1; x }                                
  9. 2
  10. scala> println(x)          
  11. 2
  12. // when method2 is called x is not incremented because 
  13. // the argument is not called/referenced
  14. scala> method2 { x += 1; x }
  15. not calling arg
  16. scala> println(x)           
  17. 2
  18. // arg is referenced 2 times in method three so x is incremented 2 times
  19. scala> def method3(arg: => Int) = println("first call="+arg+" second call="+arg)
  20. method3: (arg: => Int)Unit
  21. scala> method3 { x += 1; x }                                                   
  22. first call=3 second call=4
  23. // Now demonstrate the standard way of defining arguments
  24. // the value passed is calculated before calling the method
  25. // so is at most called once
  26. scala> def byValue(arg: Int) = println(arg)
  27. byValue: (arg: Int)Unit
  28. scala> def byValue2(arg: Int) = println("not calling arg")
  29. byValue2: (arg: Int)Unit
  30. scala> def byValue3(arg: Int) = println("first call="+arg+" second call="+arg)
  31. byValue3: (arg: Int)Unit
  32. scala> byValue{ x += 1; x }
  33. 5
  34. scala> byValue2{ x += 1; x }
  35. not calling arg
  36. scala> println(x)
  37. 6
  38. scala> byValue3{ x += 1; x }
  39. first call=7 second call=7
  40. // And finally show the normal way to pass in a function. 
  41. // This has the benefit of allowing the reader of the code to
  42. // realize that the argument is a function
  43. // but is not a nice syntax for control flow
  44. scala> def stdFunc(arg: ()=>Int) = println(arg())
  45. stdFunc: (arg: () => Int)Unit
  46. scala> def stdFunc2(arg: ()=>Int) = println("not calling arg")
  47. stdFunc2: (arg: () => Int)Unit
  48. scdef stdFunc3(arg: ()=>Int) = println("first call="+arg()+" second call="+arg()) 
  49. stdFunc3: (arg: () => Int)Unit
  50. scala> stdFunc {() => x += 1; x }  
  51. 8
  52. scala> stdFunc2 {() => x += 1; x }
  53. not calling arg
  54. scala> println(x)
  55. 8
  56. scala> stdFunc3 {() => x += 1; x }
  57. first call=9 second call=10


Now for the complete catching example:
  1. scala> def catching[T](exceptions: Class[_]*)(body: => T) = {
  2.      | try {                                                 
  3.      | Some (body)                                           
  4.      | } catch {
  5.      | case e if (exceptions contains e.getClass) => None
  6.      | }                
  7.      | }
  8. catching: [T](exceptions: Class[_]*)(body: => T)Option[T]
  9. scala> val runtime = catching[Number](classOf[NullPointerException], classOf[NumberFormatException])_
  10. runtime: (=> java.lang.Number) => Option[java.lang.Number] = < function1>
  11. scala> runtime { "".toInt }                                                                          
  12. res2: Option[java.lang.Number] = None
  13. scala> runtime { "10".toInt }
  14. res3: Option[java.lang.Number] = Some(10)
  15. scala> runtime { throw new NullPointerException }
  16. res6: Option[java.lang.Number] = None
  17. scala> runtime { throw new RuntimeException }
  18. java.lang.RuntimeException
  19.         at $anonfun$1.apply(< console>:10)
  20.         at $anonfun$1.apply(< console>:10)
  21.         at .catching(< console>:9)
  22.         at $anonfun$1.apply(< console>:8)
  23.         at $anonfun$1.apply(< console>:8)
  24.         at .< init>(< console>:10)
  25.         at .< clinit>(< console>)
  26.         at RequestResult$.< init>(< console>:4)
  27.         at RequestResult$.< clinit>(< console>)
  28.         at RequestResult$result(< console>)
  29.         ...

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