我想运行DriverTest,但Process.count的类型[I]更改为[Nothing] ... 这段代码有什么问题?
错误信息是这样的: “found:Process [Nothing,Int],required:Process [String,Int]”
sealed trait Process[I, O]
case class Halt[I, O]() extends Process[I, O]
case class Await[I, O](program: Option[I] => Process[I, O]) extends Process[I, O]
case class Emit[I, O](output: O, process: Process[I, O] = Halt[I, O]) extends Process[I, O]
object Driver {
@annotation.tailrec
def driver[I, O](process: Process[I, O], is: Stream[I], os: Stream[O] = Stream()): Stream[O] = process match {
case Halt() => os.reverse //State: Halt -> stop driver
case Await(program) => is match {
case h #:: t => driver(program(Some(h)), t, os) //State: Await + Data: remain -> loop driver
case empty => driver(program(None), empty, os) //State: Await + Data: empty -> go to Halt
}
case Emit(o, process) => driver(process, is, o #:: os) //State: Emit -> save data + loop driver
}
}
object Process {
def loop[I, O, S](s1: S)(f: (I, S) => (O, S)): Process[I, O] = Await[I, O] {
case Some(i) => {
val (o, s2) = f(i, s1)
Emit(o, loop(s2)(f))
}
case None => Halt()
}
def count[I]: Process[I, Int] = loop(0)((i: I, s: Int) => (s + 1, s + 1))
}
object DriverTest extends App {
val input = Stream("a","b","c")
val proc = Process.count
val output = Driver.driver(proc, input)
}
答案 0 :(得分:3)
下一行假设有帮助。
val proc = Process.count[String]