Looks like you could make the (println) depend directly on (re-find):
(when (re-find rx line)
  (println line))

The check for empty line and tab might be taken care of by (re-find)
to further reduce the code to something like:
(when (and line (re-find rx line)
  (println line)
  (recur (read-line)))

On Feb 16, 2:01 am, Michael Sanders <bluelamp...@gmail.com> wrote:
> mss wrote:
> > Hi folks, new to Clojure...
>
> > Question:
>
> > How do I rid the following functions of all the '(def STATE num)'
> > calls? I can not seem to work out a way to use either 'binding', or
> > 'let/atom' that wraps the loop & works as well... Any other comments
> > are always welcomed. Here's my code:
>
> > (defn load-block ([foo]
>
> > (def STATE 0)
> > (def rx (re-pattern (str "(?i)" foo "[[:space:]]*(,|$)")))
>
> >  (loop [line (read-line)]
> >    (when line
> >      (when (and (not= "" (s/trim line))     ; line not empty
> >                 (not= "\t" (subs line 0 1))) ; line does not begin w/
> > a tab
> >            (if (re-find  rx line)
> >                (def STATE 1)
> >                (def STATE 0)))
> >    (if (= STATE 1)
> >        (println line))
> >      (recur (read-line))))))
>
> I think I've worked out a better solution (hope this post renders properly):
>
> (ns topic (require [clojure.string :as s]))
>
> (defn load-block
>
> "TOPIC database: prints associated blocks to stdout"
>
>     ([tag]
>         (let [STATE (atom 0)
>                rx (re-pattern (str "(?i)" tag "[[:space:]]*(,|$)"))]
>             (loop [line (read-line)]
>                 (when line
>                     (when (and (not= "" (s/trim line))
>                             (not= "\t" (subs line 0 1)))
>                         (if (re-find  rx line)
>                             (reset! STATE 1)
>                             (reset! STATE 0)))
>                     (if (= @STATE 1)
>                         (println line))
>             (recur (read-line)))))))- Hide quoted text -
>
> - Show quoted text -

-- 
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