Stack Over Flow:Làm thế nào để xác định và gọi một hàm được áp dụng một phần với varargs?
Xin chào tất cả.
Tôi muốn xác định hàm được áp dụng một phần có các biến thể.
Để xác nhận, tôi đã chuẩn bị một số chức năng tức là Chức năng._. Các hàm ngoại trừ func có đối số varargs.
Cách xác định và gọi hàm được áp dụng một phần bằng varargs? (ví dụ gunc1 với varargs.)
java phiên bản "1.7.0_07" Scala phiên bản mã Á hậu 2.9.1.final
object Functions {
def func(x: Int, y: Int) = x + y
def gunc(x: Int*) = x.sum
def hunc(x: Int, y: Int*) = x + y.sum
def iunc(x: Int)(y: Int*) = x + y.sum // curried
def junk(x: String, y: Int*) = x + y.sum
}
object PartiallyApplied extends App {
import Functions._
val func0 = func(1, _: Int) // I can.
println("result: " + func0(2))
val gunc0 = gunc(1, _: Int) // I can invoke if I specify the varargs.size. But gunc0 no longer has varargs...
println("result: " + gunc0(2))
// val gunc1 = gunc(1, _: Int*) => compile error: ')' expected but identifier found.
// val gunc1 = gunc(1, _: Seq[Int]) => compile error: type mismatch | found: Seq[Int] | required: Int
val gunc1 = gunc(1, _: Int) // I can invoke if I specify the varargs.size. But gunc1 no longer has varargs...
println("result: " + gunc1(2))
// val hunc0 = hunc(1)_ => compile error: _ must follow method; cannot follow Int
// val hunc0 = hunc(1, _: Int*) => compile error: ')' expected but identifier found.
// val hunc0 = hunc(1, _: Seq[Int]) => compile error: type mismatch | found: Seq[Int] | required: Int
val hunc0 = hunc(1, _: Int) // I can invoke if I specify the varargs.size. But hunc0 no longer has varargs...
println("result: " + hunc0(2))
// println("result: " + hunc0(2, 3)) => compile error: too many arguments for method apply: (v1: Int)Int in trait Function1
// println("result: " + hunc0((2, 3): _*)) => compile error: type mismatch | found: (Int, Int) | required: Seq[Int]
// println("result: " + hunc0(Seq(2, 3))) => compile error: type mismatch | found: Seq[Int] | required: Int
// println("result: " + hunc0(Seq(2, 3): _*)) => compile error: no `: _*' annotation allowed here (such annotations are only allowed in arguments to *-parameters)
val hunc1 = hunc(1, _: Int, _: Int) // I can invoke if I specify the varargs.size. But hunc1 no longer has varargs...
println("result: " + hunc1(2, 3))
val hunc2 = hunc(1, _: Int, _: Int, _:Int) // I can invoke if I specify the varargs.size. But hunc2 no longer has varargs...
val hunc3 = hunc2(2, _: Int, _: Int)
val hunc4 = hunc3(3, _: Int)
println("result: " + hunc4(4))
println("result: " + hunc5(2, 3))
// val iunc0 = iunc(1)(_: Int*) => compile error: ')' expected but identifier found.
// val iunc0 = iunc(1)(_: Seq[Int]) => compile error: type mismatch | found: Seq[Int] | required: Int
val iunc0 = iunc(1)(_: Int)
println("result: " + iunc0(2))
val iunc1 = iunc(1)(_: Int, _: Int)
println("result: " + iunc1(2, 3))
}
object NotPartiallyApplied extends App {
import Functions._
println("result: " + gunc(1))
println("result: " + gunc(1, 2, 3))
println("result: " + gunc(Seq(1, 2, 3): _*))
println("result: " + hunc(1))
println("result: " + hunc(1, 2, 3))
println("result: " + hunc(1, Seq(2, 3): _*))
println("result: " + iunc(1)(2, 3))
println("result: " + iunc(1)(Seq(2, 3): _*))
println("result: " + junk("x"))
println("result: " + junk("x", 2, 3))
println("result: " + junk("x", Seq(2, 3): _*))
}
[Chỉnh sửa sau khi bình luận Rex Kerr]
Tôi muốn hàm được áp dụng một phần như guncN bên dưới:
val guncN = gunc(1, _: Int*)
println("result: " + guncN(2)) // => 3
println("result: " + guncN(2, 3)) // => 6
println("result: " + guncN(2, 3, 4, 5, 100)) // => 115
Nhưng bị cấm bởi scalac.
Tôi nghĩ rằng guncN nên được đánh giá mỗi lần, tại thời điểm (2), (2, 3) và (2, 3, 4, 5, 100) được đưa ra. Chúng ta không cần guncM dưới đây:
val guncM = guncN(2, _: Int*)
println("result: " + guncM(3, 4)) // => 10
Cảm ơn bạn đã trả lời. Nhưng tôi không thể biên dịch bạn cpde ... Phiên bản nào là Scala? 'Scala> res0 andThen res1' ': 11: lỗi: loại không phù hợp; ' 'tìm thấy: Int * => Int' ' yêu cầu: Seq [Int] =>' 'res0 andThen res1' –