Simple dosync/alter question
I have a some what (I believe) easy question. Could someone let me know what I'm doing wrong? A simplified version of what I'm trying to do looks like this: user=> (def foo (ref 0)) #'user/foo user=> (defn square [x] (* x x)) #'user/square user=> (defn square-ref [x] (dosync (alter foo square x))) #'user/square-ref user=> (square-ref 2) java.lang.IllegalArgumentException: Wrong number of args passed to: user$square (NO_SOURCE_FILE:0) user=> (doc square) - user/square ([x]) nil nil user=> Thanks, Brandon --~--~-~--~~~---~--~~ 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 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 dosync/alter question
Alter expects a function that take the current value of the ref it will alter as the first arg. I think what you want to do is ref-set. user=> (doc alter) - clojure.core/alter ([ref fun & args]) Must be called in a transaction. Sets the in-transaction-value of ref to: (apply fun in-transaction-value-of-ref args) and returns the in-transaction-value of ref. nil user=> (doc ref-set) - clojure.core/ref-set ([ref val]) Must be called in a transaction. Sets the value of ref. Returns val. nil user=> (defn square-ref [x] (dosync (ref-set foo (square x #'user/square-ref user=> (square-ref 2) 4 user=> @foo 4 Paul On Mon, Apr 6, 2009 at 3:02 PM, bgray wrote: > > I have a some what (I believe) easy question. Could someone let me > know what I'm doing wrong? A simplified version of what I'm trying to > do looks like this: > > user=> (def foo (ref 0)) > #'user/foo > user=> (defn square [x] (* x x)) > #'user/square > user=> (defn square-ref [x] (dosync (alter foo square x))) > #'user/square-ref > user=> (square-ref 2) > java.lang.IllegalArgumentException: Wrong number of args passed to: > user$square (NO_SOURCE_FILE:0) > user=> (doc square) > - > user/square > ([x]) > nil > nil > user=> > > Thanks, > Brandon > > > --~--~-~--~~~---~--~~ 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 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 dosync/alter question
I think what you want is: (def foo (ref 0)) (defn square [x] (* x x)) (defn square-ref [x] (dosync (ref-set foo (square x (square-ref 2) On Mon, Apr 6, 2009 at 3:02 PM, bgray wrote: > > I have a some what (I believe) easy question. Could someone let me > know what I'm doing wrong? A simplified version of what I'm trying to > do looks like this: > > user=> (def foo (ref 0)) > #'user/foo > user=> (defn square [x] (* x x)) > #'user/square > user=> (defn square-ref [x] (dosync (alter foo square x))) > #'user/square-ref > user=> (square-ref 2) > java.lang.IllegalArgumentException: Wrong number of args passed to: > user$square (NO_SOURCE_FILE:0) > user=> (doc square) > - > user/square > ([x]) > nil > nil > user=> > > Thanks, > Brandon > > > --~--~-~--~~~---~--~~ 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 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 dosync/alter question
On Apr 6, 2009, at 9:02 AM, bgray wrote: > > I have a some what (I believe) easy question. Could someone let me > know what I'm doing wrong? A simplified version of what I'm trying to > do looks like this: > > user=> (def foo (ref 0)) > #'user/foo > user=> (defn square [x] (* x x)) > #'user/square > user=> (defn square-ref [x] (dosync (alter foo square x))) > #'user/square-ref > user=> (square-ref 2) > java.lang.IllegalArgumentException: Wrong number of args passed to: > user$square (NO_SOURCE_FILE:0) > Paul and David N. have already given you the right advice. But to help you understand why your square-ref function didn't work, compare it to this: (defn square-ref [x] (dosync (alter foo (fn [_] (square x) Do you see the difference? Aloha, David Sletten --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---