New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement breakable generators for sequence comprehensions #9120
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9120?orig=1 |
@erikerlandson said: |
@Ichoran said: For instance, your for-loop could be more easily expressed by val r = (1 to 1000).iterator.
map{ j => println(s"iteration j = $j") }.
filterNot(j => (j % 2) == 0).
takeWhile(_ <= 5) If you are making instead an argument about efficiency, it would help to show benchmarks. It is far from clear that your approach yields a nice tradeoff between speed and power (i.e. as much of both as possible). |
@erikerlandson said: Another way of looking at it: motivation is more or less the same as the already-existing The times I, personally, find myself wanting |
@Ichoran said: Also, can you give an example which is not more compactly expressed with I think it is valuable to have multiple ways to do things as long as some of them are clearly superior in some cases. But otherwise it seems to be a net negative (Perl vs. Python for instance) to have many different approximately equivalent ways to do the same thing: it makes reading code harder since the code-to-intent map is more complex. FWIW, the normal breakable/break provides a way to exit closures and such that has less boilerplate and is faster than the stackless exception-based return that the compiler will give you. A similarly compelling story here (whether for speed or clarity or both or something else) would be nice to have. Also, any advantage has to be weighed against the potential for accidental consumption of the |
@Ichoran said: Except it's a little more fiddly because anything where |
@erikerlandson said: |
@Ichoran said: Upsides:
Downsides:
It may be just the thing for some users, which argues for it being part of some library, but not necessarily the standard library. |
@erikerlandson said: |
@erikerlandson said: |
I think this is now out of scope for Scala 2, but Scala 3's |
@SethTisue - I don't think boundary/break entirely works because you can't get partial computations that way (not with ordinary syntax). The features he wanted could be built with boundary/break, but they alone aren't enough. @erikerlandson - My kse3 library implements functionality like this for arrays using boundary/break. It doesn't solve the break-out-of-multiple-levels problem (by design), but operations that normally accumulate things can return partial results with skipping and early termination. If you wanted to do it for the Scala collections, it might be instructive to see how I did it for arrays. |
Proposing a light-weight implementation of 'break' and 'continue' that operate inside of sequence comprehensions, as specialized breakable generators.
The concept is written up here:
http://erikerlandson.github.io/blog/2015/01/24/monadic-break-and-continue-for-scala-sequence-comprehensions/
A simple example of what they look like in use:
The text was updated successfully, but these errors were encountered: