函数编程中,接受多个参数的函数都可以转化为接受单个参数的函数,这个转化过程就叫柯里化。柯里化就是证明了函数只需要一个参数而已。
//常规
def mul(x: Int, y: Int) = x * y
println(mul(10, 10))
//闭包
def mulCurry(x: Int) = (y: Int) => x * y
println(mulCurry(10)(9))
//函数柯里化
def mulCurry2(x: Int)(y:Int) = x * y
println(mulCurry2(10)(8))
//这是一个函数,可以接收两个字符串,比较是否相等
def eq(s1: String, s2: String): Boolean = {
s1.equals(s2)
}
//隐式类
implicit class TestEq(s: String) {
//体现了将比较字符串的事情,分解成两个任务完成
//1. checkEq 完转换大小写
//2. f函数完成比较任务
def checkEq(ss: String)(f: (String, String) => Boolean): Boolean = {
f(s.toLowerCase, ss.toLowerCase)
}
}
val str1 = "hello"
println(str1.checkEq("HeLLO")(eq))
//在看一个简写形式
println(str1.checkEq("HeLLO")(_.equals(_)))
————Blueicex 2020/2/17 10:55 blueice1980@126.com