[Haskell-beginners] lazy database queries

Brent Yorgey byorgey at seas.upenn.edu
Fri Jul 30 02:13:24 EDT 2010


On Thu, Jul 29, 2010 at 04:24:57PM -0600, Michael Hendricks wrote:
> Thanks Gabríel.  That's exactly what I was looking for.  I had avoided
> the docs for System.IO.Unsafe since "unsafe" scared me away :-)

That's exactly what the "unsafe" is there for! =)

The reason this particular idea is unsafe (one reason, at least,
others might know of more) is that the database might change (or
disapppear, or...) in the middle of lazily reading from it, so your
program gets an inconsistent view of the world.  I'm not trying to
discourage you, just making sure you understand the risks.  If you are
sure the database won't change (or you don't care if it does) then
this should work great.

-Brent

> 
> I've cc'd the list so that others can find your excellent answer in
> the archives later.
> 
> -- 
> Michael
> 
> On Thu, Jul 29, 2010 at 03:32:25PM +0000, "Gabríel A. Pétursson" wrote:
> >  You might be interested in unsafeInterleaveIO in the module
> > System.IO.Unsafe
> > <http://users.skynet.be/jyp/html/base/System-IO-Unsafe.html>.
> > 
> > On 29.07.2010 14:04, Michael Hendricks wrote:
> > >I have a data structure roughly like
> > >
> > >     data Prices = Prices {
> > >         today    :: [Price],
> > >         thisYear :: [Price]
> > >     }
> > >
> > >Both today and thisYear are initially populated by database queries
> > >using HDBC.  I then have functions which call today and possibly call
> > >thisYear.  thisYear is not called often and the query to retrieve that
> > >data is very expensive.
> > >
> > >I thought I could use HDBC's laziness to postpone actually running the
> > >slow query until thisYear was required.  Attempts with quickQuery
> > >suggest that the query is executed immediately and the only laziness
> > >is with fetching the results.
> > >
> > >Is it possible to make thisYear a lazy list which only executes the
> > >query if thisYear's values are required?
> > >
> > >Thank you.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list