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
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
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
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
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#
>
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
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
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
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])) => ("
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
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
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
12 matches
Mail list logo