[Learning Scala] Functions

7505 ワード

object SparkTest004 {
  def main(args: Array[String]): Unit = {
    /**
     * Chapter 5 functions
     */

    def jump(title: String = ""): Unit = print(s"
---------${title}-----------
") jump("Function Types and Values") def doubleNumbers (x: Int): Int = x * 2 // here the value "myDoubleNumbers is a type of function. Its implementation is "function: doubleNumbers" val myDoubleNumbers: (Int) => Int = doubleNumbers // Function types with a single parameter can leave off the parentheses. val copyMyDoubleNumbers = myDoubleNumbers // function copy print(s"double of 5 is: ${myDoubleNumbers(5)}, copyMyDoubleNumbers as new function: ${copyMyDoubleNumbers(5)}
") def max(x: Int, y: Int, z: Int): Int = { def max(x: Int, y: Int): Int = if (x < y) y else x 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 method print(s"double of 5 is: ${myDoubleNumbersWithNoFunctionTypeDeclare(5)}") jump("Higher-Order Functions") def safeStringOp(s: String, f: String => String): String = { // using function to be a parameter if (s != null) f(s) else s } def trimString(s: String): String = s.trim() print(s"test1: ${safeStringOp("hello world ", trimString)}, length is: ${safeStringOp("hello world ", trimString).length}
") print(s"test2: ${safeStringOp("hello world !", trimString)}, length is: ${safeStringOp("hello world !", trimString).length}
") print(s"test3: ${safeStringOp("", trimString)}
") jump("Function Literals") // method 1 def greeter(name: String): String = s"hello ${name}
" val greeter1 = greeter _ // method 2 function literals val greeter2 = (name: String) => s"hello ${name}
" // function literals print(greeter2("gavin")) val maximize3 = (x: Int, y: Int) => if (x > y) x else y // compare to maximize and maximize2 print(s"function literals maximum: ${maximize3(23, 97)}
") val moveItOn = () => "=" * 50 + "getting started" + "=" * 50 print(s"invocation the function: ${moveItOn()}
") print(s"the function type is: ${moveItOn}
") // instead of using a function value, we can also use function literals as a parameter print(s"test4: ${safeStringOp("hello world ", (s: String) => s.trim())}, " + s"length is: ${safeStringOp("hello world ", (s: String) => s.trim()).length}
") print(s"test5: ${safeStringOp("hello world ", s => s.trim())}") // elegant jump("Placeholder Syntax") print(s"test6: ${safeStringOp("hello world ", _.trim())}
") // placeholder def combination(x: Int, y: Int, f: (Int, Int) => Int) = f(x, y) print(s"the combination result is: ${combination(3, 5, _ - _)}
") print(s"the combination result is: ${combination(5, 3, _ - _)}
") 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, _ + _ + _)}
") 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, _ + _ + _)}
") jump("Partially Applied Functions and Currying") def factorOf(x: Int, y: Int) = y % x == 0 val f = factorOf _ val factorOf3 = f(3, _: Int) // the type "Int" must be specified print(factorOf3(9)) // partially apply function //curring the function: breaking up the parameters into two list def factorof2(x: Int)(y: Int) = y % x == 0 def isEven = factorof2(2) _ print(s"
Is 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 will get into it twice ~ ~ jump("Partial Functions") // Such functions are called partial functions because they can only partially apply to their input data. val statusHandler: Int => String = { case 100 => "Okay" case 400 => "error 400" case 500 => "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) } }