Functions __scala columns in Scala--scala

Source: Internet
Author: User
Tags anonymous function definition

First, the foregoing

The functions in Scala are still more important, so this article enumerates the functions that may be used in Scala and explains them in detail.

Second, the specific function

1, the definition of Scala function

def fun (A:int, b:int): unit = {
   println (a+b)
 }
Fun (1,1)
    
def fun1 (a:int, b:int) = A+b
    printl N (fun1 (1,2))  

Note the point:

The function definition syntax is defined in def.

L can define the parameters passed in, to specify the type of incoming arguments

methods can write the return value of the type can also not write, will automatically infer , sometimes can not be omitted , must be written, such as in a recursive function or the return value of the function is the function type.

When a function in Scala has a return value, it can write return or do not write returns, and the last line in the function will be returned as the result. you must write the return value of the function when you write back.

if the return value can be done in one row, you can omit {} without writing

The parameters passed to the method can be used in the method, and Scala prescribes that the parameters passed by the method are Val, not var.

If you remove the equal sign in front of the method body, the method return type must be the unit of the. this argument, regardless of the logic in the method body, Scala can convert any type to unit. Assuming that the logic in the end returns a string, the return value is converted to unit and the value is discarded.

2. Recursive function

  /**
     * Recursive function 
     * 5 factorial/
    def fun2 (num:int): int= {
      if (num ==1)
        num
      else 
        num * FUN2 (num-1 )
    }
    Print (Fun2 (5))

3, The function that contains the default value of the parameter

function of the default value, if the number of arguments passed in is the same as the function definition, the passed-in value overrides the default .

If you do not want to overwrite the default value, and the number of arguments passed is less than the parameter of the defined function, you need Specifies the parameter name.

* * *
     The function that contains the default parameter values
     * NOTE:
     * 1. In a function of the default value, if the number of arguments passed in is the same as the function definition, the passed-in value overrides the default of
     * 2. If you do not want to overwrite the default value, the number of parameters passed is less You need to specify the parameter name
     */
    def fun3 (a:int = 10,b:int) = {
      println (a+b)
    }
    fun3 (b=2)

 

4, the function of the number of variable parameters

Multiple arguments separated by commas

   /**
     * Functions with variable number of parameters
     * Note: multiple arguments comma
     separate
    /def fun4 (elements:int*) ={
      var sum = 0;
      For (Elem <-elements) {
        sum = elem
      }
      sum
    }
    println (Fun4 (1,2,3,4))

 

5. Anonymous function

A parameter anonymous function

No parameter anonymous function

anonymous function with return value

You can return an anonymous function to the value defined by Val

An anonymous function cannot explicitly declare the return type of a function

/**
     * Anonymous function *
     1. Have parameter anonymous function *
     2. No parameter anonymous function *
     3. Anonymous function with return value
     * NOTE:
     * You can return an anonymous function to a defined variable/
    // Parameter anonymous function
    val value1 = (a:int) => {
      println (a)
    }
    value1 (1)
    //No parameter anonymous function
    val value2 = () => {
      println ("I love ...)." ")
    }
    value2 ()
    ////has the return value of the anonymous function
    val value3 = (a:int,b:int) =>{
      a+b
    }
    println ( Value3 (4,4)) 

6. Nested functions

/**
     * Nested functions
     * For example: nested function 5 factorial
    /def fun5 (Num:int) ={
      def fun6 (a:int,b:int): int={
        if (a = = 1 ) {
          b
        }else{
          fun6 (a-1,a*b)
        }
      fun6 (num,1)
    }
    println (Fun5 (5))

 

7. Partial Application function

The biased application function is an expression that does not need to provide all the parameters required by the function, only the provision of a part, or the required parameters are not provided.

    /**
     * Bias application function
     /
    def log (date:D ate, s:string) = {
      println ("date is" + Date + ", log is" + s)
    }
    
    Val da Te = new Date () log (date, "Log1") log (date, "log2") log (date, "
    log3")
    
    //Want to call log, the above change is the second argument,
    val logwithdate = log (date,_:string)
    logwithdate ("log11")
    logwithdate ("Log22"
    ) can be processed with a biased application function Logwithdate ("Log33")

 

8. higher-order functions

The function's argument is a function, or the return type of the function is a function, or the function's arguments and the return type of the function are functions.

function is a function of the parameter

The return of a function is a function

The function's arguments and functions are returned by the function

/** * higher-order function The parameter of the function or function is the function or the function of the parameter and return is the function
     //
    
    /function of the parameter is function
    def hightfun (f: int,int =>int, a:int): int = {
      F (a,100)
    }
    def f (v1:int,v2:int): int  = {
      v1+v2
    }
    
    println (h Ightfun (f, 1))
    
    //function return is the function
    //1,2,3,4 add
    def hightFun2 (a:int,b:int): (int,int) =>int = {
      def f2 ( V1:int,v2:int): Int = {
        v1+v2+a+b
      }
      f2
    }
    println (hightFun2 (1,2) (3,4))
    
    //function parameters are functions, The return of a function is a function because the return of the function is a function  , so it can be understood that the return of the call HIGHTFUNC2 (1,2) is a function, so a parameter must be passed in.
    def hightFun3 (f: (int, int) => int): (int,int) => int = {
      f
    } 
    println (HightFun3 (f) (100,200)) C29/>println (HightFun3 (a,b) =>{a+b}) (200,200)/
    /The above sentence can also be written in this way
    ///If the parameter of the function is used only once in the method body, it can be written as _
    println (HightFun3 (_+_) (200,200))

9. The function of Gerty

Can be understood as the simplification of higher order functions

  /**
     * Gerty function/
    def fun7 (A:int,b:int) (c:int,d:int) = {
      a+b+c+d
    }
    println (Fun7 (1,2) (3,4))

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.