Hi Greg, here's a sample but realistic pattern of the sort of thing
you're doing;

(import '(java.io BufferedReader FileReader File IOException)
   '(bqutils BQUtil))

(defn samp-loop
  "Read a csv file containing user records."
  [#^String fpath]
  (with-open [r (BufferedReader. (FileReader. (File. fpath)))]
    (let [; read line 1 for field names
          line (.readLine r)
          flds (seq (BQUtil/parseCsv line))
          nflds (count flds)]
      (try
        (loop [line (.readLine r) i 0 ]
          (when line
            (let [ri (seq (BQUtil/parseCsv line))
                  _ (when (not (= (count ri) nflds))
                      (throw (IOException. (str "Bad line:"i))))
                  [uid lname inits] ri]
              (println :lname lname :inits inits :uid uid)
              (if (= "" uid)
                (println "Breaking at line:" i)
                (recur (.readLine r) (inc i) )))))
        (catch Exception x (prn (.toString x)))
        (finally (println "Done:" flds))))))


A few notes;
- BQUtil is home-grown java fn for parsing csv to ArrayLists.
Note the wrapping in seq and then destructuring to get the
field values for each line.
- The outer let (flds) is in scope in the (finally
- The (with-open has its own (finally
- You can throw within the loop - note the throw within
the let using a dummy _ placeholder
- Note the line counter
- Note the technique to  "break" when uid = ""
(assuming that's what you wanted to do)

Hth, Adrian.


On Tue, Dec 15, 2009 at 4:07 AM, Greg Harman <ghar...@gmail.com> wrote:
> I have a function foo which uses tail recursion:
>
> (defn foo [] .... (recur))
>
> I need to do some clean-up work after foo (there is an external
> binding that requires some post-foo processing), and this needs to
> happen even if foo fails. The naive approach was:
>
> (try (foo)
>  (finally (clean-up))
>
> However, foo recurs and you can't recur in a try/catch/finally.
> (java.lang.UnsupportedOperationException: Cannot recur from catch/
> finally)
>
> So, my question is: does anyone have a pattern for exception checking
> (this same issue would work if I just wanted to catch an exception in
> foo) when the contents of the try block recur?
>
> I could just remove the tail recursion and go for straight recursion
> in foo, but there's got to be a better way...
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to