- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Would you please explain this method to me?
Wed, 2010-02-10, 16:21
Hey,
Would you please consider this function:
def processList(l: List[Any]): Unit = l match {
case head :: tail =>
format("%s ", head)
processList(tail)
case Nil => println("")
}
Why and how head is assigned the first element and why and how tail is
assigned the remaining elements?
Thanks.
Wed, 2010-02-10, 16:47
#2
Re: Would you please explain this method to me?
Why unapplySeq is getting called in the sample function?
Stefan Langer-4 wrote:
>
> The magic is in the companion objects unapplySeq[A](x: CC[A]):
> Some
> [CC[A]] which deconstructs the list into head and tail
>
> method.
>
> 2010/2/10 HHB
>
>>
>> Hey,
>> Would you please consider this function:
>>
>> def processList(l: List[Any]): Unit = l match {
>> case head :: tail =>
>> format("%s ", head)
>> processList(tail)
>> case Nil => println("")
>> }
>>
>> Why and how head is assigned the first element and why and how tail is
>> assigned the remaining elements?
>> Thanks.
>> --
>> View this message in context:
>> http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp2753...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>
Wed, 2010-02-10, 17:07
#3
Re: Would you please explain this method to me?
Pattern matching in scala is done through the use of unapply and unapplySeq. These functions take as an argument a class and return an Option with the resulting tuple. unapplySeq is always used when you have a variable lenght datastructure such as List.
Example.
class Test(val id: Int, val value: String)
object Test {
def apply(id: Int, value: String) = new Test(id, value) // this is the construction factory
def unapply(test: Test) = Some(test.id, test.value)
}
when you use case Test(a, b) you effectivly call the unapply method of Test which returns a tuple (a, b) of Tuple2[Int, String]
When you define a case class the compiler creates these methods for you so you do not have to code them by hand.
-Stefan
2010/2/10 HHB <hubaghdadi@yahoo.ca>
Example.
class Test(val id: Int, val value: String)
object Test {
def apply(id: Int, value: String) = new Test(id, value) // this is the construction factory
def unapply(test: Test) = Some(test.id, test.value)
}
when you use case Test(a, b) you effectivly call the unapply method of Test which returns a tuple (a, b) of Tuple2[Int, String]
When you define a case class the compiler creates these methods for you so you do not have to code them by hand.
-Stefan
2010/2/10 HHB <hubaghdadi@yahoo.ca>
Why unapplySeq is getting called in the sample function?
Stefan Langer-4 wrote:
>
> The magic is in the companion objects unapplySeq[A](x: CC[A]):
> Some<http://www.scala-lang.org/archives/beta-api/scala/Some.html>
> [CC[A]] which deconstructs the list into head and tail
>
> method.
>
> 2010/2/10 HHB <hubaghdadi@yahoo.ca>
>
>>
>> Hey,
>> Would you please consider this function:
>>
>> def processList(l: List[Any]): Unit = l match {
>> case head :: tail =>
>> format("%s ", head)
>> processList(tail)
>> case Nil => println("")
>> }
>>
>> Why and how head is assigned the first element and why and how tail is
>> assigned the remaining elements?
>> Thanks.
>> --
>> View this message in context:
>> http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp27532815p27532815.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>
--
View this message in context: http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp27532815p27533108.html
Sent from the Scala - User mailing list archive at Nabble.com.
Wed, 2010-02-10, 17:17
#4
Re: Would you please explain this method to me?
You can find discussion of the use of :: in matching lists here: http://programming-scala.labs.oreilly.com/ch03.html#PatternMatching
On Wed, Feb 10, 2010 at 9:26 PM, Stefan Langer <mailtolanger@googlemail.com> wrote:
On Wed, Feb 10, 2010 at 9:26 PM, Stefan Langer <mailtolanger@googlemail.com> wrote:
Pattern matching in scala is done through the use of unapply and unapplySeq. These functions take as an argument a class and return an Option with the resulting tuple. unapplySeq is always used when you have a variable lenght datastructure such as List.
Example.
class Test(val id: Int, val value: String)
object Test {
def apply(id: Int, value: String) = new Test(id, value) // this is the construction factory
def unapply(test: Test) = Some(test.id, test.value)
}
when you use case Test(a, b) you effectivly call the unapply method of Test which returns a tuple (a, b) of Tuple2[Int, String]
When you define a case class the compiler creates these methods for you so you do not have to code them by hand.
-Stefan
2010/2/10 HHB <hubaghdadi@yahoo.ca>
Why unapplySeq is getting called in the sample function?
Stefan Langer-4 wrote:
>
> The magic is in the companion objects unapplySeq[A](x: CC[A]):
> Some<http://www.scala-lang.org/archives/beta-api/scala/Some.html>
> [CC[A]] which deconstructs the list into head and tail
>
> method.
>
> 2010/2/10 HHB <hubaghdadi@yahoo.ca>
>
>>
>> Hey,
>> Would you please consider this function:
>>
>> def processList(l: List[Any]): Unit = l match {
>> case head :: tail =>
>> format("%s ", head)
>> processList(tail)
>> case Nil => println("")
>> }
>>
>> Why and how head is assigned the first element and why and how tail is
>> assigned the remaining elements?
>> Thanks.
>> --
>> View this message in context:
>> http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp27532815p27532815.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>
--
View this message in context: http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp27532815p27533108.html
Sent from the Scala - User mailing list archive at Nabble.com.
Wed, 2010-02-10, 17:27
#5
Re: Would you please explain this method to me?
Case statements are pattern matches. There are a few different cases worth notice. Consider the following statements, assuming you are doing "x match { ... }":
1. case uncapitalizedIdentifier => ...
If you pass a single identifier starting with lowercase, this then whatever it is that is being matched will just be assigned to "uncapitalizedIdentifier", this will be accepted.
2. case CapitalizedIdentifier => ...
If you pass an identifier whose first letter is uppercase, then Scala will compare the value of CapitalizedIdentifier, which is expected to be an object of some kind, to whatever it is being matched. In other words, this is equivalent to "x.equals(CapitalizedIdentifier)".
3. case literal => ...
If you pass a literal, it will compare against the value being matched. Like above, this is equivalent to "x.equals(literal)".
4. case `uncapitalizedIdentifier` => ...
If an identifier is provided around back-ticks, then it will be compared against the value being matched, so this is equivalent to "x.equals(uncapitalizedIdentifier)".
5. case something : Type => ...
Assuming the "something" part of the statement matches, then Scala will check whether Type applies to it. Implicit conversions are not taken into account, and Type is necessarily erased (ie, you can't compare against List[Int], you must use List[_]). It similar (not sure if fully equivalent) to "something.isInstanceOf[Type]".
6. case something if condition => ...
Assuming "something" matches, then the statement will match if "condition" is an expression which evaluates to "true".
7. case identifier(something) => ...
In this case, "identifier" is supposed to be an object implementing either the method "unapply(t: T): Option[A]" or the method "unapplySeq(t: T): Option[Seq[A]]". It will match if "x.isInstanceOf[T]" (ie, the value being matched can be passed to it), if the result of calling that method -- unapply(x) or unapplySeq(x) -- is not None, and one of the rules below is satisfied:
7a) If the method called was "unapply" and the result was Some(y), then "y" must match "something", in a roughly recursive manner. If "y" is a tuple, then "something" may be composed of multiple parts separated by commas, each of which must be matched by the corresponding element in "y".
7b) If the method called was "unapplySeq", then "something" must be composed of as many parts, separated by commas, as there are elements in the sequence. Also, each element in that sequence must match the corresponding part of "something", in a roughly recursive mannar.
8. case part1 identifier part2 => ...
This is equivalent to "case identifier(part1, part2)"
9. case identifier() => ...
This may either be matched by an "unapplySeq" method returning an empty sequence, or by a method "unapply(t: T): Boolean", defined on "identifier", which returns "true" when called with "x".
10. case identifier(p1, ..., pn, _*) This is similar to the pattern 7b, but any remaining elements in the sequence after matching p1 through pn are automatically matched. 11. case y @ something Assuming "something" matches, then "y" is assigned the value that was matched against "something". This makes particular sense in an expression like "case Seq(p1, ..., pn, y @ _*)", which demonstrates the recursiveness of these rules. So "case head :: tail =>" matches if "::.unapply(l)" returns a 2-tuple, and the first and second elements of this tuple will be assigned to "head" and "tail" respectively.
On Wed, Feb 10, 2010 at 1:21 PM, HHB <hubaghdadi@yahoo.ca> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
10. case identifier(p1, ..., pn, _*) This is similar to the pattern 7b, but any remaining elements in the sequence after matching p1 through pn are automatically matched. 11. case y @ something Assuming "something" matches, then "y" is assigned the value that was matched against "something". This makes particular sense in an expression like "case Seq(p1, ..., pn, y @ _*)", which demonstrates the recursiveness of these rules. So "case head :: tail =>" matches if "::.unapply(l)" returns a 2-tuple, and the first and second elements of this tuple will be assigned to "head" and "tail" respectively.
On Wed, Feb 10, 2010 at 1:21 PM, HHB <hubaghdadi@yahoo.ca> wrote:
Hey,
Would you please consider this function:
def processList(l: List[Any]): Unit = l match {
case head :: tail =>
format("%s ", head)
processList(tail)
case Nil => println("")
}
Why and how head is assigned the first element and why and how tail is
assigned the remaining elements?
Thanks.
--
View this message in context: http://old.nabble.com/Would-you-please-explain-this-method-to-me--tp27532815p27532815.html
Sent from the Scala - User mailing list archive at Nabble.com.
--
Daniel C. Sobral
I travel to the future all the time.
method.
2010/2/10 HHB <hubaghdadi@yahoo.ca>