Sample type and pattern matching

A: sample class is declared using the keyword class case, and the general category of use are the same.

package sample classes and pattern matching 


/ ** 
  * @author: Alex_lei 
  * @Description: 
  * / 
Object Case_class { 

  / ** 
    * people is a sample class 
    * @param name the name attribute 
    * @param age age attribute 
    * / 
  Case class people (name : String, Age: Int) 
  DEF main (args: the Array [String]): Unit = { 
    Val people P1 = ( "Alex", 23 is) 
    Val people P2 = ( "Lily", 22 is) 

    the println (P1) 

  } 
}

 Two: Pattern Matching

  Mode is divided into: a wildcard, constant mode, the variable mode, the configuration mode, sequential mode tuple pattern, pattern type.

  Code Example:

  

Sample package type and pattern matching 


Import Math {E, Pi}. 
/ ** 
  * @author: Alex_lei 
  * @Description: Several types of pattern 
  * / 
Object Mode { 
  Case class people (name: String, Age: Int) 
  DEF main (args: the Array [String]): Unit = { 
    / ** 
      * test wildcard 
      * / 
    catch_all (. 3) 
    the println (DESCRIBE (. 5)) 

    / ** 
      * variable mode 3. in this case, pi is the variable mode, so it can match any value, the subsequent value does not match. 
      * / 

    Val PI Pi = 
    Val E RES = {match 
      Case PI => "PI" 
      Case _ => "the OK" 
    } 
    the println (RES) 

    / ** 
      * Test configuration mode 
      * / 
    Val people P = ( "Alex", twenty two)
    Constructor_mode (P) 

    / ** 
      * 5. The sequence patterns, such as matching type List or the Array 
      * / 
    Val = L List (l, 2,3) 
    L match { 
      Case List (_, _, _) => the println ( "List") 
      Case _ => the println ( "something the else") 
    } 

    / ** 
      * 6. The tuple pattern 
      * / 

    Val T = (. 1, "Alex", 22 is) 
    T {match 
      Case (_, _, _) => the println ( "tuple") 
      Case _ => the println ( "something the else") 
    } 

    / ** 
      * type test mode 
      * / 
    Val S = "Alex" 
    type_mode (S) 
    Val the Map m = (. 1 -> '2' 2 -> '3')
    type_mode(m)

    val m1 = Map("alex" -> "name","Lily" -> "name1") 
    the println (Show (M1 GET "alex"))
  }

  /**
    1. * wildcard symbols: _ matches any object 
    * / 
  DEF catch_all (A: Int): Unit = { 
    A {match 
      Case. 1 => the println ( 'A') 
      Case 2 => the println ( 'C') 
      Case _ => the println ( '_') 
    } 

  } 

  / ** 
    * 2. constant mode: constant used to match basic types of constants and val or singletons constants may also be used. 
    @Param X * 
    * / 
  DEF DESCRIBE (X: the Any) = { 
    X {match 
      Case. 5 => "Five" 
      Case to true => "Truth" 
      Case "Hello" => "Hi" 
      Case Nil => "The empty List" 
      _ = Case> "something the else" 
    } 
  } 

  / ** 
    *. 4.
    p match {
      case people(_,_) => println("people")
      case _ => println("any")
    }
  }

  /**
    * 7.类型模式
    * @param x
    */
  def type_mode(x:Any) = {
    x match {
      case s: String => println(s.length)
      case m: Map[_,_] => println(m.size)
      case _ => println("something else")
    }
  }

  def show(x:Option[String]) = x match {
    case Some(s) => s
    case None => "?"
  }

}

 

 Three: guard mode

  Action: mainly to a more accurate matching mode, the main condition is to add some filters on match.

  Code Example:

  

Sample package type and pattern matching 

/ ** 
  * @author: Alex_lei 
  * @Description: guard mode, primarily in order to more accurately match the pattern 
  * / 
Object Mode_guard { 
  DEF main (args: the Array [String]): Unit = { 
    / * * 
      * test pattern guard 
      * / 

    Val A = -10 
    Print ( "A = -10:") 
    m (A) 

    Val B = 10 
    Print ( "B = 10:") 
    m (B) 

    Val S = "ABC" 
    Print ( "S = ABC:") 
    m (S) 

    Val T = "A" 
    Print ( "T = A:") 
    m (T) 
  } 

  DEF m (X: the Any) = X match { 
    Case X: Int IF X> = 0> the println ( "Int")  
    Case X: String IF x.length> 2 =>println("String")
    Case _ => the println ( "the Any")
  }
}

Four: Option Type 

  Option type value of only two types, may be s Some (x), where x is the actual value, the object may be None, Some and None are its subclasses, are modified using the final, there can not be subclassed.

  Code Example:

Sample package type and pattern matching 

/ ** 
  * @author: Alex_lei 
  * @Description: option type value of only two types, may be s Some (x), where x is the actual value, the object may be None 
  * Some and None are its subclasses, final modification is used, then there can be subclassed 
  * / 
Object {Option- 
  DEF main (args: the Array [String]): Unit = { 
    Val Book the Map = ( "the Hadoop" -> 10, "the Spark "-> 20 is," Flink "-> 30," Scala "-> 40) 

    Val book.get T = (" the Hadoop ") // Option [Int] = s Some (10) return value is Option [Int] type 
    println (T) 

    / ** 
      * the following two examples illustrate, when there is a return value, the result is not returned parameters getOrElse 
      * when no return value, the result will be a return parameter getOrElse 
      * / 
    Val T1 = book.get ( "Hbase"). getOrElse ( "No key") //No key
    println(t1)

    val t2 = book.get("Spark").getOrElse("yes") //20
    println(t2)
  }
}

 Five: enclosing class

  Enclosing class, using the sealed keyword for pattern matching, pattern matching when we do with sample class, let the compiler help us ensure that lists all possible, we need to sample a superclass be closed . But when the pattern matching, it is necessary to add a comment behind @unchecked expression matching function for subsequent pattern of exhaustive checks would have been suppressed. No warning appears.

  Code Example:

  

package sample classes and pattern matching 



/ ** 
  * @author: Alex_lei 
  * @Description: a closed class, using the sealed keyword for pattern matching, when we use 
  pattern matching * Sample class to do, want to help us ensure that the compiler lists all 
  possible *, we need to sample a superclass be closed. 
  * / 
Object Sealed { 
  DEF main (args: the Array [String]): Unit = { 

    Val Number The T = (2) 
    Val DESCRIBE RES = (T) 
    the println (RES) 
    des (T) 
    desc (T) 


  } 

  / ** 
    * Warning: (16, 33 is) match On May Not BE Exhaustive. 
    * It Would Fail ON The following Inputs: binop (_, _, _), UNOP (_, _) 
    * DEF DESCRIBE (X: Expr): String = X match { 
    * 
    * written warning appears that there are two possibilities 
    *
    * There are two ways to solve, namely des desc function and function 
    * des plus a function primarily in the wildcard 
    * desc function is to add a comment behind @unchecked expression to match, the function for subsequent pattern of poor For check will be squelched 
    * / 
  DEF DESCRIBE (X: Expr): String = X match { 
    Case Number the (_) => "A Number" 
    Case Var (_) => "A String" 
  } 

  DEF des (X: expr): String = X match { 
    Case Number The (_) => "A Number" 
    Case Var (_) => "A String" 
    Case _ => the throw new new a RuntimeException 
  } 

  DEF desc (X: expr): String = (X : @unchecked) match { 
    Case Number The (_) => "A Number" 
    Case Var (_) => "A String"
  } 
} 

/ ** 
  * define a closed class Expr 
  * / 
Sealed abstract class Expr 

/ **
  * Define four sample class inherits Expr 
  * 
  * / 
Case class Var (name: String) the extends Expr 
Case Number The class (NUM: Double) the extends Expr 
Case class UNOP (operator: String, args: Expr) the extends Expr 
Case class binop (operator : String, left: Expr, right : Expr) extends Expr

 

Guess you like

Origin www.cnblogs.com/lyr999736/p/11373274.html