Big Data series cultivation-scala Course 07

Source: Internet
Author: User

Because yesterday after work a bit sleepy, did not come and write a blog, today will fill it up! Keep this habit going!

More about Scala's higher-order functions

1.Scala High-order function code implementation: higher-order functions are applied in our functions

2. Higher-order function code details: higher-order functions can make method invocation more convenient

println ("Welcome to the Scala worksheet")//> Welcome to the SCALA worksheet//(1 to 9) The map method in the array is placed in the array * using foreach to loop println _ to represent the function, passing the function as a parameter(1 to 9). Map ("*" * _). foreach (println _)//> *//| **                                                  //| ***                                                  //| ****                                                  //| *****                                                  //| ******                                                  //| *******                                                  //| ********                                                  //| *********  //traversal println, which is divisible by 2 after filtering, is also passed as a parameter, which is the higher order function.(1 to 9). Filter (_% 2 = = 0). foreach (println)//> 2//| 4//| 6//| 8//_*_ represents the multiplication of two data in an array also belongs to the higher order functionprintln ((1 to 9). Reduceleft (_ * _))//> 362880//divide a string into a space by its length"Spark is the most exciting thing happening in big data Today". Split (""). Sortwith (_.length<_.length). foreach (println)//> is//| in//| the//| Big//| most//| data//| Spark//| thing//| Today//| exciting//| Happening//The ceil function represents an upward valueVal Fun = Ceil _//> fun:double = Double = <function1>Val num = 3.14//> num:double = 3.14Fun (num)//> res0:double = 4.0Array (3.14, 1.42, 2.0). Map (Fun)//The fun function is passed as a function//> Res1:array[double] = Array (4.0, 2.0, 2.0)//function represents a multiple of threeval triple = (x:double) = 3 * x//> triple:double = Double = <function1>Array (3.14, 1.42, 2.0). Map (x:double) = 3 *x)//> Res2:array[double] = Array (9.42, 4.26, 6.0)Array (3.14, 1.42, 2.0). map{(x:double) = 3 *x}//> Res3:array[double] = Array (9.42, 4.26, 6.0)//defining higher order functionsDef high_order_functions (f: (double) + = double) = f (0.25)                                                  //> High_order_functions: (f:double = double) Doubleprintln (High_order_functions (ceil _))//> 1.0println (High_order_functions (sqrt _))//> 0.5//function Multiplicationdef mulby (factor:double) = (x:double) = factor *x//> Mulby: (factor:double) Double = DoubleVal quintuple = Mulby (5)//> quintuple:double = Double = <function1>println (Quintuple (20))//> 100.0//Multiple resolution of 3println (high_order_functions (x:double) = 3 *x))//> 0.75High_order_functions ((x) = 3 * x)//> res4:double = 0.75High_order_functions (x = 3 * x)//> res5:double = 0.75println (High_order_functions (3 * _))//> 0.75Val fun2= 3 * (_: Double)//> fun2:double = Double = <function1>Val fun3: (double) = = Double = 3 * _//> fun3:double = Double = <function1>
Sam Conversion tutorials in Scala

The meaning of 1.SAM: implicit conversion

2.SAM conversion in a detailed

var data = 0Val Frame=NewJFrame ("SAM testing"); Val JButton=NewJButton ("Counter") Jbutton.addactionlistener (NewActionListener {override def actionperformed (event:actionevent) {data+ = 1println (data)}) //implicit functions define functions like function definitions in Java--and this can be implicitly convertedImplicit def convertedaction (action: (actionevent) = Unit) =NewActionListener {override def actionperformed (event:actionevent) {action}}//This is when the code is written only to care about business logic, there is no need to write the non-relational code, directly write the desired resultsJbutton.addactionlistener ((event:actionevent) = {Data + = 1; println (data)}) //this is the same as GUI programming in Java.Frame.setcontentpane (JButton);      Frame.pack (); Frame.setvisible (true);

curring in Scala

Definition of 1.curring:curring particle size, is to simplify the parameter into one parameter to operate, return to the other calculation

2.curring importance in the project: very important in the push-to-neutralize calculation of the formula

    // curring particle size, is to simplify the parameter into one parameter to operate, return to the other calculation    def multiple (x:int, y:int) = x * y    = (y:int) + x * y    println (Multipleone (6) (7))    /c8>// can put the above calculation method for the following methods    def curring (X:int) (Y:int) = x * y        println (curring ( (Ten    )) = Array ("Hello", "Spark"    )= Array ("Hello", "Spark")     // compare two variables for equality    println (a.corresponds (b) (_.equalsignorecase (_)))

Getting started with pattern matching in Scala

1. Pattern matching analysis: Scala matching mode is flexible, can pass in parameters and functions, etc.

2. Use guard in pattern matching: The next blog will be explained in depth

3. Use of variables in pattern matching

  //pattern matching is represented in Java with a switch, which is not a lot of restrictions, Int,byte,char,shortVal Data =2Data Match { Case1 = println ("First")       Case2 = println ("Second")       Case_ = = println ("Not known number")    }    //In Scala, you can use conditional functions or functions, or you can pass argumentsVal result =Data Match { CaseIifi = = 1 = "The first" CaseNumberifNumber ==2 = "The Second" + Number Case_ = "not known number"} println (Result)"Spark!" foreach {c = =println (c match { Case' = ' and ' space ' Casech = "Char:" +ch})}

This is a default task, and today we will write another study on Scala! I hope you are concerned about Liaoliang Teacher's (18610086859), he will update the big Data video every day!

The latest big Data video 74 speaks: Http://pan.baidu.com/s/1hqJByvU

This article Baidu Cloud address 23-26 Speaks: Http://pan.baidu.com/s/1bOsSQ

Big Data series cultivation-scala Course 07

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.