Re: 1st script, last hurtle...
Armando, James, Mike. Thank you very much for the help, I'll certainly put your ideas to good use. Appreciate it! -- 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
1st script, last hurtle...
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)) -- 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
Re: 1st script, last hurtle...
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))) -- 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
Re: 1st script, last hurtle...
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
Re: 1st script, last hurtle...
On Feb 16, 2:01 am, Michael Sanders bluelamp...@gmail.com 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 m...@mired.org 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 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