Scala: Exposing a JDBC ResultSet through a generator (iterable) -


i've got set of rows in database, , i'd provide interface spin through them this:

def findall: iterable[myobject] 

where don't require having instances in memory @ once. in c# can create generators using yield, compiler takes care of converting code loops through recordset iterator (sort of inverting it).

my current code looks this:

def findall: list[myobject] = {   val rs = getrs   val values = new listbuffer[myobject]   while ( rs.next() )      values += new valuefromresultset(rs)   values.tolist } 

is there way convert not store entire set in memory? perhaps use comprehension?

try extending iterator instead. haven't tested it, this:

def findall: iterator[myobject] = new iterator[myobject] {   val rs = getrs   override def hasnext = rs.hasnext   override def next = new valuefromresultset(rs.next) } 

this should store rs when it's called, , otherwise light wrapper calls rs.

if want save values traverse, check out stream.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -