Re: Lazy living, without variables

2008-12-02 Thread Rich Hickey
On Dec 2, 8:43 am, Luke Amdor <[EMAIL PROTECTED]> wrote: > The "@" symbol is a reader macro. It's a hard coded table so that when > the reader comes across the @ symbol it knows to instead change it to > a deref call. For eg, > > (def a (ref 0)) > > (dosync (alter inc a)) > > @a ;; is the same t

Re: Lazy living, without variables

2008-12-02 Thread Luke Amdor
The "@" symbol is a reader macro. It's a hard coded table so that when the reader comes across the @ symbol it knows to instead change it to a deref call. For eg, (def a (ref 0)) (dosync (alter inc a)) @a ;; is the same thing as (deref a) http://clojure.org/reader#toc2 or http://clojure.org/ap

Re: Lazy living, without variables

2008-12-02 Thread Rich Hickey
On Dec 1, 9:35 pm, Timothy Pratley <[EMAIL PROTECTED]> wrote: > > The reason is simple - plain mutable variables have no concurrency > > semantics. What if you closed over a mutable local? Now you have an > > object with no synchronization, a concurrency mess. > > Thanks Rich for the clarificati

Re: Lazy living, without variables

2008-12-02 Thread David Powell
Hi, On the subject of with-local-vars, I noticed that I could use @ to deference them in addition to var-get. Is that intended behaviour? I didn't see it documented anywhere. -- Dave --~--~-~--~~~---~--~~ You received this message because you are subscribed t

Re: Lazy living, without variables

2008-12-01 Thread Timothy Pratley
;H but wait, I saw on the website that Clojure does have local variables! user=> (with-local-vars [a 2] (var-set a 3) (var-get a)) 3 ;I am allowed to close over them: user=> (def f (with-local-vars [a 2] #(+ 1 (var-get a #=(var user/f) ;But they stop me to shoot myself user=> (f) java.lan

Re: Lazy living, without variables

2008-12-01 Thread Timothy Pratley
> The reason is simple - plain mutable variables have no concurrency > semantics. What if you closed over a mutable local? Now you have an > object with no synchronization, a concurrency mess. Thanks Rich for the clarification... that makes it immediately obvious why using mutable locals is a bad

Re: Lazy living, without variables

2008-12-01 Thread Rich Hickey
On Dec 1, 7:37 pm, Timothy Pratley <[EMAIL PROTECTED]> wrote: > Local variables are good in these situations > > 1) You want to accumulate some changes eg: > // sum odds > int x = 0; > for (int i=0; i<100; i++) { >if ( i%2==1 ) x+=i; } > > ;It is relatively easy to rearrange these sort of th

Re: Lazy living, without variables

2008-12-01 Thread Timothy Pratley
Local variables are good in these situations 1) You want to accumulate some changes eg: // sum odds int x = 0; for (int i=0; i<100; i++) { if ( i%2==1 ) x+=i; } ;It is relatively easy to rearrange these sort of things into a form that requires no variables user=> (reduce #(+ %1 (if (= 1 (rem

Re: Lazy living, without variables

2008-12-01 Thread Jarkko Oranen
> Here's how I think you could do a mutable local: > (defn f [] >   (def mut-local) >   (binding [mut-local 0] >      (set! mut-local 1) >      mut-local)) > > (f) should yield 1 > > The flaw with this approach is that def actually creates a global > var.  It remains unbound, but the fact that th

Re: Lazy living, without variables

2008-12-01 Thread Ralf Bensmann
Hm... this way you have to setup a ref and a transaction, but its local: user=> (let [a (ref 0)] (dosync (ref-set a 1))) 1 user=> a java.lang.Exception: Unable to resolve symbol: a in this context (NO_SOURCE_FILE:0) -Ralf On Mon, Dec 1, 2008 at 10:07 PM, jim <[EMAIL PROTECTED]> wrote: > > The rul

Re: Lazy living, without variables

2008-12-01 Thread jim
The rule of thumb I used when I started with Clojure was if I'm tempted to use a def inside a function, I'm doing something wrong. Now, I'm not even tempted. (def mut-local) (defn f [] (binding [mut-local 0] (set! mut-local 1) mut-local)) Is the same as what you wrote. Functional

Re: Lazy living, without variables

2008-12-01 Thread puzzler
I'm often copying "classic algorithms" from various sources, and it is true that it can take some thought to do a functional conversion. Sometimes, it's easier to just go ahead and use mutable local variables. Fortunately, Clojure lets you do mutability if needed. But I'm still unclear on the be

Re: Lazy living, without variables

2008-12-01 Thread jim
I agree totally. I just implemented a language tokenizer that uses the maximal munch algorithm described in this paper. http://www.csc.lsu.edu/~gb/csc4351/Papers/Reps.pdf The algorithm given uses all kinds of variables and state. I was able to implement it without all that. I've also been abl

Lazy living, without variables

2008-12-01 Thread Stuart Halloway
Lazyness is just so cool: http://blog.thinkrelevance.com/2008/12/1/living-lazy-without-variables Feedback welcomed, Stu --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send e