Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Berlin Brown
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 whi

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Chouser
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 fi

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Mike Hinchey
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 r

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Adrian Cuthbertson
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 (Buffer

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Adrian Cuthbertson
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

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Adrian Cuthbertson
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

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread David Nolen
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

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Berlin Brown
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 (

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread Daniel Lyons
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-

Re: Simple idiom in clojure, mutate a value

2009-06-11 Thread David Nolen
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)))

Simple idiom in clojure, mutate a value

2009-06-11 Thread BerlinBrown
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