Re: Simple idiom in clojure, mutate a value
On Jun 11, 3:42 pm, Chouser wrote: > On Thu, Jun 11, 2009 at 3:03 PM, Adrian > > > > Cuthbertson wrote: > > > Here's a fuller example of similar techniques extracted from a working > > program. It reads a file of lines and applies some transformations and > > accumulates a vector of records which it finally returns; > > > (defn some-fn > > "Read a file and return a vector of its records." > > [fpath] > > (let > > [r (BufferedReader. (FileReader. (File. fpath)))] > > (try > > (let [line (.readLine r)] ; discard line 1 > > (loop [line (.readLine r) recs []] > > (if-not line recs > > (let [rec (do-something-with line) > > newrecs (conj recs rec)] > > (recur (.readLine r) newrec) > > (finally > > (.close r) > > To test this I'm using: > > (import '(java.io File FileReader BufferedReader)) > (defn do-something-with [line] (.toUpperCase line)) > > Note that (let [r ...] (try ... (finally (.close r is > already packaged up in the with-open macro: > > (defn some-fn > "Read a file and return a vector of its records." > [fpath] > (with-open [r (BufferedReader. (FileReader. (File. fpath)))] > (.readLine r) ; discard line 1 > (loop [line (.readLine r) recs []] > (if-not line > recs > (let [rec (do-something-with line) > newrecs (conj recs rec)] > (recur (.readLine r) newrecs)) > > Also note that this is a great candidate for line-seq: > > (defn some-fn > "Read a file and return a vector of its records." > [fpath] > (with-open [r (BufferedReader. (FileReader. (File. fpath)))] > (vec (map do-something-with > (next ; discard first line > (line-seq r)) > > --Chouser Yea, I think conj was what I was looking for. --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
On Thu, Jun 11, 2009 at 3:03 PM, Adrian Cuthbertson wrote: > > Here's a fuller example of similar techniques extracted from a working > program. It reads a file of lines and applies some transformations and > accumulates a vector of records which it finally returns; > > (defn some-fn > "Read a file and return a vector of its records." > [fpath] > (let > [r (BufferedReader. (FileReader. (File. fpath)))] > (try > (let [line (.readLine r)] ; discard line 1 > (loop [line (.readLine r) recs []] > (if-not line recs > (let [rec (do-something-with line) > newrecs (conj recs rec)] > (recur (.readLine r) newrec) > (finally > (.close r) To test this I'm using: (import '(java.io File FileReader BufferedReader)) (defn do-something-with [line] (.toUpperCase line)) Note that (let [r ...] (try ... (finally (.close r is already packaged up in the with-open macro: (defn some-fn "Read a file and return a vector of its records." [fpath] (with-open [r (BufferedReader. (FileReader. (File. fpath)))] (.readLine r) ; discard line 1 (loop [line (.readLine r) recs []] (if-not line recs (let [rec (do-something-with line) newrecs (conj recs rec)] (recur (.readLine r) newrecs)) Also note that this is a great candidate for line-seq: (defn some-fn "Read a file and return a vector of its records." [fpath] (with-open [r (BufferedReader. (FileReader. (File. fpath)))] (vec (map do-something-with (next ; discard first line (line-seq r)) --Chouser --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
Adrian and David are suggesting how to work functionally, which is best when appropriate. Berlin, if you really need state modified over time, Clojure does this differently from most languages. Values never change, only references to values and only in controlled ways. Maybe what you want is a ref, see http://clojure.org/refs Something like this: (def x (ref 0)) (dosync ... (alter x inc)) (println @x) -Mike On Thu, Jun 11, 2009 at 11:21 AM, Berlin Brown wrote: > > > > On Jun 11, 12:16 pm, Daniel Lyons wrote: > > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: > > > > > > > > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > > > am using some pseudo code because I don't have a clojure solution yet. > > > > > SET UPDATEABLE_VALUE = 0 > > > (loop [line (.readLine reader)] > > > (if (or (empty? line) (> line-num max-num)) > > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) > > >(recur (.readLine reader > > > > In general it's going to be something like this: > > > > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] > > (if (or (empty? line) (> line-num max-num)) > > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) > > > > Whenever you would have modified a local variable before, in FP you > > establish a new binding instead. > > > > — > > Daniel Lyonshttp://www.storytotell.org-- Tell It! > > I can modify the value within the loop, but what is a good approach > for accessing the value outside of the loop. For example (pseudo code > with a mix of procedural logic). > > ;; Init updateable value outside of 'loop' > SET UPDATEABLE_VALUE = 0 > (loop [line (.readLine reader)] > (if (or (empty? line) (> line-num max-num)) > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >(recur (.readLine reader > ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated > value > (println UPDATEABLE_VALUE) > > > > > > --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
Here's a fuller example of similar techniques extracted from a working program. It reads a file of lines and applies some transformations and accumulates a vector of records which it finally returns; (defn some-fn "Read a file and return a vector of its records." [fpath] (let [r (BufferedReader. (FileReader. (File. fpath)))] (try (let [line (.readLine r)] ; discard line 1 (loop [line (.readLine r) recs []] (if-not line recs (let [rec (do-something-with line) newrecs (conj recs rec)] (recur (.readLine r) newrec) (finally (.close r) On Thu, Jun 11, 2009 at 8:48 PM, Adrian Cuthbertson wrote: > Re-read your example - that should have been; > > (let [updated-val (loop [updateable 0 line (.readline reader)] > (if (or (empty? line) (> line-num max-num)) > (+ updateable (somefunc)) > (recur (.readLine reader)] > (do-something-with updated-val)) > > I.e initialising updateable to 0. > > > On Thu, Jun 11, 2009 at 8:38 PM, Adrian > Cuthbertson wrote: >> You could do something like; >> >> (let [updated-val (loop [updateable start-value line (.readline reader)] >> (if (or (empty? line) (> line-num max-num)) >> (+ updateable (somefunc)) >> (recur (.readLine reader)] >> (do-something with updated-val)) >> >> Rgds, Adrian. >> >> On Thu, Jun 11, 2009 at 8:34 PM, David Nolen wrote: >>> Why isn't the following satisfactory for your needs? get-value takes a value >>> returns a new value based on it. do-something can 'do something' to/with >>> this new value. >>> (defn get-value [start-value] >>> (loop [updateable start-value line (.readline reader)] >>> (if (or (empty? line) (> line-num max-num)) >>> (+ updateable (somefunc)) >>> (recur (.readLine reader) >>> (defn do-something [] >>> (let [updated-value (get-value 0)] >>> (println updated-value)) >>> On Thu, Jun 11, 2009 at 2:21 PM, Berlin Brown >>> wrote: On Jun 11, 12:16 pm, Daniel Lyons wrote: > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: > > > > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > > am using some pseudo code because I don't have a clojure solution yet. > > > SET UPDATEABLE_VALUE = 0 > > (loop [line (.readLine reader)] > > (if (or (empty? line) (> line-num max-num)) > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) > > (recur (.readLine reader > > In general it's going to be something like this: > > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] > (if (or (empty? line) (> line-num max-num)) > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) > > Whenever you would have modified a local variable before, in FP you > establish a new binding instead. > > — > Daniel Lyonshttp://www.storytotell.org-- Tell It! I can modify the value within the loop, but what is a good approach for accessing the value outside of the loop. For example (pseudo code with a mix of procedural logic). ;; Init updateable value outside of 'loop' SET UPDATEABLE_VALUE = 0 (loop [line (.readLine reader)] (if (or (empty? line) (> line-num max-num)) SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) (recur (.readLine reader ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated value (println UPDATEABLE_VALUE) >>> >>> >>> >>> >>> >> > --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
Re-read your example - that should have been; (let [updated-val (loop [updateable 0 line (.readline reader)] (if (or (empty? line) (> line-num max-num)) (+ updateable (somefunc)) (recur (.readLine reader)] (do-something-with updated-val)) I.e initialising updateable to 0. On Thu, Jun 11, 2009 at 8:38 PM, Adrian Cuthbertson wrote: > You could do something like; > > (let [updated-val (loop [updateable start-value line (.readline reader)] > (if (or (empty? line) (> line-num max-num)) > (+ updateable (somefunc)) > (recur (.readLine reader)] > (do-something with updated-val)) > > Rgds, Adrian. > > On Thu, Jun 11, 2009 at 8:34 PM, David Nolen wrote: >> Why isn't the following satisfactory for your needs? get-value takes a value >> returns a new value based on it. do-something can 'do something' to/with >> this new value. >> (defn get-value [start-value] >> (loop [updateable start-value line (.readline reader)] >> (if (or (empty? line) (> line-num max-num)) >> (+ updateable (somefunc)) >> (recur (.readLine reader) >> (defn do-something [] >> (let [updated-value (get-value 0)] >> (println updated-value)) >> On Thu, Jun 11, 2009 at 2:21 PM, Berlin Brown >> wrote: >>> >>> >>> >>> On Jun 11, 12:16 pm, Daniel Lyons wrote: >>> > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: >>> > >>> > >>> > >>> > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I >>> > > am using some pseudo code because I don't have a clojure solution yet. >>> > >>> > > SET UPDATEABLE_VALUE = 0 >>> > > (loop [line (.readLine reader)] >>> > > (if (or (empty? line) (> line-num max-num)) >>> > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >>> > > (recur (.readLine reader >>> > >>> > In general it's going to be something like this: >>> > >>> > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] >>> > (if (or (empty? line) (> line-num max-num)) >>> > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) >>> > >>> > Whenever you would have modified a local variable before, in FP you >>> > establish a new binding instead. >>> > >>> > — >>> > Daniel Lyonshttp://www.storytotell.org-- Tell It! >>> >>> I can modify the value within the loop, but what is a good approach >>> for accessing the value outside of the loop. For example (pseudo code >>> with a mix of procedural logic). >>> >>> ;; Init updateable value outside of 'loop' >>> SET UPDATEABLE_VALUE = 0 >>> (loop [line (.readLine reader)] >>> (if (or (empty? line) (> line-num max-num)) >>> SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >>> (recur (.readLine reader >>> ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated >>> value >>> (println UPDATEABLE_VALUE) >>> >>> >>> >>> >> >> >> >> >> > --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
You could do something like; (let [updated-val (loop [updateable start-value line (.readline reader)] (if (or (empty? line) (> line-num max-num)) (+ updateable (somefunc)) (recur (.readLine reader)] (do-something with updated-val)) Rgds, Adrian. On Thu, Jun 11, 2009 at 8:34 PM, David Nolen wrote: > Why isn't the following satisfactory for your needs? get-value takes a value > returns a new value based on it. do-something can 'do something' to/with > this new value. > (defn get-value [start-value] > (loop [updateable start-value line (.readline reader)] > (if (or (empty? line) (> line-num max-num)) > (+ updateable (somefunc)) > (recur (.readLine reader) > (defn do-something [] > (let [updated-value (get-value 0)] > (println updated-value)) > On Thu, Jun 11, 2009 at 2:21 PM, Berlin Brown > wrote: >> >> >> >> On Jun 11, 12:16 pm, Daniel Lyons wrote: >> > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: >> > >> > >> > >> > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I >> > > am using some pseudo code because I don't have a clojure solution yet. >> > >> > > SET UPDATEABLE_VALUE = 0 >> > > (loop [line (.readLine reader)] >> > > (if (or (empty? line) (> line-num max-num)) >> > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >> > > (recur (.readLine reader >> > >> > In general it's going to be something like this: >> > >> > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] >> > (if (or (empty? line) (> line-num max-num)) >> > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) >> > >> > Whenever you would have modified a local variable before, in FP you >> > establish a new binding instead. >> > >> > — >> > Daniel Lyonshttp://www.storytotell.org-- Tell It! >> >> I can modify the value within the loop, but what is a good approach >> for accessing the value outside of the loop. For example (pseudo code >> with a mix of procedural logic). >> >> ;; Init updateable value outside of 'loop' >> SET UPDATEABLE_VALUE = 0 >> (loop [line (.readLine reader)] >> (if (or (empty? line) (> line-num max-num)) >> SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >> (recur (.readLine reader >> ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated >> value >> (println UPDATEABLE_VALUE) >> >> >> >> > > > > > --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
Why isn't the following satisfactory for your needs? get-value takes a value returns a new value based on it. do-something can 'do something' to/with this new value. (defn get-value [start-value] (loop [updateable start-value line (.readline reader)] (if (or (empty? line) (> line-num max-num)) (+ updateable (somefunc)) (recur (.readLine reader) (defn do-something [] (let [updated-value (get-value 0)] (println updated-value)) On Thu, Jun 11, 2009 at 2:21 PM, Berlin Brown wrote: > > > > On Jun 11, 12:16 pm, Daniel Lyons wrote: > > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: > > > > > > > > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > > > am using some pseudo code because I don't have a clojure solution yet. > > > > > SET UPDATEABLE_VALUE = 0 > > > (loop [line (.readLine reader)] > > > (if (or (empty? line) (> line-num max-num)) > > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) > > >(recur (.readLine reader > > > > In general it's going to be something like this: > > > > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] > > (if (or (empty? line) (> line-num max-num)) > > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) > > > > Whenever you would have modified a local variable before, in FP you > > establish a new binding instead. > > > > — > > Daniel Lyonshttp://www.storytotell.org-- Tell It! > > I can modify the value within the loop, but what is a good approach > for accessing the value outside of the loop. For example (pseudo code > with a mix of procedural logic). > > ;; Init updateable value outside of 'loop' > SET UPDATEABLE_VALUE = 0 > (loop [line (.readLine reader)] > (if (or (empty? line) (> line-num max-num)) > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >(recur (.readLine reader > ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated > value > (println UPDATEABLE_VALUE) > > > > > > --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
On Jun 11, 12:16 pm, Daniel Lyons wrote: > On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: > > > > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > > am using some pseudo code because I don't have a clojure solution yet. > > > SET UPDATEABLE_VALUE = 0 > > (loop [line (.readLine reader)] > > (if (or (empty? line) (> line-num max-num)) > > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) > >(recur (.readLine reader > > In general it's going to be something like this: > > (loop [line (.readLine reader) UPDATEABLE_VALUE 0] > (if (or (empty? line) (> line-num max-num)) > (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) > > Whenever you would have modified a local variable before, in FP you > establish a new binding instead. > > — > Daniel Lyonshttp://www.storytotell.org-- Tell It! I can modify the value within the loop, but what is a good approach for accessing the value outside of the loop. For example (pseudo code with a mix of procedural logic). ;; Init updateable value outside of 'loop' SET UPDATEABLE_VALUE = 0 (loop [line (.readLine reader)] (if (or (empty? line) (> line-num max-num)) SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) (recur (.readLine reader ;; Now outside of the loop, use UPDATEABLE_VALUE with the mutated value (println UPDATEABLE_VALUE) --~--~-~--~~~---~--~~ 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: Simple idiom in clojure, mutate a value
On Jun 11, 2009, at 9:25 AM, BerlinBrown wrote: > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > am using some pseudo code because I don't have a clojure solution yet. > > SET UPDATEABLE_VALUE = 0 > (loop [line (.readLine reader)] > (if (or (empty? line) (> line-num max-num)) > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >(recur (.readLine reader In general it's going to be something like this: (loop [line (.readLine reader) UPDATEABLE_VALUE 0] (if (or (empty? line) (> line-num max-num)) (recur (.readLine reader) (+ UPDATEABLE_VALUE (SOME_FUNC) Whenever you would have modified a local variable before, in FP you establish a new binding instead. — Daniel Lyons http://www.storytotell.org -- Tell 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 -~--~~~~--~~--~--~---
Re: Simple idiom in clojure, mutate a value
Not totally clear what you are trying to accomplish, but would something like the following work? (defn get-value [start-value] (loop [updateable start-value line (.readline reader)] (if (or (empty? line) (> line-num max-num)) (+ updateable (somefunc)) (recur (.readLine reader) (defn do-something [] (let [updated-value (get-value 0)] ...)) On Thu, Jun 11, 2009 at 11:25 AM, BerlinBrown wrote: > > I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I > am using some pseudo code because I don't have a clojure solution yet. > > SET UPDATEABLE_VALUE = 0 > (loop [line (.readLine reader)] > (if (or (empty? line) (> line-num max-num)) > SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) >(recur (.readLine reader > > -- > > basically I want to have access to some value UPDATEABLE_VALUE outside > of the loop but also have the ability to update the > value. > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Simple idiom in clojure, mutate a value
I do this a lot but can't figure out to change the UPDATEABLE_VALUE, I am using some pseudo code because I don't have a clojure solution yet. SET UPDATEABLE_VALUE = 0 (loop [line (.readLine reader)] (if (or (empty? line) (> line-num max-num)) SET UPDATEABLE_VALUE = UPDATEABLE_VALUE + (SOME_FUNC) (recur (.readLine reader -- basically I want to have access to some value UPDATEABLE_VALUE outside of the loop but also have the ability to update the value. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---