tag:blogger.com,1999:blog-6818189468244276547.post3406161341581946302..comments2023-10-10T15:54:46.212+01:00Comments on Quoi qu'il en soit: Scala Streams for iteration / Use streams to avoid TCO(?)TimAhttp://www.blogger.com/profile/03169884264169945941noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-6818189468244276547.post-31649493205241195932009-10-18T18:56:37.470+01:002009-10-18T18:56:37.470+01:00You'll notice that lazy_:: (known as #:: in Sc...You'll notice that lazy_:: (known as #:: in Scala 2.8) isn't defined on Stream[A]. The call on the right side, which returns a Stream[A] is converted to StreamCons[T] by Predef.lazyStreamToConsable, which takes the stream as a by name parameter. <br /><br />What's the takeaway? We know we can define short circuit evaluation for ourselves by <br /><br />class Foo{<br /> def toBool:Boolean = //...<br /> def and (rhs: => Foo) = if (toBool) rhs.toBool else false<br />}<br /><br /><br />and rhs will not be evaluated. But what if we want to avoid evaluating the left side of the operator? In that case, we can create an implicit conversion with a by name parameter to convert to some other class that defines the appropriate operator.<br /><br />Then all that's left is to reverse the sides when the operator in question ends with a colon.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6818189468244276547.post-4368252728415179982009-10-16T19:18:53.854+01:002009-10-16T19:18:53.854+01:00I think that reason that the streams do not eat st...I think that reason that the streams do not eat stack is because the remainder of the stream is constructed lazily. It doesn't actually call the function recursively, it just looks like it does. It actually captures a function that will generate the next segment of the stream when it is accessed for the first time.<br /><br />Either way, streams are looking pretty useful, thanks for the post!Danhttps://www.blogger.com/profile/02192772456217563238noreply@blogger.comtag:blogger.com,1999:blog-6818189468244276547.post-55452316208222534032009-10-15T19:40:22.793+01:002009-10-15T19:40:22.793+01:00@Johan: Done@Johan: DoneTimAhttps://www.blogger.com/profile/03169884264169945941noreply@blogger.comtag:blogger.com,1999:blog-6818189468244276547.post-85142812206849930322009-10-14T22:31:16.062+01:002009-10-14T22:31:16.062+01:00I'd just like to point out that the last funct...I'd just like to point out that the last function, the definition of <i>until</i>, is not correct. Either rename it to iterateFor or swap the cases in the if-statement.Johanhttps://www.blogger.com/profile/02361156710674732570noreply@blogger.com