I have a function that looks like this:
def emulate: (Cpu => Cpu) => (Cpu => Cpu) = render => {
handleOpcode andThen
handleTimers andThen
handleInput andThen
debug andThen
render
}
I want to call the handleOpcode function n number of times (say 10 times). In Haskell I might write a function like so:
ntimes n f = foldr (.) id (replicate n f)
But in Scala, I'm not sure how I might write it. I tried:
def nTimes(n: Int, f: => Any) = {
val l = List.fill(n)(f)
l.foldRight(identity[Function]){ (x, y) => y.andThen(x) }
}
but the types are all wrong.
Is there a simple way to achieve this? Ideally without having to create my own function. Something in Scalaz perhaps?