码迷,mamicode.com
首页 > 其他好文 > 详细

[Learning Scala] Functions

时间:2015-12-31 17:28:23      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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. 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)}\n")

    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}\n")
    print(s"test2: ${safeStringOp("hello world        !", trimString)}, length is: ${safeStringOp("hello world        !", trimString).length}\n")
    print(s"test3: ${safeStringOp("", trimString)}\n")

    jump("Function Literals")
    // method 1
    def greeter(name: String): String = s"hello ${name}\n"
    val greeter1 = greeter _
    // method 2 function literals
    val greeter2 = (name: String) => s"hello ${name}\n" // 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)}\n")

    val moveItOn = () => "=" * 50 + "getting started" + "=" * 50
    print(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 parameter
    print(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())}") // elegant

    jump("Placeholder Syntax")
    print(s"test6: ${safeStringOp("hello world    ", _.trim())}\n") // placeholder

    def 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 == 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"\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 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)

  }
}

 

[Learning Scala] Functions

标签:

原文地址:http://www.cnblogs.com/alexander-chao/p/5092294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!