an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Sunil S Nandihalli
Hello everybody, An object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise .. Is this way by design? can it be made to behave like a hash-map without any performance penalty in terms of behaving like a function? Thanks, Sunil. -- You

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Chas Emerick
d, there's always get, e.g. ((partial get some-record) :my-slot) - Chas On Dec 30, 2010, at 8:08 AM, Sunil S Nandihalli wrote: > Hello everybody, > An object of class created using defrecord does not implement IFn .. while > it behaves very similar to map otherwise .. Is this

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Stuart Sierra
You can make it implement IFn easily enough: (defrecord Foo [a b c] clojure.lang.IFn (invoke [this key] (get this key))) It has been debated whether or not this is a good idea, design-wise, but it should not be a performance penalty. -Stuart Sierra clojure.com -- You received this messag

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Alex Baranosky
I've been playing with making a macro to encapsulate Stuart's post, like this: (defmacro defrecord-ifn [name & args] `(defrecord ~name ~...@args clojure.lang.IFn (invoke [this key] (get this key (defrecord-ifn Foo [a b c]) (def foo (Foo. "A" "B" "C")) (prn (map foo [:a :c])) => ("

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Alex Osborne
Alex Baranosky writes: > I've been playing with making a macro to encapsulate Stuart's post, like this: > > (defmacro defrecord-ifn [name & args] >   `(defrecord ~name ~...@args >     clojure.lang.IFn >     (invoke [this key] (get this key > > (defrecord-ifn Foo [a b c]) > > (def foo (Foo. "A

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Alex Baranosky
Worked like a charm. Thanks Alex. -- 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 f

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Alyssa Kwan
Technically, there's no possibility of variable capture, but you should use gensyms: (defmacro defrecord-ifn [name & args] `(defrecord ~name ~...@args clojure.lang.IFn (invoke [this# key#] (get this# key# Thanks, Alyssa On Dec 30, 9:29 pm, Alex Baranosky wrote: > I've been playi

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Alyssa Kwan
Argh! Six minutes! :P On Dec 30, 9:42 pm, Alyssa Kwan wrote: > Technically, there's no possibility of variable capture, but you > should use gensyms: > > (defmacro defrecord-ifn [name & args] >   `(defrecord ~name ~...@args >      clojure.lang.IFn >      (invoke [this# key#] (get this# key# >

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread Sunil S Nandihalli
thanks everybody for the responses .. with macros around ... you just have to wish for it to get it .. but we need to be careful what we wish for :) Sunil. On Fri, Dec 31, 2010 at 8:18 AM, Alyssa Kwan wrote: > Argh! Six minutes! :P > > On Dec 30, 9:42 pm, Alyssa Kwan wrote: > > Technically, ther

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-30 Thread André Thieme
Am 31.12.2010 03:29, schrieb Alex Baranosky: I've been playing with making a macro to encapsulate Stuart's post, like this: (defmacro defrecord-ifn [name & args] `(defrecord ~name ~...@args clojure.lang.IFn (invoke [this key] (get this key (defrecord-ifn Foo [a b c]) (def foo

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-31 Thread Alyssa Kwan
Generating readable code for IDEs is not a good reason. You should think carefully about variable capture and decide which you want. Usually, in a macro-generated defn, I do want to capture the parameters, so I would use ~'this. On Dec 30, 11:54 pm, André Thieme wrote: > Am 31.12.2010 03:29, schr

Re: an object of class created using defrecord does not implement IFn .. while it behaves very similar to map otherwise ..

2010-12-31 Thread Robert McIntyre
Be sure to also implement the version of get which takes a not-found argument so that your objects will work with map code which uses this functionality. (defrecord map-like-object [field-1 field-2 etc] clojure.lang.IFn (invoke [this k] (get this k)) (invoke [this k not-found] (get this k no