|
Scala Programming Language
»
Scala - Debate
Tail recursive version of foldRight for List?
View:
Threaded
Chronologically
All Messages
New views
1 Messages —
Rating Filter:
0
1
2
3
4
5
Alert me
Tail recursive version of foldRight for List?
by Sébastien Bocq
:: Rate this Message:
Reply to Author
|
View Threaded
|
Show Only this Message
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
Free embeddable forum
powered by
Nabble
Forum Help