You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LinkedListLike's length implementation (with which size happens to be overridden) is recursive, but not tail-recursive. Naturally, this means a call to length can result in Stack Overflow.
In a brief research into length and size implementations I couldn't find any other suffering from this malady. So I propose the following implementation for it:
overridedeflength:Int= {
varlen=0varleft=thiswhile (!left.isEmpty) {
len +=1
left = left.next
}
len
}
Which happens to be an almost verbattim copy of... I think Stream, replacing tail with next. LinearSeqOptimized also has a very similar definition, but using these instead of left as the variable name, so I'm not sure why Stream is overridding it at all, except, perhaps, as an optimization opportunity missed.
Since tail is present all the way back in Seq, I don't see why the above implementation (but using tail) isn't made default at SeqLike, and let collections which can optimize it override that.
At any rate, LinkedListLike's implementation ought to be fixed.
The text was updated successfully, but these errors were encountered:
@paulp said:
It's worse than that. Here is TraversableOnce:
defsize:Int= {
varresult=0for (x <- self) result +=1
result
}
Why is this not sufficient. Does someone have evidence that some other way of writing this is faster if you have to traverse the collection regardless? Because otherwise we could just stop overriding it without a clear reason.
@dcsobral said:
Well, I did see that at TraversableOnce, but it uses a closure, which the implementation using while and tail avoids. I guess both come pretty close with specialization and JVM optimizations, but...
retronym
changed the title
length method redundant and missing implementations, and {{LinkedList}}'s non-tail recursive's length
length method redundant and missing implementations, and LinkedList's non-tail recursive's length
Apr 24, 2017
retronym
changed the title
length method redundant and missing implementations, and LinkedList's non-tail recursive's length
length method redundant and missing implementations, and LinkedList's non-tail recursive's length
Apr 24, 2017
Uh oh!
There was an error while loading. Please reload this page.
LinkedListLike
'slength
implementation (with whichsize
happens to be overridden) is recursive, but not tail-recursive. Naturally, this means a call tolength
can result in Stack Overflow.In a brief research into
length
andsize
implementations I couldn't find any other suffering from this malady. So I propose the following implementation for it:Which happens to be an almost verbattim copy of... I think
Stream
, replacingtail
withnext
.LinearSeqOptimized
also has a very similar definition, but usingthese
instead ofleft
as the variable name, so I'm not sure whyStream
is overridding it at all, except, perhaps, as an optimization opportunity missed.Since
tail
is present all the way back inSeq
, I don't see why the above implementation (but usingtail
) isn't made default atSeqLike
, and let collections which can optimize it override that.At any rate,
LinkedListLike
's implementation ought to be fixed.The text was updated successfully, but these errors were encountered: