Scala-- 3. Functions

In Scala, almost equivalent methods and functions (such as their definitions, used, operating mechanisms are the same), but a more flexible use of the function. Functional programming from the perspective of the programming mode (paradigm) of the talk, it can be appreciated that: the functional programming functions as a first class citizen, making full use of, but use a variety of functions, in Scala, the function is a class citizens , like a variable, either as a function of the parameters, you can also assign a function to a variable, create function without relying on a class or object, which in Java, create a function that will have to depend on the class, abstract class or interface. In Scala functional programming and object-oriented programming fused together.

1. Functional Programming

Function is a programming paradigm programming (programming paradigm).
Functional programming belongs to the "structured programming", the main idea is to try write operation during a series of nested function calls.
Functional programming, but also a function as a data type, as can also accept as input function (parameter) and output (return value).
Functional programming, the most important thing is to function.

2. functional form

def function name ([Parameter name: Parameter Type], ...) [[: Return Value Type] =] {

Statement ...

return return value

}

# =表示返回值类型不确定,需要类型推导完成
  def getRes(n1: Int, n2: Int, oper: Char) = {
    if (oper == '+') {
      n1 + n2 //返回
    } else if (oper == '-') {
      n1 - n2
    } else {
      //返回null
      null
    }
  }

A. free to infer the type of function return value as a function of the body last line of code
DEF GetSum (N1: Int, N2: Int): Int = {
N1 + N2
}
** last expression is the function return value **
If the function is not parameter, the call can not bring ()

B. If the function explicitly use the return keyword, then the function returns the inference can not be used on their own, this time to clearly written as: return type =, of course, if nothing to write, even if there is return the return value is ()

package hello

/**
 * @author Administrator 
 * @date 2020/2/16 9:29
 */
object Test1 {

  def main(args: Array[String]): Unit = {
    # 输出()
    print(fun1(1, 2))
    # 输出3
    print(fun2(1, 2))
  }

  def fun1(i: Int, j: Int) {
    return i + j
  }

  def fun2(i: Int, j: Int): Int = {
    return i + j
  }
}

C. nested functions

object boke_demo01 {
 
  def main(args: Array[String]): Unit = {
 
    def f1(): Unit = { //ok private final
      println("f1")
    }
 
    println("ok~~")
 
    def sayOk(): Unit = { // private final sayOk$1 ()
      println("main sayOk")
 
      def sayOk(): Unit = { //  private final sayOk$2 ()
        println("sayok sayok")
      }
    }
 
  }
 
  def sayOk(): Unit = { //成员
    println("main sayOk")
  }
}

D. function parameter default values

object boke_demo01 {
 
  def main(args: Array[String]): Unit = {
    println(sayOk("mary"))
  }
 
  //name形参的默认值jack
  def sayOk(name: String = "jack"): String = {
    return name 
  }
}

E. function with parameter name

object boke_demo01 {
 
  def main(args: Array[String]): Unit = {
    //    mysqlCon()
    //    mysqlCon("127.0.0.1", 7777)   //从左到右覆盖
 
    //如果我们希望指定覆盖某个默认值,则使用带名参数即可,比如修改用户名和密码
    mysqlCon(user = "tom", pwd = "123")
 
    //f6("v2")  // (错误)
    f6(p2 = "v2") // (?)
 
  }
 
  def mysqlCon(add: String = "localhost", port: Int = 3306,
               user: String = "root", pwd: String = "root"): Unit = {
    println("add=" + add)
    println("port=" + port)
    println("user=" + user)
    println("pwd=" + pwd)
  }
 
  def f6(p1: String = "v1", p2: String) {
    println(p1 + p2);
  }
 
}

F. before the recursive function not implemented can not be inferred from the result type, must have a clear return type in use

 def f8(n: Int) = { //?错误,递归不能使用类型推断,必须指定返回的数据类型
    if (n < 0)
      1
    else
      n * f8(n - 1)
 
}

G: function of the variable parameter

//支持0到多个参数
def sum(args : Int*) : Int = {
}
//支持1到多个参数
def sum(n1 : Int, args : Int*) : Int = {
}

3. Special functional form process (Procedure)

F10 DEF (name: String): Unit = {
the println (name + "Hello")
}
does not return value if the type function declaration, but the equal sign (=), type inference may be the last line of code. Then this is the actual function returns a value, the function is not a process.

4. Inert function

When the function returns a value to be declared as lazy, execution of the function will be delayed until the first time this value, will perform this function, this function is called inertia function.

object boke_demo01 {
 
  def main(args: Array[String]): Unit = {
    lazy val res = sum(10, 20)
    println("-----------------")
    println("res=" + res) //在要使用res 前,才执行
  }
 
  //sum函数,返回和
  def sum(n1: Int, n2: Int): Int = {
    println("sum() 执行了..") //输出一句话
    return n1 + n2
  }
 
}

lazy type of variable var can not be modified, not only when you call the function, plus a lazy, will result in the execution of the function is delayed, when you declare a variable, if to declare a lazy, then assign the value of the variable will be postponed.

————Blueicex 2020/2/15 18:06 [email protected]

Published 58 original articles · won praise 0 · Views 2098

Guess you like

Origin blog.csdn.net/blueicex2017/article/details/104338276