Scala pattern matching, type system and spark source reading

Source: Internet
Author: User
Tags case statement switch case

Java switch-case (pair value)
Scala is not only for values, but also for types, collections (map,list metadata matching), Object,class
Scala uses a lot of pattern matching (Match case)

Scala's pattern match, which differs from the Java switch case:

1. Not only can match the value, can match type

2. Can match the collection of arrays
An array of the same array, the same length, and an array beginning with an element
Automatic variable assignment for arrays of the same length

3. Match Class-Sample case Class
Like a bean in Java
Generally not change, the default is only getter
Can be used for message communication
The base class passes the parameters and matches the different subclasses with match to receive and process
Some () and none (), which indicate matching to and not matched when pattern matches
A case class and case object distinguish the former from producing multiple instances, the latter having only one instance, which is itself

Data input, which is a string
The underscore indicates neither spark nor Hadoop
Simple Example

Similar to the switch case statement
Underline represents the default condition
You can use the if to branch judgment by default

def bigData(dataage: Int){data match{case"Spark" => println("Wow!!"case"Hadoop" => println("OK!!")caseif(age>18) => println("adult")case _ => println("nothing")}}

Two parameters passed in

Pass in two parameter overrides

Variables outside of match can be automatically passed to the match variable
Data assignment to Data_

def bigData(data : String){   data match{    case"Spark" => println("wow")    case"Hadoop" => println("ok")    caseif"Flink" => println("cool"+data_)    case _ => println("something else")   }}

Output results

type Match
The exception type matches, the first type matches, the match filenotfound,exception itself prints out, including the full package name, the class name, followed by the fill in the information oops

exception(e :Exception){e match{case fileException : FileNotFoundException => println("File Not Found:"+fileException)caseException => println("Exception getting thread dump from $executorId",e)}}

Collection Match
arrays, arrays of specified elements, arrays of specified number of elements, and arrays beginning with an array of elements, set,list consistent
Pattern matching for array
Pass in three elements, assigned to Spark,hadoop,flink (they are three variables)

def data(array:Array[String]){array match{caseArray("Scala") => println("Scala")caseArray(spark,hadoop,flink) => println(spark+" : "+hadoop+" : "+flink)caseArray("Spark", _*) => println("Spark ...")//以Spark开头case _ => println("Unknown")}

class Matching
Case class is equivalent to Java in JavaBean default is the Val type only getter method, through case class can make the class pattern matching

def sayHi(person:Person){person match{      case Student(name,score) => println("I am a studnet:"+name+score)      case Worker(name,salary) => println("I am a worker:"+name+salary)      case _ =>println("Unknow")     }     }

Automatically match to worker (Worker,student is a subclass of person)

Case class generates multiple instances
Case object has only one instance

Generic functions

class Person[T](val content : T){def getContent(id : T) = id + " _ " + content  }val p = new Person[String]("Spark")p.getContent("Scala")//必须为String 类型

Type System

generic classes, generic functions, type definitions (upperbound), view Bounds, covariance, and contravariance

Type parameters, mainly used for type qualification, to enhance the robustness of the Code

1. Generic classes and generic functions

2. Type definition

A type can also qualify a boundary, Upperbounds (<: denotes, upper boundary, is it or its subclass), (A;: Represents, the next boundary, is the parent of a class, or the class itself)

3. View definition

View definition (Bounds)
View Bounds Implicit conversion
Implicit implicit conversion of a type

implict  def rddToSequendeFileRDDFuncions[K <% Wirtable:ClassTag,V <% Writable:ClassTag]

Type [T], the implicit value is injected into the context, and the injected process is automatic

class Compare[T :ordering](val n1:T,val n2:T){def bigger(implicit ordered :Ordering[T])=}

Manifest->classtag
T:classtag,t is generic, run-time has a more complete type match, and reflection gets the type of information

Scala pattern matching, type system and spark source reading

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.