scala 函数采用 returns 另一个函数的函数
scala function takes function which returns another function
在查看教程时我看到了一段代码,为了理解它,尝试编写一个示例函数并调用它。
因为我是 scala 的新手,不知道从哪里开始。
val flow3: Flow[Int, Int, NotUsed] = Flow[Int].statefulMapConcat {
() =>
var sequence = 0
println("statefulMapConcat func call")
i =>
sequence = sequence + i
List(sequence)
}
以上两点对我来说很奇怪
- () => 为什么没有参数
- () => and i => // 都是参数,这是什么风格。这是调用实际功能。以及我们如何编写一个函数来调用这个
定义是:
def statefulMapConcat[T](f: () ⇒ Out ⇒ immutable.Iterable[T]): Repr[T] =
via(new StatefulMapConcat(f))
我的尝试!!
def suffix(x: String): String = x + ".zip"
// not sure this is true
def test1(f: String ⇒ String ⇒ String): String = f("a1")("a2") + "a3"
// not sure this is also a true definition
def test2(f: String ⇒ String ⇒ String): String = f + "a4"
// compile is okay but can not call this
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
// giving even compile time error
test(suffix(suffix("")))
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
在这个例子中你不能调用 mytest 因为 "mytest" 不是函数而是 "test1" 函数求值的结果。 "mytest" 是一个字符串值。所以你可以用下面的代码替换这段代码,这仍然会编译:
var mytest: String= test1{
a => a + "a5"
b => b + "a6"
}
你的第二个例子还有一个问题。您正在调用 "test" 函数传递评估的结果 "suffix(suffix(""))" 这是字符串。
要使其编译,您需要创建返回函数的函数,然后将其传递给 "test"(1 或 2)
def functionReturiningFunciton(s:String):String=>String = (k) => suffix(suffix(""))
val f: String => String => String = functionReturiningFunciton // convert to value
test1(f)
test2(f)
或者你甚至可以直接传递"functionReturiningFunciton",因为它会自动转换为val
def functionReturiningFunciton(s:String):String=>String = (k) => suffix(suffix(""))
test1(functionReturiningFunciton)
test2(functionReturiningFunciton)
甚至像这样
test1(s => k => suffix(suffix("")))
注意。当您这样做时:
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
如果你去糖你的代码。你实际上是这样做的:
def someFunction(a:String):String=>String = {
a + "a5" // this is evaluated but ignored
b => b + "a6" //this is what returned by someFunction
}
var mytest:String= test1{someFunction}
definition is :
def statefulMapConcat[T](f: () ⇒ Out ⇒ immutable.Iterable[T]): Repr[T] =
via(new StatefulMapConcat(f))
函数 statefulMapConcat 将另一个参数为零的函数作为参数,returns 另一个函数将参数类型为 "Out" 和 returns Iterable[T]
在查看教程时我看到了一段代码,为了理解它,尝试编写一个示例函数并调用它。
因为我是 scala 的新手,不知道从哪里开始。
val flow3: Flow[Int, Int, NotUsed] = Flow[Int].statefulMapConcat {
() =>
var sequence = 0
println("statefulMapConcat func call")
i =>
sequence = sequence + i
List(sequence)
}
以上两点对我来说很奇怪
- () => 为什么没有参数
- () => and i => // 都是参数,这是什么风格。这是调用实际功能。以及我们如何编写一个函数来调用这个
定义是:
def statefulMapConcat[T](f: () ⇒ Out ⇒ immutable.Iterable[T]): Repr[T] =
via(new StatefulMapConcat(f))
我的尝试!!
def suffix(x: String): String = x + ".zip"
// not sure this is true
def test1(f: String ⇒ String ⇒ String): String = f("a1")("a2") + "a3"
// not sure this is also a true definition
def test2(f: String ⇒ String ⇒ String): String = f + "a4"
// compile is okay but can not call this
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
// giving even compile time error
test(suffix(suffix("")))
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
在这个例子中你不能调用 mytest 因为 "mytest" 不是函数而是 "test1" 函数求值的结果。 "mytest" 是一个字符串值。所以你可以用下面的代码替换这段代码,这仍然会编译:
var mytest: String= test1{
a => a + "a5"
b => b + "a6"
}
你的第二个例子还有一个问题。您正在调用 "test" 函数传递评估的结果 "suffix(suffix(""))" 这是字符串。
要使其编译,您需要创建返回函数的函数,然后将其传递给 "test"(1 或 2)
def functionReturiningFunciton(s:String):String=>String = (k) => suffix(suffix(""))
val f: String => String => String = functionReturiningFunciton // convert to value
test1(f)
test2(f)
或者你甚至可以直接传递"functionReturiningFunciton",因为它会自动转换为val
def functionReturiningFunciton(s:String):String=>String = (k) => suffix(suffix(""))
test1(functionReturiningFunciton)
test2(functionReturiningFunciton)
甚至像这样
test1(s => k => suffix(suffix("")))
注意。当您这样做时:
var mytest= test1{
a => a + "a5"
b => b + "a6"
}
如果你去糖你的代码。你实际上是这样做的:
def someFunction(a:String):String=>String = {
a + "a5" // this is evaluated but ignored
b => b + "a6" //this is what returned by someFunction
}
var mytest:String= test1{someFunction}
definition is :
def statefulMapConcat[T](f: () ⇒ Out ⇒ immutable.Iterable[T]): Repr[T] = via(new StatefulMapConcat(f))
函数 statefulMapConcat 将另一个参数为零的函数作为参数,returns 另一个函数将参数类型为 "Out" 和 returns Iterable[T]