- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[Fwd: Re: continue keyword]
Detering Dirk wrote:
> I would be really, really interested in seeing a simplified
> (but not oversimplified!) fully functional prototype version
> of this usecase written in your style, and than let us
> (well, not me, but the experts ;) ) show how that would be
> solved in The Scala Way.
>
> It seems a sufficiently complex, but not too complex,
> properly isolated problem for a showcase, and even coming
> out of real world necessities.
>
A hearty +1 to that!
I'm someone from the Java / traditional procedural OO world who has been
watching Scala and other functional hybrids with interest (though as yet
without time to devote to really getting my feet wet). Though by all rights
my bias should be for adding the continue and break (I've written plenty of
file parsing loops, and have used these constructs now and then), from what I've seen,
I find the alternative compelling, and actually find
data.takeWhile(_.time < endTime).filter(_.time > startTime).map(line
=> println("Found "+line.recordType+" at "+line.time))
quite comprehensible even though my grasp of Scala syntax is shaky at best.
As someone still deciding whether and to what degree to take the plunge into Scala
or FP in general, it would be EXTREMELY useful to see the non-simplified version of
this in several forms:
* The procedural without continue and break
* The procedural with continue and break (even if hypothetical)
* However many FP ways of doing it the various FP proponents think are better,
perhaps including one or more FP version using continue and break
* Perhaps a version with a bit of DSL work to soup up the readability?
It doesn't even matter if there is remotely a consensus afterwards as to which
is more readable, it would still be valuable.
Eric










Re: [Fwd: Re: continue keyword]
And yeah, the for-comprehension is just a sequence of calls to filter, map, and flatMap, which preserve the laziness of ManagedSequence.
And yes, your revised version works.
--j
On Fri, Mar 20, 2009 at 3:08 PM, Brett Knights <brett [at] knightsofthenet [dot] com> wrote:
Re: [Fwd: Re: continue keyword]
var go = true
for (line <- infile.toFile.lines if go) {
...
}
--j
--j
On Fri, Mar 20, 2009 at 3:19 PM, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote:
Re: [Fwd: Re: continue keyword]
Jorge Ortiz wrote:
> On further reflection, another way to write it (without takeWhile)
> would be:
>
> var go = true
> for (line <- infile.toFile.lines if go) {
> ...
> }
>
according to the article I cited that would produce the correct output
but would end up reading all the lines (and I assume discarding them --
no OOM at least) because the guard doesn't stop the generator so once go
has been set to true all the rest of the lines in the file will just be
read and skipped.
This has certainly been an educational thread.
Re: [Fwd: Re: continue keyword]
--j
On Fri, Mar 20, 2009 at 3:36 PM, Brett Knights <brett [at] knightsofthenet [dot] com> wrote:
Re: [Fwd: Re: continue keyword]
For comprehensions for streams should yield streams (it is just
transformed into filters and flatMaps) and therefore takeWhile should
cause early termination even in the original code.
On 3/20/09, Brett Knights wrote:
>
> >
> Doesn't this one still read all the lines?
>
>
Re: [Fwd: Re: continue keyword]
2009/3/19 Russ Paielli <russ [dot] paielli [at] gmail [dot] com>
--
Thanks,
-Vlad
Re: [Fwd: Re: continue keyword]
--j
On Thu, Mar 19, 2009 at 1:03 PM, Jorge Ortiz <jorge [dot] ortiz [at] gmail [dot] com> wrote: