I have a DateTime and TimeSpan class in Scala (assume that the < and + operators work as they should). I'm trying to define a 'range' function that takes a start/stop time and a timespan for stepping. In C# I would do this with a yield, and I think I should be able to do the same in Scala... except I'm getting a strange error.
On the 'yield t' line, I get "Illegal start of statement".
def dateRange(from : DateTime, to : DateTime, step : TimeSpan) =
{
// not sure what the list'y way of doing this is
var t = from
while(t < to)
{
yield t; // error: illegal start of statement
t = t + step
}
}
Looking at this code, I am curious about 2 things: 1) what did I do wrong? 2) the code as written is very imperative (uses a var t, etc). What's the more functional way to do this in Scala that is reasonably fast?
Thanks!
yield
in Scala has nothing whatsoever to do withyield
in C# (or Python). Furthermore, Scala has no equivalent to it -- look up the many questions about Scala, Python, yield and generators. And, of course, look up the questions about whatyield
actually does. – Several