8. High-order function

Source: Internet
Author: User

Common functions

Higher order functions: parameters or return values are functions

Object A_ Base {  = {    println (Mymax (1,2, Math.max))    println (Myadd ( 1) (2))  }   = ={//f Type of mapping is function    f (A, B)  }  def myadd (a:int)={//return value type is a function, inner _ represents the function itself    def inner (b:int)={      + b    }    inner _  }}

anonymous functions

Scala> Arr.map ((i:int) = i*2= Array (24) Scala> Arr.map ((i:int ) = = i*2= Array (24)
Scala> Arr.map ((i) = i*2= Array (24) Scala> arr.map (i = > i*2= Array (24)
Scala> Arr.map (_*2= Array (24)

Currying

Definition: The process of turning a function that accepts two parameters into a new function that takes a parameter. That is, the function after the curry returns the function with the second argument as the argument.

Scala> def Add (a:int,b:int) =a+b//original function add: (A:int, B:int) Int^Scala> Def add (a:int) ={b:int = A += Intscala> Add (1) (23Scala> Def add (a:i NT) (b:int) =a + b//Curry simplified add: (a:int) (b:int) Intscala> Add (3) (47 

Benefit: Take a parameter out alone to provide more information for type inference

Control abstraction

In Scala, a series of statements can be composed of functions with no parameters and no return value

Object C_ Control Abstract {  = {    runinthread (println (Thread.CurrentThread (). GetName ()))    Runinthread (println (Thread.CurrentThread (). GetName ()))  }   = unit): unit ={    new  Thread () {      override def run (): unit ={        block      }    }. Start ()  }}

8. High-order function

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.