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.<br><br>I am using Jay McCarthy&#39;s SQLite module and using prepare, load-params, step and finalize to generate all the matching rows.&nbsp; I see that I could use the exec function, but I was interested in using sequences for stylistic reasons and to learn sequences better.<br>
<br>My problem is how to call finalize correctly.&nbsp; 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.&nbsp; <br>
<br>From reading the docs I couldn&#39;t think of a way to ensure that I was releasing the resources correctly, but I was wondering if anyone had an idea.<br><br>Here is the code that I wrote:<br><br>(define (in-database-file-indexes (where #f) . rest)<br>
&nbsp; (let ([stmt (prepare <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (database) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (format &quot;SELECT file_index FROM media WHERE ~A&quot; where)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;SELECT file_index FROM media&quot;))])<br>
&nbsp;&nbsp;&nbsp; (make-do-sequence<br>&nbsp;&nbsp;&nbsp;&nbsp; (lambda ()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (values<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lambda (p) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (and-let* ([r (step p)])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (vector-ref r 0)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lambda (p) p)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when (and where<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (not (null? rest)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (apply load-params stmt rest))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stmt)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lambda (p) #t)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lambda (v) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if v <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #t<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (begin<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (finalize stmt)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #f)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (lambda (p v) #t))))))<br><br>-- <br>Christopher W. Bowron &lt;<a href="mailto:chris@bowron.us">chris@bowron.us</a>&gt;<br>[ Nothing is exciting if you know what the outcome will be ]<br>
 &nbsp; &nbsp;- Joseph Campbell<br>