Friday, January 29, 2010
Overcoming Type Erasure in Matching 2 (Variance)
A commenter (Brian Howard) on the post Overcoming Type Erasure in Matching 1 made a very good point:
There is a way to do this, however the information is not captured in the Manifest. A manifest is not designed to do full reflection it is by design very light and has little impact on performance. So to provide the functionality requested by Brian one needs to add that information to the Extractor Definition. Have have a possible solution below.
Is there a way to deal with some type arguments being contravariant? Try the following:
class A
class B extends A
val AAFunction = new Def[Function1[A,A]]
((a:A) => a) match {case AAFunction(f) => Some(f(new A)); case _ => None} // this is OK
((a:A) => new B) match {case AAFunction(f) => Some(f(new A)); case _ => None} // this is OK
((b:B) => b) match {case AAFunction(f) => Some(f(new A)); case _ => None} // gives a ClassCastException, since new A is not a B
There is a way to do this, however the information is not captured in the Manifest. A manifest is not designed to do full reflection it is by design very light and has little impact on performance. So to provide the functionality requested by Brian one needs to add that information to the Extractor Definition. Have have a possible solution below.
- scala> class A
- defined class A
- scala> class B extends A
- defined class B
- scala> object Variance extends Enumeration {
- | val Co, Contra, No = Value
- | }
- defined module Variance
- scala> import Variance._
- import Variance._
- scala> class Def[C](variance:Variance.Value*)(implicit desired : Manifest[C]) {
- | def unapply[X](c : X)(implicit m : Manifest[X]) : Option[C] = {
- | val typeArgsTriplet = desired.typeArguments.zip(m.typeArguments).
- | zipWithIndex
- | def sameArgs = typeArgsTriplet forall {
- | case ((desired,actual),index) if(getVariance(index) == Contra) =>
- | desired <:< actual
- | case ((desired,actual),index) if(getVariance(index) == No) =>
- | desired == actual
- | case ((desired,actual),index) =>
- | desired >:> actual
- | }
- |
- | val isAssignable = desired.erasure.isAssignableFrom(m.erasure) || (desired >:> m)
- | if (isAssignable && sameArgs) Some(c.asInstanceOf[C])
- | else None
- | }
- | def getVariance(i:Int) = if(variance.length > i) variance(i) else No
- | }
- defined class Def
- scala> val AAFunction = new Def[Function1[A,A]]
- AAFunction: Def[(A) => A] = Def@64a65760
- scala> ((a:A) => a) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res0: Option[A] = Some(A@1bd4f279)
- scala> ((a:A) => new B) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res1: Option[A] = None
- scala> ((b:B) => b) match {case AAFunction(f) => Some(f(new A)); case _ => None}
- res2: Option[A] = None
- scala> val BAFunction = new Def[Function1[B,A]](Contra,Co)
- BAFunction: Def[(B) => A] = Def@26114629
- scala> ((b:A) => b) match {case BAFunction(f) => Some(f(new B)); case _ => None}
- res3: Option[A] = Some(B@15dcc3ca)
Labels:
Advanced,
contravariance,
covariance,
Enumeration,
manifest,
match,
matching,
Scala,
variance
Thursday, January 28, 2010
Overcoming Type Erasure in matching 1
Since Scala runs on the JVM (it also runs on .NET but we this tip is aimed at Scala on the JVM) much of the type information that is available at compile-time is lost during runtime. This means certain types of matching are not possible. For example it would be nice to be able to do the following:
If you run this code the list matches the list of ints which is incorrect (I have ran the following with -unchecked so it will print the warning about erasure):
Another example is trying to match on structural types. Wouldn't it be nice to be able to do the following (We will solve this in a future post):
So lets see what we can do about this. My proposed solution is to create an class that can be used as an extractor when instantiated to do the check.
This is a fairly advanced tip so make sure you have read up on Matchers and Manifests:
The key parts of the next examples are the Def class and the function 'func'. 'func' is defined in the comments in the code block.
The Def class is the definition of what we want to match. Once we have the definition we can use it as an Extractor to match List[Int] instances.
The Def solution is quite generic so it will satisfy may cases where you might want to do matching but erasure is getting in the way.
The secret is to use manifests:
It is critical to notice the use of the typeArguments of the manifest. This returns a list of the manifests of each typeArgument. You cannot simply compare desired == m because manifest comparisons are not deep. There is a weakness in this code in that it only handles generics that are 1 level deep. For example:
List[Int] can be matched with the following code but List[List[Int]] will match anything that is List[List[_]]. Making the method more generic is an exercise left to the reader.
- scala> val x : List[Any] = List(1.0,2.0,3.0)
- x: List[Any] = List(1, 2, 3)
- scala> x match { case l : List[Boolean] => l(0) }
If you run this code the list matches the list of ints which is incorrect (I have ran the following with -unchecked so it will print the warning about erasure):
- scala> val x : List[Any] = List(1.0,2.0,3.0)
- x: List[Any] = List(1, 2, 3)
- scala> x match { case l : List[Boolean] => l(0) }
- < console>:6: warning: non variable type-argument Boolean in type pattern List[Boolean] is unchecked since it is eliminated by erasure
- x match { case l : List[Boolean] => l(0) }
- ^
- java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Boolean
- at scala.runtime.BoxesRunTime.unboxToBoolean(Unknown Source)
- at .< init>(< console>:6)
Another example is trying to match on structural types. Wouldn't it be nice to be able to do the following (We will solve this in a future post):
- scala> "a string" match {
- | case l : {def length : Int} => l.length
- | }
- < console>:6: warning: refinement AnyRef{def length: Int} in type pattern AnyRef{def length: Int} is unchecked since it is eliminated by erasure
- case l : {def length : Int} => l.length
- ^
- res5: Int = 8
So lets see what we can do about this. My proposed solution is to create an class that can be used as an extractor when instantiated to do the check.
This is a fairly advanced tip so make sure you have read up on Matchers and Manifests:
The key parts of the next examples are the Def class and the function 'func'. 'func' is defined in the comments in the code block.
The Def class is the definition of what we want to match. Once we have the definition we can use it as an Extractor to match List[Int] instances.
The Def solution is quite generic so it will satisfy may cases where you might want to do matching but erasure is getting in the way.
The secret is to use manifests:
- When the class is created the manifest for the class we want is captured.
- Each time a match is attempted the manifest of the class being matched is captured
- In the unapply method, the two manifests are compared and when they match we can return the matched element but cast to the desired type for the compiler
It is critical to notice the use of the typeArguments of the manifest. This returns a list of the manifests of each typeArgument. You cannot simply compare desired == m because manifest comparisons are not deep. There is a weakness in this code in that it only handles generics that are 1 level deep. For example:
List[Int] can be matched with the following code but List[List[Int]] will match anything that is List[List[_]]. Making the method more generic is an exercise left to the reader.
- scala> import reflect._
- import reflect._
- /*
- This is the key class
- */
- scala> class Def[C](implicit desired : Manifest[C]) {
- | def unapply[X](c : X)(implicit m : Manifest[X]) : Option[C] = {
- | def sameArgs = desired.typeArguments.zip(m.typeArguments).forall {case (desired,actual) => desired >:> actual}
- | if (desired >:> m && sameArgs) Some(c.asInstanceOf[C])
- | else None
- | }
- | }
- defined class Def
- // first define what we want to match
- scala> val IntList = new Def[List[Int]]
- IntList: Def[List[Int]] = Def@6997f7f4
- /*
- Now use the object as an extractor. the variable l will be a typesafe List[Int]
- */
- scala> List(1,2,3) match { case IntList(l) => l(1) : Int ; case _ => -1 }
- res36: Int = 2
- scala> List(1.0,2,3) match { case IntList(l) => l(1) : Int ; case _ => -1 }
- res37: Int = -1
- // 32 is not a list so it will fail to match
- scala> 32 match { case IntList(l) => l(1) : Int ; case _ => -1 }
- res2: Int = -1
- scala> Map(1 -> 3) match { case IntList(l) => l(1) : Int ; case _ => -1 }
- res3: Int = -1
- /*
- The Def class can be used with any Generic type it is not restricted to collections
- */
- scala> val IntIntFunction = new Def[Function1[Int,Int]]
- IntIntFunction: Def[(Int) => Int] = Def@5675e2b4
- // this will match because it is a function
- scala> ((i:Int) => 10) match { case IntIntFunction(f) => f(3); case _ => -1}
- res38: Int = 10
- // no match because 32 is not a function
- scala> 32 match { case IntIntFunction(f) => f(3); case _ => -1}
- res39: Int = -1
- // Cool Map is a function so it will match
- scala> Map(3 -> 100) match { case IntIntFunction(f) => f(3); case _ => -1}
- res6: Int = 100
- /*
- Now we see both the power and the limitations of this solution.
- One might expect that:
- def func(a:Any) = {...}
- would work.
- However, if the function is defined with 'Any' the compiler does not pass in the required information so the manifest that will be created will be a Any manifest object. Because of this the more convoluted method declaration must be used so the type information is passed in.
- I will discuss implicit parameters in some other post
- */
- scala> def func[A](a:A)(implicit m:Manifest[A]) = {
- | a match {
- | case IntList(l) => l.head
- | case IntIntFunction(f) => f(32)
- | case i:Int => i
- | case _ => -1
- | }
- | }
- func: [A](a: A)(implicit m: Manifest[A])Int
- scala> func(List(1,2,3))
- res16: Int = 1
- scala> func('i')
- res17: Int = -1
- scala> func(4)
- res18: Int = 4
- scala> func((i:Int) => i+2)
- res19: Int = 34
- scala> func(Map(32 -> 2))
- res20: Int = 2
Tuesday, January 26, 2010
Guard Sugar
This topic is a simple tip for a cleaner syntax for guards. Guards/filters are statements in for-comprehensions and case statements that guard or filter matches. Often you will see them as follows:
However you have the option to apply a little sugar to the statements cleaning them up a little:
As you can see the brackets are optional. That is because in both cases the brackets are not required for the parser to determine the start and end of the guard statements. They are added so that the "normal" syntax used in the typical if statements will compile.
- scala> for (i <- 1 to 10; if (i % 2 == 1)) yield i
- res0: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 3, 5, 7, 9)
- scala> util.Random.nextInt(10) match {
- | case i if(i>5) => println("hi")
- | case _ => println("low")
- | }
- low
However you have the option to apply a little sugar to the statements cleaning them up a little:
- scala> for (i <- 1 to 10; if i % 2 == 1) yield i
- res2: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 3, 5, 7, 9)
- scala> util.Random.nextInt(10) match {
- | case i if i>5 => println("hi")
- | case _ => println("low")
- | }
- hi
As you can see the brackets are optional. That is because in both cases the brackets are not required for the parser to determine the start and end of the guard statements. They are added so that the "normal" syntax used in the typical if statements will compile.
- scala> 10 match { case i if i == 1 || i == 10 => "obviously this is a match"}
- res4: java.lang.String = obviously this is a match
- /*
- In case statements you can split the if almost any way you want because it is very clearly bound by the if and the => that is required for all case statements with a guard
- */
- scala> 10 match { case i if i == 1 ||
- | i == 10 => "obviously this is a match"}
- res5: java.lang.String = obviously this is a match
- scala> 10 match {
- | case i
- | if
- | i == 1
- | ||
- | i == 10
- | => "matched"
- | }
- res6: java.lang.String = matched
- /*
- For-comprehensions are not as flexible since it is harder for the compiler to determine where the guard ends. So try to keep it on one line or otherwise make it a function. That is probably good advice for case statements as well.
- */
- scala> for {
- | x <- 1 to 10
- | if
- | x > 10
- | || x == 2 } yield x
- < console>:5: error: '<-' expected but integer literal found.
- || x == 2 } yield x
- ^
- < console>:5: error: illegal start of simple expression
- || x == 2 } yield x
- ^
- scala> for {
- | x <- 1 to 10
- | if x == 1 || x == 10 } yield x
- res7: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 10)
Labels:
beginner,
filter,
for,
for-comprehension,
guard,
match,
matching,
Scala,
syntactic sugar
Monday, January 25, 2010
Matching with Or
There is a common case where several expressions in a match statement need to have the same result. For example:
this could be changed to:
but this is rather verbose. Another option is to use or matching:
- scala> "word" match {
- | case a @ "word" => println(a)
- | case a @ "hi" => println(a)
- | }
- word
this could be changed to:
- scala> "word" match {
- | case a if(a == "word" || a == "hi") => println(a)
- | }
- word
but this is rather verbose. Another option is to use or matching:
- scala> "word" match { case a @ ("word" | "hi") => println(a)}
- word
- /*
- it is possible to provide alternatives in matching
- Anything more advanced needs to be handled with a guard
- See the last examples
- */
- scala> 1 match { case _:Int | _:Double => println("Found it")}
- Found it
- // v will be the value if v is either an Int or a Double
- scala> 1.0 match { case v @ ( _:Int | _:Double) => println(v)}
- 1.0
- // Having variables as part of the patterns is not permitted
- scala> 1.0 match { case v:Int | d:Double => println(v)}
- < console>:5: error: illegal variable in pattern alternative
- 1.0 match { case v:Int | d:Double => println(v)}
- ^
- < console>:5: error: illegal variable in pattern alternative
- 1.0 match { case v:Int | d:Double => println(v)}
- ^
- /*
- Variables are not permitted not even when the name is the same.
- */
- scala> 1.0 match { case d:Int | d:Double => println(d)}
- < console>:5: error: illegal variable in pattern alternative
- 1.0 match { case d:Int | d:Double => println(d)}
- ^
- < console>:5: error: illegal variable in pattern alternative
- 1.0 match { case d:Int | d:Double => println(d)}
- ^
Friday, January 22, 2010
Streams 2: Stream construction
This post just adds to the information about streams in the previous post Introducing Streams. In this topic the methods in the Stream object are looked at a little closer:
(standard warning. Examples were done in Scala 2.8 so some examples may need to be modified for Scala 2.7)
(standard warning. Examples were done in Scala 2.8 so some examples may need to be modified for Scala 2.7)
- scala> import Stream._
- import Stream._
- // the empty stream. good for terminating a stream
- scala> empty
- res0: scala.collection.immutable.Stream[Nothing] = Stream()
- // one way to declare a stream.
- scala> 1 #:: 2 #:: empty foreach (println _)
- 1
- 2
- // the other main way to create a stream
- scala> cons(1, cons(2, empty)) foreach println
- 1
- 2
- /*
- Neither of the previous methods of stream creation are particularily useful because they are explicit and therefore they may as well be Lists or some other collection. There is very little benefit in those cases. However when the cons of a stream (tail) is defined as a function then things get a bit more interesting
- */
- scala> def i(x:Int,y:Int):Stream[Int] = (x*y) #:: i(x+1,y*2)
- i: (x: Int,y: Int)Stream[Int]
- scala> i(2,3) take 3 foreach println
- 6
- 18
- 48
- // now lets visit a few more methods in the Stream object
- // create an infinite stream starting at 10
- scala> from (10) take 3 foreach println
- 10
- 11
- 12
- // an infinite stream starting at 10 an increasing by 3
- scala> from (10,3) take 3 foreach println
- 10
- 13
- 16
- // converting an interator to a stream
- scala> (1 until 4).iterator.toStream foreach println
- 1
- 2
- 3
- // creating an Iterable to a stream
- scala> (1 until 4).toStream foreach println
- 1
- 2
- 3
- // a stream that always returns 7
- // the following is a pretty convoluted way to compute 7*49 :-P
- scala> continually(7) take 49 reduceLeft {_ + _}
- res10: Int = 343
- // create a stream of 6 streams
- scala> fill(6)(1 to 2 toStream) foreach println
- Stream(1, ?)
- Stream(1, ?)
- Stream(1, ?)
- Stream(1, ?)
- Stream(1, ?)
- Stream(1, ?)
- /*
- create the same stream as the last example but flatten it out so instead of being a stream of 6 streams it is a stream of 12 elements. Each element in each of the streams are visited in order
- */
- scala> fill(6)(1 to 2 toStream).flatten take 6 foreach println
- 1
- 2
- 1
- 2
- 1
- 2
- /*
- equivalent to:
- (1 until 20 by 3).toStream foreach println
- */
- scala> range(1, 20, 3) foreach println
- 1
- 4
- 7
- 10
- 13
- 16
- 19
- /*
- equivalent to
- (1 until 3).toStream foreach println
- */
- scala> range(1,3) foreach println
- 1
- 2
- /*
- iterate is fun!
- signature is iterator(start)(elem)
- basically starting at 3 execute the function on the previous value in the stream.
- This is an infinite stream
- */
- scala> iterate(3){i => i-10} take 5 foreach println _
- 3
- -7
- -17
- -27
- -37
- /*
- Another example
- */
- scala> iterate(3){i => i*2} take 5 foreach println _
- 3
- 6
- 12
- 24
- 48
- /*
- A final example
- */
- scala> iterate(3){i => i} take 5 foreach println _
- 3
- 3
- 3
- 3
- 3
- /*
- The real final,final example. This example uses the optional length parameter. So the stream is restricted to 5 elements
- */
- scala> iterate(3,5){i => i} foreach println
- 3
- 3
- 3
- 3
- 3
Labels:
intermediate,
Scala,
stream
Thursday, January 21, 2010
Exclude style import semantics
I can't remember if I posted this tip yet so I will do it again (or not :-P ).
If you want to import all classes in a package or object exception for one or two there is a neat trick to exclude the unwanted elements:
If you want to import all classes in a package or object exception for one or two there is a neat trick to exclude the unwanted elements:
- /*
- The File class is aliased to _ (oblivion) and then everything is imported (except File since it does not exist in that scope)
- Note: File can be imported later
- */
- scala> import java.io.{File=>_,_}
- import java.io.{File=>_, _}
- scala> new File("it is a file")
- < console>:8: error: not found: type File
- new File("it is a file")
- scala> object O {
- | def one = 1
- | def two = 2
- | def o = 0
- | }
- defined module O
- /*
- Same tricks can apply to importing methods and fields from objects
- */
- scala> import O.{o=>_, _}
- import O.{o=>_, _}
- scala> one
- res2: Int = 1
- scala> two
- res3: Int = 2
- scala> o
- < console>:15: error: not found: value o
- o
- // Once a class is imported into scope it can not be removed
- scala> import java.io.File
- import java.io.File
- scala> import java.io.{File=>_}
- import java.io.{File=>_}
- /*
- this still works because the previous import statement only adds an alias it does not remove the alias
- */
- scala> new File("..")
- res6: java.io.File = ..
- // There can be multiple aliases in a scope
- scala> import java.io.{File=>jfile}
- import java.io.{File=>jfile}
- scala> new jfile("..")
- res0: java.io.File = ..
- // one more example of importing from objects
- scala> case class X(a:Int, b:Int, c:Int)
- defined class X
- scala> val x = new X(1,2,3)
- x: X = X(1,2,3)
- scala> import x.{a=>_,b=>_,_}
- import x.{a=>_, b=>_, _}
- scala> c
- res1: Int = 3
- scala> b
- < console>:14: error: not found: value b
- b
- ^
Labels:
import,
import alias,
intermediate,
Scala
Wednesday, January 20, 2010
Case classes in 2.8
Thanks to Scala 2.8's default parameters case classes have a couple wonderful new features in Scala 2.8. Specifically the copy method and the ability to have case classes with default parameters.
- // constructor methods have defaults defined
- scala> case class Node (name:String, left : Option[Node] = None, right : Option[Node] = None)
- defined class Node
- // the name is the only required parameter because it does not have a default
- scala> val child = Node("leaf")
- child: Node = Node(leaf,None,None)
- // left is being assigned here because the name of the parameter is not explicit
- scala> val parent = Node("parent", Some(res0))
- parent: Node = Node(parent,Some(Node(leaf,None,None)),None)
- // now the left node is not defined just the right
- scala> val node = Node("node", right=Some(res0))
- node: Node = Node(node,None,Some(Node(leaf,None,None)))
- /*
- The real power is the copy constructor that is automatically generated in the case class. I can make a copy with any or all attributes modifed by using the copy constructor and declaring which field to modify
- */
- scala> parent.copy(right = Some(node))
- res4: Node = Node(parent,Some(Node(leaf,None,None)),Some(Node(node,None,Some(Node(leaf,None,None)))))
- scala> parent.copy(left=None)
- res5: Node = Node(parent,None,None)
- scala> parent.copy(name="hoho")
- res6: Node = Node(hoho,Some(Node(leaf,None,None)),None)
- scala> parent.copy(name="hoho", left=None)
- res7: Node = Node(hoho,None,None)
Labels:
2.8,
case-classes,
Scala
Monday, January 18, 2010
Introducing Streams
Streams are a special type of Iterable/Traversable whose elements are not evaluated until they are requested. Streams are normally constructed as functions.
A Scala List basically consists of the head element and the rest of the list. A Scala Stream is the head of the stream and a function that can construct the rest of the Stream. It is a bit more than this because each element is evaluated only once and stored in memory for future evaluations. That should be more clear after some examples.
As usual I created these examples with the Scala 2.8 REPL but I think most if not all should work in 2.7.
A very common way to construct a Stream is to define a recursive method. Each recursive call constructs a new element in the stream. The method may or may not have a guard condition that terminates the stream.
This is only an introduction. I hope to add a few more topics that focus on Streams because they can be very powerful but are also more challenging to recognize where they should be used instead of a standard collection.
A Scala List basically consists of the head element and the rest of the list. A Scala Stream is the head of the stream and a function that can construct the rest of the Stream. It is a bit more than this because each element is evaluated only once and stored in memory for future evaluations. That should be more clear after some examples.
As usual I created these examples with the Scala 2.8 REPL but I think most if not all should work in 2.7.
- scala> import Stream.cons
- import Stream.cons
- /*
- Because streams are very functional in nature it is recommended that methods from the Stream object are used for creation
- This is a real boring example of creating a Stream. Anything this simple should be a list.
- The important part is that cons take the value at the point and a function to return the rest of the
- stream NOT another stream.
- */
- scala> val stream1 = cons(0,cons(1,Stream.empty))
- stream1: Stream.Cons[Int] = Stream(0, ?)
- scala> stream1 foreach {print _}
- 01
- /*
- This illustrates the similarity in design between Stream and list, again the difference is the entire list is created in a stream the second argument of cons is not evaluated until it is requested
- */
- scala> new ::(0, new ::(1,List.empty))
- res35: scala.collection.immutable.::[Int] = List(0, 1)
- /*
- To drive home the point of the similarities. Here is an alternative declaration of a
- stream most similar to declaring a list
- */
- scala> val stream2 = 0 #:: 1 #:: Stream.empty
- stream2: scala.collection.immutable.Stream[Int] = Stream(0, ?)
- scala> stream2 foreach {print _}
- 01
- scala> 0 :: 1 :: Nil
- res36: List[Int] = List(0, 1)
- /*
- A little more interesting now. The accessing the second element will run the function. Notice it is not evaluated until request
- */
- scala> val stream3 = cons (0, {
- | println("getting second element")
- | cons(1,Stream.empty)
- | })
- stream3: Stream.Cons[Int] = Stream(0, ?)
- scala> stream3(0)
- res56: Int = 0
- // function is evaluated
- scala> stream3(1)
- getting second element
- res57: Int = 1
- /*
- Function is only evaluated once.
- Important! This means that all elements in a Stream are loaded into a memory so
- it can cause a OutOfMemoryError if the stream is large
- */
- scala> stream3(1)
- res58: Int = 1
- scala> stream3(1)
- res59: Int = 1
- /*
- This creates an infinate stream then forces resolution of all elements
- */
- scala> Stream.from(100).force
- java.lang.OutOfMemoryError: Java heap space
- // Alternative demonstration of laziness
- scala> val stream4 = 0 #:: {println("hi"); 1} #:: Stream.empty
- stream4: scala.collection.immutable.Stream[Int] = Stream(0, ?)
- scala> stream4(1)
- hi
- res2: Int = 1
A very common way to construct a Stream is to define a recursive method. Each recursive call constructs a new element in the stream. The method may or may not have a guard condition that terminates the stream.
- // construct a stream of random elements
- scala> def make : Stream[Int] = Stream.cons(util.Random.nextInt(10), make)
- make: Stream[Int]
- scala> val infinate = make
- infinate: Stream[Int] = Stream(3, ?)
- scala> infinate(5)
- res10: Int = 6
- scala> infinate(0)
- res11: Int = 3
- // Once evaluated each element does not change
- scala> infinate(5)
- res13: Int = 6
- // this function makes a stream that does terminate
- scala> def make(i:Int) : Stream[String] = {
- | if(i==0) Stream.empty
- | else Stream.cons(i + 5 toString, make(i-1))
- | }
- make: (i: Int)Stream[String]
- scala> val finite = make(5)
- finite: Stream[String] = Stream(10, ?)
- scala> finite foreach print _
- 109876
- // One last demonstration of making a stream object
- scala> Stream.cons("10", make(2))
- res18: Stream.Cons[String] = Stream(10, ?)
- /*
- This method is dangerous as it forces the entire stream to be evaluated
- */
- scala> res18.size
- res19: Int = 3
This is only an introduction. I hope to add a few more topics that focus on Streams because they can be very powerful but are also more challenging to recognize where they should be used instead of a standard collection.
Labels:
collections,
intermediate,
list,
non-strict,
Scala,
stream
Friday, January 15, 2010
More on Null to Option Conversion
I have seen requests for a simpler way of dealing with nulls than Option(x) so here are a couple ideas:
Note: This only works with Scala 2.8+
Note: This only works with Scala 2.8+
- // create an alias from Option.apply to ?
- scala> import Option.{apply => ?}
- import Option.{apply=>$qmark}
- scala> ?(null)
- res0: Option[Null] = None
- scala> ?(3)
- res1: Option[Int] = Some(3)
- scala> ?(3).getOrElse(10)
- res2: Int = 3
- scala> ?(null).getOrElse(10)
- res3: Any = 10
- // create an implicit conversion to Option
- scala> implicit def toOption[T](x:T) : Option[T] = Option(x)
- toOption: [T](x: T)Option[T]
- scala> 3 getOrElse (10)
- res4: Int = 3
- scala> val i:String = null
- i: String = null
- scala> i getOrElse "hi"
- res6: String = hi
Labels:
intermediate,
null,
option,
Scala
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:
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:
Now the implementation of intercepts:
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:
- scala> intercepts[Exception] {println("hi :)")}
- hi :)
- 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:
- scala> import scala.reflect.{
- | Manifest, ClassManifest
- | }
- import scala.reflect.{Manifest, ClassManifest}
- // from class creates a manifest object given a class object
- scala> import ClassManifest.fromClass
- import ClassManifest.fromClass
- // several comparisons using <:<
- scala> fromClass(classOf[Exception]) <:< fromClass(classOf[Exception])
- res4: Boolean = true
- scala> fromClass(classOf[Exception]) <:< fromClass(classOf[RuntimeException])
- res5: Boolean = false
- scala> fromClass(classOf[Exception]) <:< fromClass(classOf[AssertionError])
- res6: Boolean = false
- // now the opposite operator >:>
- scala> fromClass(classOf[Exception]) >:> fromClass(classOf[AssertionError])
- res7: Boolean = false
- scala> fromClass(classOf[Exception]) >:> fromClass(classOf[RuntimeException])
- res8: Boolean = true
- scala> fromClass(classOf[Exception]) >:> fromClass(classOf[Error])
- res9: Boolean = false
- scala> fromClass(classOf[Exception]) >:> fromClass(classOf[Throwable])
- res10: Boolean = false
- // the method singleType creates a manifest given an object
- scala> ClassManifest.singleType(new RuntimeException())
- res12: scala.reflect.Manifest[Nothing] = java.lang.RuntimeException.type
- scala> ClassManifest.singleType(new RuntimeException()) <:< fromClass(classOf[Throwable])
- res13: Boolean = true
- scala> fromClass(classOf[Exception]) <:< fromClass(classOf[Throwable])
- res14: Boolean = true
Now the implementation of intercepts:
- scala> import scala.reflect.{
- | Manifest, ClassManifest
- | }
- import scala.reflect.{Manifest, ClassManifest}
- scala>
- scala> import ClassManifest.singleType
- import ClassManifest.singleType
- scala> def intercepts[E <: Throwable](test : => Unit)(implicit m:Manifest[E]) : Unit = {
- | import Predef.{println => fail}
- | try {
- | test
- | // this is a failure because test is expected to throw an exception
- | fail("Expected "+m.toString+" but instead no exception was raised")
- | }catch{
- | // this checks that the expected type (m) is a superclass of the class of e
- | case e if (m >:> singleType(e)) => ()
- | // any other error is handled here
- | case e => fail("Expected "+m.toString+" but instead got "+e.getClass)
- | }
- | }
- intercepts: [E <: Throwable](test: => Unit)(implicit m: scala.reflect.Manifest[E])Unit
- scala> intercepts[Exception] {println("hi :)")}
- hi :)
- Expected java.lang.Exception but instead no exception was raised
- scala> intercepts[Exception] { throw new IllegalArgumentException("why not throw this :)")}
- scala> intercepts[Exception] { throw new AssertionError("The method should complain")}
- Expected java.lang.Exception but instead got class java.lang.AssertionError
Labels:
Advanced,
control structure,
manifest,
Scala
Monday, January 11, 2010
Matching Nulls
As a bit of explanation of one of the techniques in Regex Matching this topic reviews matching nulls.
- // No surprise _ matches everything
- scala> null match { case _ => println("null") }
- null
- // again null matches null
- scala> null match { case null => println("null") }
- null
- // a is bound to anything including null
- scala> null match { case a => println("matched value is: "+a) }
- matched value is: null
- scala> val a:String = null
- a: String = null
- // basically same as last example
- scala> a match {case a => println( a + " is null")}
- null is null
- // Any matches any non-null object
- scala> null match {
- | case a:Any => println("matched value is: "+a)
- | case _ => println("null is not Any")
- | }
- null is not Any
- scala> val d:String = null
- d: String = null
- // In fact when matching null does not match any type
- scala> d match {
- | case a:String => println("matched value is: "+a)
- | case _ => println("no match")
- | }
- no match
- scala> val data:(String,String) = ("s",null)
- data: (String, String) = (s,null)
- // matching can safely deal with nulls but don't forget the catch all
- // clause or you will get a MatchError
- scala> data match {
- | case (a:String, b:String) => "shouldn't match"
- | case (a:String, _) => "should match"
- | }
- res10: java.lang.String = should match
- // again null is all objects but will not match Any
- scala> data match {
- | case (a:String, b:Any) => "shouldn't match"
- | case (a:String, _) => "should match"
- | }
- res12: java.lang.String = should match
Labels:
intermediate,
matching,
null,
Scala
Subscribe to:
Comments (Atom)