On Feb 16, 2:01 am, Michael Sanders <[email protected]> wrote:
> 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)))))))

I think this might be considered a bit more idiomatic to make state a
parameter of the loop rather than a mutable variable. 

([tag]
   (let [rx (re-pattern (str "(?i)" tag "[[:space:]]*,(,|$)"))]
      (loop [line (read-line)
             state 0]
         (when line
            (let [new-state (or (when (and (not= "" (s/trim line))
                                           (not= "\t" (subs line 0 1)))
                                   (if (re-find rx line) 1 0))
                                 state)]
               (if (= new-state 1)
                  (println line))
               (recur (read-line) new-state))))))
  

   <mike
-- 
Mike Meyer <[email protected]>             http://www.mired.org/consulting.html
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

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

Reply via email to