Big Data series Cultivation-scala Course 06

Source: Internet
Author: User

about regular expressions in Scala in conjunction with pattern matching reg

The implementation of regular expressions: Regular expressions are defined in the same way as other languages, simply by adding an. R to the expression, and you can iterate through the corresponding expression to match

 //  regular expression defined  val regex= "" "([0-9]+) ( [a-z]+] "". R //  A constant of numbers and letters  Val Numpattern = "[0-9]+". R //  constant with numbers  Val Numberpattern = "" "\s+[0-9]+\s+" "". R //  by S and number +    The regular expression composed of S   println (regex);  //  Findallin method returns an iterator that iterates through all occurrences  Span style= "color: #0000ff;" >for  (matchstring <-numpattern.findallin ("99345 Scala, 22298 Spark" ))    println (matchstring)  //  Find first match  println (Numberpattern.findfirstin ("99ss Java, 222 Hadoop")) 

Reg implementations combined with pattern matching: Regular expressions may often be used in the future to combine pattern-matching

// Regular Expressions Combined    with pattern matching // Regular Expressions    // prescribed mode    Val line = "93459 spark" line    match{      case numitempattern (num, blog) = println (num + " \ t "+ Blog      " case _ = = println ("Oops ...")    } 
native functions and class-a civic functions in Scala

Local function Implementation: The function is included, which makes the function more secure.

  // a local function is a function that contains a function so that the inside function can use the arguments of the outside function def processdata (filename:string, Width:int) {    def processline (line:string) {      if ( Line.length > width)        + ":" + line )    }    = source.fromfile (filename)      for (Line <- source.getlines)      processline (line)  } }

Implemented as a class-a citizen function; enables more convenient use of function representations in the process

  //Class Citizen functiondef main (args:array[string]): Unit = {     //parameters set in the startup itemVal width = args (0). ToInt for(Arg <-args.drop (1) ) ProcessData (arg, width) var increase= (X:int) + x + 1println (Increase (10)) Increase= (X:int) + x + 9999Val somenumbers= List (-11,-10,-5, 0, 5, 10)      //iterating through an arraySomenumbers.foreach ((x:int) =print (x)) println//filter greater than 0Somenumbers.filter ((X:int) = x > 0). foreach ((x:int) =print (x)) println//filter greater than 0Somenumbers.filter ((x) = x > 0). foreach ((x:int) =print (x)) println//filter greater than 0Somenumbers.filter (x = x > 0). foreach ((x:int) =print (x)) println//filter greater than 0Somenumbers.filter (_ > 0). foreach ((x:int) =print (x)) println//define two numbers to addVal F = (_: Int) +(_: Int) println (f (5, 10))   }
How to explain the partial function in Scala

Partial function Implementation: Partial function can make the expression more concise, there is no need to write extra parameters

Partial function parsing: When the partial function is implemented, the Apply function is generated to perform the corresponding operation

 val data = List (1, 2, 3, 4, 5, 6)  //   Data.foreach (println _) Data.foreach (x  => PR INTLN (x)) def sum (A:int, B:int, c:int)  = a + b + C println (sum ( 1, 2, 3 = sum _  //  call define the SUM function directly, invoke its corresponding apply function  println (fp_a (1, 2, 3)) println (Fp_a.apply ( 1, 2, 3)) val fp_b  = SUM (1, _: Int, 3) println (Fp_b ( 2 10)) d Ata.foreach (println _) Data.foreach (println)  

about closures in Scala

Scala closure parsing: Let the function body implement redundant things with simple expressions

Scala Closure implementation

    def main (args:array[string]) {            = List (1, 2, 3, 4, 5, 6)    = 0    + = _)    = (X:int) + x +    more = Add (1)    = Add (9999)    println (A (  ))        println (b (10)

Scala's functional programming is really profound, a lot of things have been achieved by his bottom, which allows us to be more concise in the use of the process. We'll be here today.

Welcome to watch Liaoliang's big Data related video ....

Liaoliang No.: 18610086859

Latest Videos 72 Speaks: http://pan.baidu.com/s/1qWsq6Jy

The blog Video 19-22 Speaks: Http://pan.baidu.com/s/1o63QbPo

Big Data series Cultivation-scala Course 06

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.