Showing posts with label unapplySeq. Show all posts
Showing posts with label unapplySeq. Show all posts
Monday, November 30, 2009
Overloaded Unapply
This topic is related to previous posts on matching. I recommend reading some of them as well:
Since an Extactor is just an object with an unapply method it logically follows that an Extractor can have an overloaded unapply method. In other words can have an unapply(String) and an unapply(Int); allowing matching Strings and Ints.
Since an Extactor is just an object with an unapply method it logically follows that an Extractor can have an overloaded unapply method. In other words can have an unapply(String) and an unapply(Int); allowing matching Strings and Ints.
- scala> object T{
- | def unapply(v:String)= if(v== "s") Some("yay") else None
- | def unapply(v:Int) = if(v==1) Some("hmmm") else None
- | }
- defined module T
- scala> 1 match { case T(x) => println(x) }
- hmmm
- scala> "s" match { case T(x) => println(x) }
- yay
- scala> object T{
- | def unapplySeq(v:String) = if (v=="x") Some(List(1,2,3)) else None
- | def unapplySeq(v:Int) = if (v==1) Some(List("one","two")) else None
- | }
- defined module T
- scala> "x" match { case T(x,y,z) => println(x,y,z) }
- (1,2,3)
- scala> 1 match { case T(x,y) => println(x,y) }
- (one,two)
Labels:
match,
matching,
Scala,
unapply,
unapplySeq
Tuesday, September 29, 2009
Extract sequences (unapplySeq)
This topic continues the previous topic on matching and Extractors. Make sure you look at Extractors 1.
The first extractor topic covered the unapply method and how it is used during matching. Today I want to visit a similar method unapplySeq, which is used to match sequences. The method
Note: if both unapply and unapplySeq are defined only unapply is used.
When matching on Sequences the _* symbol means to match an arbitrary sequence. We use this several times in the examples below
The first extractor topic covered the unapply method and how it is used during matching. Today I want to visit a similar method unapplySeq, which is used to match sequences. The method
def unapplySeq(param):Option[Seq[T]
can be used instead of unapply. Note: if both unapply and unapplySeq are defined only unapply is used.
When matching on Sequences the _* symbol means to match an arbitrary sequence. We use this several times in the examples below
- scala> object FindAs {
- | def unapplySeq(string:String):Option[List[String]] = {
- | def containsA (word:String) = word.toLowerCase contains "a"
- |
- | if (string.toLowerCase contains "a") {
- | val words = string.split ("\\s+").
- | filter (containsA _)
- | Some(words.toList)
- | } else {
- | None
- | }
- | }
- | }
- defined module FindAs
- // as usual you can use extractors to assign variables
- scala> val FindAs(a,b) = "This sentence contains 2 a-s"
- a: String = contains
- b: String = a-s
- // If you only care about the first variable you can use _* to
- // reference the rest of the sequence that you don-t care about
- scala> val FindAs(a, _*) = "A crazy a sentence ack!"
- a: String = A
- // using b @ _* we can get the rest of the sequence assigned to b
- scala> val FindAs(a, b@_*) = "A crazy a sentence ack!"
- a: String = A
- b: Seq[String] = List(crazy, a, ack!)
- // standard matching pattern
- scala> "This sentence contains 2 a-s" match {
- | case FindAs(a,b) => println(a,b)
- | case _ => println("whoops")
- | }
- (contains,a-s)
- // In this example we only care that it can match not the values
- // so we ignore all of the actual sequence by using: _* as the parameters
- scala> "This sentence contains 2 a-s" match {
- | case FindAs(_*) => println("a match")
- | }
- a match
- scala> "This sentence contains 2 a-s" match {
- | case FindAs( first, _*) => println("first word = "+first)
- | }
- first word = contains
- scala> "A crazy a sentence ack!" match {
- | case FindAs( first, next, rest @ _*) => println("1=%s, 2=%s, rest=%s".format(first, next, rest) )
- | }
- 1=A, 2=crazy, rest=List(a, ack!)
Labels:
extractor,
intermediate,
match,
matching,
Scala,
unapply,
unapplySeq
Subscribe to:
Comments (Atom)