1. By now, probably grown accustomed to the concise-
Decomposed and analyzed using pattern matching. This chapter shows
How to generalize this concept further. Until now, constructor patterns were
Linked to case classes. For instance, Some (x) is a valid pattern because Some
is a case class. Sometimes you might wish so could write patterns like
This without creating a associated case class. In fact, you might wish
Able to create your own kinds of patterns. Extractors give you a-to-do
So. This chapter explains what Extractors is and how can use them to
Define patterns that is decoupled from an object ' s representation
So far, the constructor pattern only applies to case class, such as some (x), which is legal because some is a case class
Sometimes you want to write in a case class-independent pattern and create your own schema.
2. An extractor in Scala are an object and have a method called unapply as one of its members
Extractor in Scala is the object with the Unapply method
The purpose of that unapply method was to match a value and take it apart
The purpose of this method is to receive a value and then take it apart
Often, the Extractor object also defines a dual method, apply for building values, and this is not required
Usually a extractor also has the Apply method, which is used to establish the value, but it is not required
3. If an object defines the Unapply method, the constructor form of the object can appear behind the case statement, and the result of the decomposition is bound to the form variable
Package Chapter26object Email extends app{def unapply (str:string): option[(String, string)] = {val parts = str spli T "@" if (parts.length = = 2) Some (Parts (0), parts (1)) Else None} val str = "####### @cn. ibm.com" str match {C ASE Email (name, domain) = println ("Name:" +name + "domain:" + domain) Case _ = = println ("Match Fail")}}
4. To bind N variables, an unapply would return an n-element tuple, wrapped in a Some.
To bind n variables, unapply returns an n-tuple wrapped in some
5. Unapplyseq
You can define the Unapplyseq method if the number of results for the split is undefined
Package Chapter26object Domain extends app{def unapplyseq (whole:string): option[seq[string]] = Some (whole.split ("\ \ ."). Reverse) Val dom = "cn.scala.org" Dom match {case Domain ("com", "Sun", "Java") + println ("java.sun.com") case Domain ("org", "Scala", "cn") = println ("cn.scala.org") Case Domain (_*) = println ("Other Domain")}
6.Extractor can also be used for decomposition of variables
Package Chapter26object Domain extends app{def unapplyseq (whole:string): option[seq[string]] = Some (whole.split ("\ \ ."). Reverse) Val dom = "cn.scala.org" val Domain (z/y) = Dom println (x)//org println (y)//Scala println (z)//cn}
7. It dawned on me.
You can access the elements of a list or an array
Using sequence patterns such as:
List ()
List (x, y, _*)
Array (x, 0, 0, _)
In fact, these sequence patterns is all implemented using extractors in the
Standard Scala Library. For instance, patterns of the form List (...) are
Possible because the Scala. List Companion object is a extractor that defines an unapplyseq method.
You can do a pattern match on a list because the list object defines the Unapplyseq method
8. Independence of expression
Even though they is very useful, case classes has one shortcoming:they expose the concrete representation of data. This means, the name of the class in a constructor pattern corresponds to the concrete representation type of the Sele ctor Object
While case class is useful, there is one drawback: they expose the exact type of data. That is, the name of the constructor corresponds to a specific type
If a match against:
Case C (...)
Succeeds, you know the selector expression was an instance of class C.
If Case C (...) matches successfully, you know that the object to match is an instance of Class C
Extractors break this link between data representations and patterns. You
The seen in the examples with this section, the they enable patterns that has
Nothing to does with the data type of the object that's selected on. This property
is called representation independence. In open systems of large size, representation independence is very important because it allows
Implementation type used in a set of components without affecting clients of
These components.
Extractor interrupts the connection of data representations and data types. Makes the pattern independent of the data type. This nature is called the independence of the expression. In a large-scale development system, the expression of independence is very important. It allows you to change the implementation of a component without affecting the customer code
9. Regular expressions
One particularly useful application area of extractors is regular expressions.
Like Java, Scala provides regular expressions through a library, but extractors
Make it much nicer to interact with them
Extractor makes Scala's interaction with regular expressions even more beautiful
Package Chapter26object Testreg extends App {val decimal = "" "(-)? ( \d+) (\.\d*)? "". R val Input = "for-1.0 to the 3" for (s <-decimal.findallin (input)) {println (s)}}
Programming in Scala (Second Edition) reading notes-extractors