« Return to Thread: Tail recursive version of foldRight for List?

Tail recursive version of foldRight for List?

by Sébastien Bocq :: Rate this Message:

Reply to Author | View in Thread

Hi,

Wouldn't this version be safer?

override def foldRight[B](z: B)(f: (A, B) => B): B = this.reverse.foldLeft(z)((b, a) => f(a, b)))

(same idea for reduceRight)

The argument is the same as for trampolining: it may be less efficient but at least it will never blow up the stack.

Thanks,
Sebastien

 « Return to Thread: Tail recursive version of foldRight for List?