Big Data series Cultivation-scala Course 08

Source: Internet
Author: User

The next step is to explain the importance of pattern matching, from which you will learn about pattern matching.

About type, Array, List, tuple mode parsing

1.Type Pattern Matching Code parsing

 //  about type pattern matching  //  match int type, string type, map type [_,_] represents any type of k,v  def match_type (T:any) = T match { case  P:int = = println ("It is Integer" )  case  p:string = println ("It is String, the content is:" + p)  case  m:map[_, _] => M.foreach ( println)  case  _ = = println ("Unknown type!!!"  2 "Spark" 
     
      ) Match_type (Map (
      Scala, "Spark") 

2.Array Pattern Matching Code parsing

    //about array pattern matchingdef match_array (Arr:any) =arr Match {//Match array Data The first one is 0     CaseArray (0) = = println ("array:" + "0")     //match array two digits     CaseArray (x, y) = = println ("array:" + x + "" +y) CaseArray (0, _*) = println ("array:" + "0 ...")       Case_ = = println ("Something Else")} match_array (Array (0)) Match_array (Array (0,1)) Match_array (Array (0,1,2,3,4,5))

3.List Pattern Matching Code parsing

        // match list type data by:: To match    def match_list (lst:any) = lst Match {    case 0:: Nil = println ("list:" + "0")       case x:: Y:: Nil = println ("list:" + x + "+ y)    case 0:: tail = println (" List : "+" 0 ... ")    Case _ = = println (" Something Else " )    }    match_list (list ( 0))    match_list (list(3,4))    match_list (list (0,1,2,3,4,5))

4.Tuple, pattern matching code parsing

    // The tuple pattern matching-->tuple mode is more casual    def match_tuple (tuple:any) = tuple Match {    case (0, _) = = println ("Tuple:" + "0"    )   Case (x, 0) = println ("Tuple:" + x)    Case _ = = println ("Something Else" )    }        Match_tuple ((0, "Scala"))    match_tuple ((2,0))    match_tuple ((0,1,2,3,4,5))
About the Extractor extractor parsing in Scala

1.Extractor Real-Combat analysis

2.Extractor Source Code Analysis: Extractor is like apply function, get the user's

    //The extractor is the FETCH function in which the parameter gets the data--the pattern match of the array is the extractor gets the datadef match_array (Arr:any) =arr Match { CaseArray (0) = = println ("array:" + "0")      CaseArray (x, y) = = println ("array:" + x + "" +y) CaseArray (0, _*) = println ("array:" + "0 ...")       Case_ = = println ("Something Else")} match_array (Array (0)) Match_array (Array (0,1)) Match_array (Array (0,1,2,3,4,5))        //Regular and pattern matching combine to form a function to get the matching values and characters.Val pattern = "([0-9]+) ([a-z]+)]. R"20150628 Hadoop"Match { CasePattern (num, item) = println (num + ":" +Item)}

Case class and case object code parsing

1.Case Class Code parsing: Using case class and case object in pattern matching is much more used in message passing, enabling messages to be sent according to the business

2.Case Object code parsing: can be understood as a singleton mode

Object Case_class_object {def main (args:array[string]): Unit= {      //define a pattern matchdef caseops (Person:person) =Person Match { CaseStudent (age) = println ("I am" + Age + "Years")       CaseWorker (_, salary) = println ("Wow, I got" +salary) CaseShared = println ("No Property")} caseops (Worker (1,19)) Caseops (Student (19) ) Caseops (Shared)//because the inheritance will find that the parameters are constant-and need to change when the object can be copied to changeVal worker = worker (29, 10000.1) Val Worker2= Worker.copy (Salary = 19.95) Val Worker3= Worker.copy (age = 30)  }}//Case Class object plays an important role in the message transmission process//Abstract classAbstract class Person//inheriting abstract classes with constants of type int Case classStudent (Age:int)extends Person//inheriting abstract classes with constants of type int and double Case classWorker (Age:int, salary:double)extends Person//inherit abstract class--object CaseObject SharedextendsPerson
Pattern matching Advanced Combat: Nested Case class

1. Nested case class parsing: Parameters also use case class and caseobject in Match mode

2.Case Object Real-combat analysis

def main (args:array[string]): Unit = {        //define a pattern matching case classdef caseclass_nested (Person:item) =Person Match {//the 12th parameter in the match is not counted, and the third lesson parameter uses art instead of the book object with the @ symbol reference similar to this         CaseBundle (_, _, Art @ Book (_, _), rest @ _*) = println (Art.description + ":" +Art.price) CaseBundle (_, _, Book (Descr, _), _*) = println ("The first description is:" +DESCR) Case_ = = println ("oops!")      }      //calling a defined pattern matchCaseclass_nested (Bundle ("1111 Special's", 30.0,book ("Scala for the Spark Developer", 69.95), Bundle ("Hadoop", 40.0,book ("Hive", 79.95), book ("HBase", 32.95))) caseclass_nested (Bundle ("1212 Special ' s", 35.0, Book ("Spark for the impatient", 39.95)))   }  }Abstract classItem Case classBook (description:string, Price:double)extendsItem//inherits the abstract class, and the parameters are nested in the abstract class--that's the nested class in the class Case classBundle (description:string, price:double, items:item*)extendsItem

So much to say, to continue tomorrow ....

I hope you pay attention to Liaoliang Teacher's video, this series is also from his video learning, he is the number (18610086859)

Baidu Cloud Address: HTTP://PAN.BAIDU.COM/S/1KTW4IDP

Big Data series Cultivation-scala Course 08

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.