Scala:函数组合中的类型不匹配,发现(Int,Int)=> Seq [Int]需要? => 序列 [Int]
Scala: type mismatch in function composition, found (Int, Int) => Seq[Int] require ? => Seq[Int]
我正在尝试在脚本中编写 2 个函数,但遇到了我无法解决的类型不匹配问题。
这是示例代码:
def generate(start: Int, end: Int): Seq[Int] = (start until end).toSeq
def restrain(seq: Seq[Int]) = seq.dropWhile(_ < 20).takeWhile(_ < 60)
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
通过将其加载到 REPL 中:
:load test.sc
我收到以下错误:
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
^
test.sc:1: error: type mismatch;
found : (Int, Int) => Seq[Int]
required: ? => Seq[Int]
我做错了什么?
类型Function2[Int, Int, Seq[Int]]
和Function1[(Int, Int), Seq[Int]]
不一样。 (generate _)
产生前者,而对于此组合,您需要后者。尝试:
restrain _ compose (generate _).tupled
我正在尝试在脚本中编写 2 个函数,但遇到了我无法解决的类型不匹配问题。 这是示例代码:
def generate(start: Int, end: Int): Seq[Int] = (start until end).toSeq
def restrain(seq: Seq[Int]) = seq.dropWhile(_ < 20).takeWhile(_ < 60)
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
通过将其加载到 REPL 中:
:load test.sc
我收到以下错误:
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
^
test.sc:1: error: type mismatch;
found : (Int, Int) => Seq[Int]
required: ? => Seq[Int]
我做错了什么?
类型Function2[Int, Int, Seq[Int]]
和Function1[(Int, Int), Seq[Int]]
不一样。 (generate _)
产生前者,而对于此组合,您需要后者。尝试:
restrain _ compose (generate _).tupled