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