Advanced features of Scala functions

1. Calling by name

summary
The function will be called only when the return value of the function is really used
example
Normal mode call
code
 

 def main(args: Array[String]): Unit = {
    f2(f1())
  }
  def f1(): Int ={
    println("f1")
    return 1
  }
  def f2(a:Int): Unit ={
    println("f2")
    println(a)
  }
  - Output results

Output results:

f1
f2
1
  - conclusion : Although no value is used a,But it will be executed first f1

Call by name
Syntax: parameter: = > type. Note that there must be a space after the colon
code

  def main(args: Array[String]): Unit = {
    f2(f1())
  }
  def f1(): Int ={
    println("f1")
    return 1
  }
  def f2(a: =>Int): Unit ={
    println("f2")
    println(a)
  }
  - Output results
f2
f1
1
  - conclusion : Only when using variables a Method will be called when f1

2. Call by specifying parameter name

summary
When there are too many parameters, you can't remember the order of the parameters. You can specify the parameter name to pass the value
Example
 

  def f3(name:String,age:Int)={
      println(name+age)
    }
    f3(age=10,name = "Zhang San")

3. Parameter default value

summary
Set the default value for the parameter. If the parameter is not passed, the default value will be used
Example

def f5(name:String="antg")={
      println(name)
    }
f5()

4. Variable length parameter

summary
This technique can be used when the number of parameters is uncertain
Example

def f4(str:String*): Unit ={
      str.foreach(x=>println(x))
    }
f4("1","2","3")

5. Recursive function

summary
The function itself is a recursive function
Pay attention to two points
Termination condition of recursion
Recursive formula
Example

def f6(n:Int):Int={
      if(n==1){
        return 1
      }
      n*f6(n-1)
    }
    println(f6(5))


6. Higher order function

summary
The parameter or return value is a function of the function, i.e. a higher-order function
Example
 

   //Parameters are functions of functions
    def f7(f:(Int)=>Unit)={
      f(10)
    }
    f7((x:Int)=>println(x))
    //The return value is the function of the function
    def f8(y:Int):(Int)=>Unit={
      return (x:Int)=>println(x*y)
    }
    var f8r = f8(10)
    f8r(2)


7. Embedded function

summary
Functions can be re created
Example    

 //Nested Function 
    def f9()={
      def f()={
        println("This is an embedded function")
      }
      f()
    }
    f9()


8. Anonymous function

summary
Function without name
Use = > to separate the function definition from the function body
Example
        

//Anonymous function
    val f10 = (x:String)=>println(x)
    f10("123")

9. Partial application function

summary
If a function is called multiple times and one of the variables remains unchanged, partial application functions can be used
Example
 

  //Partial application function
    def f11(name:String,hello:String)={
      println(name+"--"+hello)
    }
    val f12 = f11("Zhang San",_)
    f12("good morning")
    f12("Good noon")
    f12("Good evening")

10. Function coritization

summary
In computer science, Currying is a technology that transforms a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a new function that accepts the remaining parameters and returns the result.
In short, coriolism is computer technology that fixes some parameters and then returns a function with only one parameter
Example
    // Function coritization
    // Implementation of traditional add method
 

  def f13(a:Int,b:Int)={
      println(a+b)
    }
    f13(10,20)
        //Coriolis implementation
    def f14(a:Int)=(b:Int)=>println(a+b)
    def f15(a:Int)(b:Int)=println(a+b)
    //use
    val f16 = f14(3)
    f16(4)
    f16(5)
    f16(6)
    var f17 = f15(3)(_)
    f17(4)
    f17(5)
    f17(6)

Keywords: Scala Back-end

Added by Tonic-_- on Sat, 04 Dec 2021 20:26:58 +0200