Black monkey house: Scala higher-order functions

We can accept the function as an argument, called higher-order functions.

1, the use of a higher-order functions

def highOrderFunction1(f: Double => Double) = f(10)
def minus7(x: Double) = x - 7
val result2 = highOrderFunction1(minus7)
println(result2)

2, the use of higher-order functions II

def highFunction(x:Int,y:Int,f:(Int,Int)=>Double) = f(x,y)
def subF(a:Int,b:Int):Double={
  a*b
}
println(highFunction(2,3,subF))

3, function (name) parameter analysis

// f: => Double indicates that the function f takes no parameters and returns a value of type Double
// f: Int => Double f indicates that the function takes a parameter of type Int, and returns a value of type Double
/ / f: (Int, String) => Double f indicates that the function takes two parameters, a parameter Int type, a parameter type String, and returns a value of type Double

4, the closure currying + + return function type

Higher order functions can also return type of the function

Curried function, higher order function may return type of the function, the same function can be used as the return type, which is called curried function, could have a function of receiving two parameters, it segmentation bit, into two functions, each function takes one argument, which is curried, curried (Haskell Curry) in order to design a conceptual scala not generated, but in the design of functional programming scala inevitable product, is a function object that can be used as another function's return value

Closures
Closures is inside a function parameter out of the function, called closure

def m1(x:Int) = (y:Int) => x-y
println(m1(10))
println(m1(10)(3))
val function1 = m1(10)
val i6 = function1(3)
println("i6-------       "+i6)

Reproduced in: https: //www.jianshu.com/p/ef75d8359e9e

Guess you like

Origin blog.csdn.net/weixin_33910460/article/details/91182467