[plt-scheme] Sequencing with a resource that needs to be released

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Tue Dec 16 17:25:53 EST 2008

One thing that might make sense is to use a will executor, however,
you'll want to have a more efficient case when it is possible to
detect.

http://docs.plt-scheme.org/reference/willexecutor.html

Jay

On Tue, Dec 16, 2008 at 1:21 PM, Christopher Bowron <cwbowron at gmail.com> wrote:
> I recently figured out how the sequence generators can be helpful in my
> program and I was putting together a function to generate all the rows of
> the database in my app, but I realized I have a problem.
>
> I am using Jay McCarthy's SQLite module and using prepare, load-params, step
> and finalize to generate all the matching rows.  I see that I could use the
> exec function, but I was interested in using sequences for stylistic reasons
> and to learn sequences better.
>
> My problem is how to call finalize correctly.  In the ideal case, where the
> entire sequence is iterated on, I can clean up in the functions the
> determine whether or not to continue the sequence, but it seems that if the
> sequence is short circuited as in a for/and or for/or then my statement
> cannot be finalized and the database will stay locked.
>
> From reading the docs I couldn't think of a way to ensure that I was
> releasing the resources correctly, but I was wondering if anyone had an
> idea.
>
> Here is the code that I wrote:
>
> (define (in-database-file-indexes (where #f) . rest)
>   (let ([stmt (prepare
>                (database)
>                (if where
>                    (format "SELECT file_index FROM media WHERE ~A" where)
>                    "SELECT file_index FROM media"))])
>     (make-do-sequence
>      (lambda ()
>        (values
>         (lambda (p)
>           (and-let* ([r (step p)])
>             (vector-ref r 0)))
>         (lambda (p) p)
>         (begin
>           (when (and where
>                      (not (null? rest)))
>             (apply load-params stmt rest))
>           stmt)
>         (lambda (p) #t)
>         (lambda (v)
>           (if v
>               #t
>               (begin
>                 (finalize stmt)
>                 #f)))
>         (lambda (p v) #t))))))
>
> --
> Christopher W. Bowron <chris at bowron.us>
> [ Nothing is exciting if you know what the outcome will be ]
>    - Joseph Campbell
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://jay.teammccarthy.org

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.