我有一个功能,看起来像这样:斯卡拉 - 构建功能N次
def emulate: (Cpu => Cpu) => (Cpu => Cpu) = render => {
handleOpcode andThen
handleTimers andThen
handleInput andThen
debug andThen
render
}
我要打电话的次数handleOpcode功能n个(比如说10次)。在Haskell我可以写一个函数,像这样:
ntimes n f = foldr (.) id (replicate n f)
但在Scala中,我不知道我怎么会写。我试过了:
def nTimes(n: Int, f: => Any) = {
val l = List.fill(n)(f)
l.foldRight(identity[Function]){ (x, y) => y.andThen(x) }
}
但是类型都是错的。
有没有简单的方法来实现这一点?理想情况下,无需创建自己的功能。也许在斯卡拉斯有一些东西?
那么'f'不是那里的函数,它只是一个非严格的'Any',所以类型都是错的并不奇怪。为什么你不像Haskell那样开始写类型? –