CategoriesQuicksearchLogin |
Delimited Continuations vs. For-ComprehensionsSunday, May 1. 2011Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
I'm actually not convinced that I do give up type safety with the way I did it. The 'reset' method I have returns the correct type, not a Future[Any]. My first implementation used the '@suspendable' annotation which is the same as @cps[Unit], and I wasn't returning 'Unit'. I only had to change it to get exceptions returned properly. That being said, making an implementation for a Future is different then something like this so it may not work the same. It might be worth trying it with Generator[Any] and seeing if it does break type safety. You might have to make Generator covariant for that to work. Good information in there though, I'll have to play around with your code when I get a chance. I tried, but simply changing it to Generator[Any] did not work and I didn't invest much time in it. What was the exact problem with @suspendable? Making the cps-annotations simpler looks desirable This works for me: https://gist.github.com/951681 Nevermind, checked to make sure the right type was infered and it wasn't. Not sure why it works properly in Akka. Maybe I need to test it more, perhaps it isn't doing what I think it is. Checked again. I did all my testing of type safety within the reset, not with the result. I ignored the value of the reset as it wasn't part of the original spec, just a side effect of the way I implemented it. Values were originally supposed to be returned by assigning values to futures, which is type safe. So it's not really applicable to what you are trying to do here. Fixed now in Akka. I knew I had compile time type safety with this before, I accidently removed it during a refactoring. Added a test for it now too. If it wasn't for this blog post I wouldn't have tested for it, thanks! The thing is: A generator like you have created here with CPS is much easier created without any CPS at all: https://gist.github.com/951254 So the question is what the value of using CPS is here. The only line where you are using CPS is here: Generator( A(genIntA, genBoolA) ) CPS is essentially making the control flow explicit so what you could do behind the scenes here is changing the order in which genInt/genBool are called to produce an A, but why would you want to make use of that here? Yes, you are right. It seems I was too much caught up in that flatMap scheme to see the easier alternative (and that one can be composed, too). At least it made me research and understand how the continuations plugin can be used, now I can confidently ambush any opportunity for using it Seriously, though, CPS would for example allow me to keep track of the generators used within one expression. What I also have in my real use case (though based solely on flatMap) is to calculate an estimate of the size of the value set in order to be able to adapt the number of samples taken. |
Calendar
|