我有一个贷款模式,应用函数n次,其中'i'是递增变量."偶尔",我希望传入的函数可以访问'i'....但我不想要求传入的所有函数都要求定义一个param来接受'i'.以下示例......
def withLoaner = (n:Int) => (op:(Int) => String) => {
val result = for(i <- 1 to n) yield op(i)
result.mkString("\n")
}
def bob = (x:Int) => "bob" // don't need access to i. is there a way use () => "bob" instead?
def nums = (x:Int) => x.toString // needs access to i, define i as an input param
println(withLoaner(3)(bob))
println(withLoaner(3)(nums))
Run Code Online (Sandbox Code Playgroud) scala ×1