函数式编程-函数对象作为方法的参数
package com.zishi.scala.a02.okk05
object Test05_Lambda01 {
def main(args: Array[String]): Unit = {
def fun33(age: Int): String = {
"Hello:" + age
}
def test(f: (Int) => String): Unit = {
println(f(23))
}
val f2 = fun33 _
test(f2)
def sum(x:Int, y:Int): Int = {
x + y
}
def sub(x: Int, y: Int): Int = {
x - y
}
def test2(fun: (Int, Int) => Int): Unit = {
val i = fun(10, 20)
println(i)
}
test2(sum)
test2(sub)
test2((x: Int, y: Int) => {
x * y
})
val a = (x: Int, y: Int) => {
x / y
}
test2(a)
test2((x, y) => {
x * y
})
test2((x, y) => x * y)
println("-------------------")
test2(_ * _)
def f(func: String => Unit): Unit = {
func("abcde")
}
f((name: String) => {
println(name)
})
println("========================")
f((name) => {
println(name)
})
f(name => {
println(name)
})
f(name => println(name))
f(println(_))
f(println _)
f(println)
}
}
练习
package com.zishi.scala.a02.okk05
object Test05_Lambda02 {
def main(args: Array[String]): Unit = {
def dualFunctionOneAndTwo(fun: (Int, Int) => Int): Int = {
fun(1, 2)
}
val add = (a: Int, b: Int) => a + b
val minus = (a: Int, b: Int) => a - b
println(dualFunctionOneAndTwo(add))
println(dualFunctionOneAndTwo(minus))
println(dualFunctionOneAndTwo((a: Int, b: Int) => a + b))
println(dualFunctionOneAndTwo((a: Int, b: Int) => a - b))
println(dualFunctionOneAndTwo((a, b) => a + b))
println(dualFunctionOneAndTwo(_ + _))
println(dualFunctionOneAndTwo(_ - _))
println(dualFunctionOneAndTwo((a, b) => b - a))
println(dualFunctionOneAndTwo(-_ + _))
def cal(x: Int, func: (Int, Int) => Int, y: Int) = {
func(x, y)
}
def sum(x: Int, y: Int) = {
x + y
}
println("....")
println(cal(3,
(x: Int, y: Int) => {
x + y
}, 5))
println(cal(3, (x: Int, y: Int) => x + y, 5))
println(cal(3, (x, y) => x + y, 5))
println(cal(3, _ + _, 5))
}
}
函数作为返回值
package com.zishi.scala.a02.okk05
object Test05_Lambda03 {
def main(args: Array[String]): Unit = {
def outer() = {
def inner(): Unit = {
print("abc")
}
inner _
}
val f1 = outer()
f1()
outer()()
println("...............")
def outer2() = {
def inner(name: String): Unit = print(s"abc${name}")
inner _
}
val f2 = outer2()
f2("aege")
def outer3(x: Int) = {
def mid(f : (Int, Int) => Int) = {
def inner(y: Int) = {
f(x, y)
}
inner _
}
mid _
}
println()
val mid = outer3(1)
val inner = mid( _ + _)
val res = inner(3)
println(res)
val res2 = outer3(1)( _ + _)(3)
println(res2)
}
}
高阶函数01
package com.zishi.scala.a02.okk05
object Test06_HighOrderFunction01 {
def main(args: Array[String]): Unit = {
def f(n: Int): Int = {
println("f调用")
n + 1
}
val result: Int = f(123)
println(result)
val f1: Int => Int = f
val f2 = f _
println(f1)
println(f1(12))
println(f2)
println(f2(35))
def fun(): Int = {
println("fun调用")
1
}
val f3: () => Int = fun
val f4 = fun _
println(f3)
println(f4)
}
}
高阶函数02
object Test06_HighOrderFunction02 {
def main(args: Array[String]): Unit = {
def dualEval(op: (Int, Int) => Int, a: Int, b: Int): Int = {
op(a, b)
}
def add(a: Int, b: Int): Int = {
a + b
}
println(dualEval(add, 12, 35))
println(dualEval((a, b) => a + b, 12, 35))
println(dualEval(_ + _, 12, 35))
def f5(): Int => Unit = {
def f6(a: Int): Unit = {
println("f6调用 " + a)
}
f6
}
println(f5()(25))
}
}
object Test06_HighOrderFunction03 {
def main(args: Array[String]): Unit = {
def f5(): Int => Unit = {
def f6(a: Int): Unit = {
println("f6调用 " + a)
}
f6
}
println(f5()(25))
}
}
练习
package com.zishi.scala.a02.okk05
object Test08_Practice {
def main(args: Array[String]): Unit = {
def func(i: Int): String => (Char => Boolean) = {
def f1(s: String): Char => Boolean = {
def f2(c: Char): Boolean = {
if (i == 0 && s == "" && c == '0') false else true
}
f2
}
f1
}
println(func(0)("")('0'))
println(func(0)("")('1'))
println(func(23)("")('0'))
println(func(0)("hello")('0'))
def func1(i: Int): String => (Char => Boolean) = {
s => c => if (i == 0 && s == "" && c == '0') false else true
}
println(func1(0)("")('0'))
println(func1(0)("")('1'))
println(func1(23)("")('0'))
println(func1(0)("hello")('0'))
def func2(i: Int)(s: String)(c: Char): Boolean = {
if (i == 0 && s == "" && c == '0') false else true
}
println(func2(0)("")('0'))
println(func2(0)("")('1'))
println(func2(23)("")('0'))
println(func2(0)("hello")('0'))
}
}
object Test08_Practice01 {
def main(args: Array[String]): Unit = {
val fun = (i: Int, s: String, c: Char) => {
if (i == 0 && s == "" && c == '0') false else true
}
println(fun(0, "", '0'))
println(fun(0, "", '1'))
println(fun(23, "", '0'))
println(fun(0, "hello", '0'))
}
}
package com.zishi.scala.a02.okk05
object Test08_Practice {
def main(args: Array[String]): Unit = {
def func(i: Int): String => (Char => Boolean) = {
def f1(s: String): Char => Boolean = {
def f2(c: Char): Boolean = {
if (i == 0 && s == "" && c == '0') false else true
}
f2
}
f1
}
println(func(0)("")('0'))
println(func(0)("")('1'))
println(func(23)("")('0'))
println(func(0)("hello")('0'))
def func33(i: Int): String => (Char => Boolean) = {
def f1(s: String): Char => Boolean = {
c => true
}
f1
}
def func44(i: Int): String => (Char => Boolean) = {
s => c => if (i == 0 && s == "" && c == '0') false else true
}
def func1(i: Int): String => (Char => Boolean) = {
s => c => if (i == 0 && s == "" && c == '0') false else true
}
println(func1(0)("")('0'))
println(func1(0)("")('1'))
println(func1(23)("")('0'))
println(func1(0)("hello")('0'))
def func2(i: Int)(s: String)(c: Char): Boolean = {
if (i == 0 && s == "" && c == '0') false else true
}
println(func2(0)("")('0'))
println(func2(0)("")('1'))
println(func2(23)("")('0'))
println(func2(0)("hello")('0'))
}
}
package com.zishi.scala.a02.okk05
object Test08_Practice02 {
def main(args: Array[String]): Unit = {
def test(name: String, f: String => Unit): Unit = {
f(name)
}
def func(name: String): Unit = {
println("name:" + name)
}
test("zbc", func)
test("zbc", (name: String) => println("name:" + name))
test("zbc", name => println("name:" + name))
test("zbc", _ => println("name:" + _))
def test2(f: String => Unit): Unit = {
f("测试变形")
}
def func2(name: String): Unit = {
println("func2:" + name)
}
test2((name: String) => println("func2:" + name))
test2(name => println("func2:" + name))
test2(name => println(name))
test2(println(_))
test2(println _)
test2(println)
}
}