Object SparkTest004 {def main (args:array[string]): Unit= { /*** Chapter 5 functions*/def jump (title:string= ""): Unit = print (S "\ n---------${title}-----------\ n") Jump ("Function Types and Values") def doublenumbers (x:int): Int= x * 2//Here the value "Mydoublenumbers is a type of function. It implementation is "Function:doublenumbers"Val mydoublenumbers: (int) = = Int = Doublenumbers//Function types with a single parameter can leave off the parentheses.Val copymydoublenumbers = mydoublenumbers//function CopyPrint (S "Double of 5 is: ${mydoublenumbers (5)}, copymydoublenumbers as new function: ${copymydoublenumbers (5)}\n") def max (X:int, Y:int, z:int): Int={def max (X:int, y:int): Int=if(x < y) yElsex Max (x, max (Y, z))} val maximize: (int, int, int)= = Int =Max Val Maximize2=Max _ Print (S"Maximum number of three is: ${maximize2 (1,5,3)}") Jump ("Assigning a Function with the Wildcard Operator") Val Mydoublenumberswithnofunctiontypedeclare= Doublenumbers _//comparing with the above declare methodPrint (S "Double of 5 is: ${mydoublenumberswithnofunctiontypedeclare (5)}") Jump ("Higher-order Functions") def safestringop (s:string, f:string= string): String = {//using function to is a parameter if(s! =NULL) F (s)Elses} def trimstring (s:string): String=S.trim () print (S"Test1: ${safestringop (" Hello World ", trimstring)}, Length was: ${safestringop (" Hello World ", trimstring). L Ength}\n ") print (S"Test2: ${safestringop (" Hello World! ", trimstring)}, Length was: ${safestringop (" Hello World! ", trimstring) . length}\n ") print (S"Test3: ${safestringop (" ", trimstring)}\n") Jump ("Function Literals") //Method 1def greeter (name:string): String = S "Hello ${name}\n"Val Greeter1=greeter _//Method 2 Function LiteralsVal greeter2 = (name:string) + S "Hello ${name}\n"//function LiteralsPrint (Greeter2 ("Gavin")) Val maximize3= (X:int, y:int) =if(x > Y) xElseY//compare to maximize and maximize2Print (S "function literals Maximum: ${maximize3 (+)}\n") Val Moveiton= () = "=" * + "Getting Started" + "=" * 50Print (S"Invocation the function: ${moveiton ()}\n") print (S"The function type is: ${moveiton}\n") //instead of using a function value, we can also use function literals as a parameterPrint (S "test4: ${safestringop (" Hello World ", (s:string) = S.trim ())}," +s"Length is: ${safestringop (" Hello World ", (s:string) = S.trim ()). length}\n") print (S"Test5: ${safestringop (" Hello world ", s = = S.trim ())}")//ElegantJump ("Placeholder Syntax") print (S"Test6: ${safestringop (" Hello World ", _.trim ())}\n")//placeholderdef combination (X:int, y:int, F: (int, int)= + Int) =f (x, Y) print (s"The combination result is: ${combination (3, 5, _-_)}\n") print (S"The combination result is: ${combination (5, 3, _-_)}\n") def triple (X:int, Y:int, Z:int, F: (int, int, int)= + Int) =f (x, Y, z) print (s"Triple Test 1: ${triple (1, 2, 3, _ + _ + _)}\n"def Triple2[a, B] (x:a, y:a, Z:a, F: (A, A, a)= + B) =f (x, Y, z) print (s"Triple Test 2: ${triple2[int, Double" (1, 2, 3, _ + _ + _)}\n ") Jump ("Partially applied Functions and currying") def factorof (X:int, Y:int)= y% x = = 0Val F=factorof _ val factorOf3= f (3, _: Int)//the type "Int" must be specifiedPrint (FACTOROF3 (9))//partially Apply function//curring The function:breaking up the parameters into the listdef factorof2 (X:int) (y:int) = y% x = = 0def IsEven= Factorof2 (2) _ Print (s"\nis 4 even: ${iseven (4)}") Jump ("By-name Parameters") def doubles (X:int)={println ("Invoking the function, value is:" +x) x* 2} def greeter3 (X:int)= {println ("getting into Greeter3"); X} doubles (5) Doubles (Greeter3 (5))//didn ' t get into the Greeter3 twice! the book says it'll get into it twice ~ ~Jump ("Partial Functions") //Such functions is called partial functions because they can only partially apply to their input data.Val Statushandler:int and String = { Case"Okay" Casepage = "Error 400" Case"Error 500" Case_ = "Can ' t handle this case"} println (Statushandler (9)) println (Statushandler (100)) println (Statushandler (400)) println (Statushandler (500)) Jump ("Invoking Higher-order Functions with Function Literal Blocks") Val uuid=java.util.UUID.randomUUID.toString Val Timeduuid= Safestringop (uuid, {s = =)Val now=System.currenttimemillis Val timed= S.take (24) +Now Timed.touppercase ()}) println (UUID) println (Timeduuid)}}
[Learning Scala] Functions