Re: Allow Data Structure to Be Called as Function

2011-06-20 Thread Ryan Twitchell
Most notably, reify does not "def" anything. It's very in-tune with functional programming in that way, as it has no side-effects, whereas defrecord adds a class to the namespace. reify has been compared to Java's anonymous classes. A good example of its use might be in implementing a factory me

Re: Allow Data Structure to Be Called as Function

2011-06-20 Thread Jonathan Fischer Friberg
Sometimes I'm just too eager; I changed the macro so that the first argument to the function is the current record (i.e. "this"). user=> (definvokerecord (fn [this & args] (apply + args)) ATEST []) user.ATEST user=> (def a (ATEST.)) #'user/a user=> (a 1 2 3) 6 user=> (a 1 2 3 4 5) 15 Jonathan O

Re: Allow Data Structure to Be Called as Function

2011-06-20 Thread Jonathan Fischer Friberg
Maybe I should add how to actually use it: the first argument is a function which will be called when the record is called. the rest is simply the arguments to defrecord, as usual. Jonathan On Mon, Jun 20, 2011 at 3:23 PM, Jonathan Fischer Friberg < odysso...@gmail.com> wrote: > Here is said ma

Re: Allow Data Structure to Be Called as Function

2011-06-20 Thread Jonathan Fischer Friberg
Here is said macro: https://gist.github.c om/1035590 user=> (definvokerecord (fn [& args] (apply + args)) ATEST []) user.ATEST user=> (ATEST.) #:user.ATEST{} user=> (def a (ATEST.)) #'user/a user=> (a 1 2 3) 6 user=> (a 1 2 3 4 5) 1

Re: Allow Data Structure to Be Called as Function

2011-06-19 Thread Ken Wesson
On Sat, Jun 18, 2011 at 10:47 AM, David Nolen wrote: > On Sat, Jun 18, 2011 at 4:44 AM, Sam Aaron wrote: >> >> Is it possible to use this approach to create a callable record which can >> take a variable number of arguments? >> >> I can't get the following to work: >> >> (defrecord Foo [a] >>  cl

Re: Allow Data Structure to Be Called as Function

2011-06-18 Thread David Nolen
On Sat, Jun 18, 2011 at 4:44 AM, Sam Aaron wrote: > Is it possible to use this approach to create a callable record which can > take a variable number of arguments? > > I can't get the following to work: > > (defrecord Foo [a] > clojure.lang.IFn > (invoke [this & args] (println (str a args

Re: Allow Data Structure to Be Called as Function

2011-06-18 Thread Ken Wesson
On Sat, Jun 18, 2011 at 4:44 AM, Sam Aaron wrote: > Is it possible to use this approach to create a callable record which can > take a variable number of arguments? > > I can't get the following to work: > > (defrecord Foo [a] >  clojure.lang.IFn >  (invoke [this & args] (println (str a args

Re: Allow Data Structure to Be Called as Function

2011-06-18 Thread Sam Aaron
Is it possible to use this approach to create a callable record which can take a variable number of arguments? I can't get the following to work: (defrecord Foo [a] clojure.lang.IFn (invoke [this & args] (println (str a args (def yo (Foo. "sam")) (yo 1 2 3 4) ;=> sc-one.Foo.invoke(Ljav

Re: Allow Data Structure to Be Called as Function

2011-06-15 Thread Stuart Halloway
> You could also use reify: > > (defn make-foo [s] > (reify clojure.lang.IFn > (invoke [this] (str "Hello, " s > > ((make-foo "RJ")) > "Hello, RJ" > > I have to admit, though, that I'm unclear on the relative merits of defrecord > vs. reify. Anyone want to comment? > > Cheers, > -M

Re: Allow Data Structure to Be Called as Function

2011-06-15 Thread RJ Nowling
Thank you, Ken and Michael. Not knowing how to do that bothered me since it felt like I couldn't create data structures that were on the same level of the built-in data structures. :) On Jun 15, 6:59 pm, Michael Nygard wrote: > You could also use reify: > > (defn make-foo [s] >   (reify clojure

Re: Allow Data Structure to Be Called as Function

2011-06-15 Thread Michael Nygard
You could also use reify: (defn make-foo [s] (reify clojure.lang.IFn (invoke [this] (str "Hello, " s ((make-foo "RJ")) "Hello, RJ" I have to admit, though, that I'm unclear on the relative merits of defrecord vs. reify. Anyone want to comment? Cheers, -Michael Nygard On Jun 15, 20

Re: Allow Data Structure to Be Called as Function

2011-06-15 Thread Ken Wesson
On Wed, Jun 15, 2011 at 3:53 PM, RJ Nowling wrote: > Hi, > > I'm sorry if this has been asked before, but I would like to know how > to create data structures in Clojure that can be used in the same way > as the built-in data structures.  For example, I can access the > elements of a vector by (my

Allow Data Structure to Be Called as Function

2011-06-15 Thread RJ Nowling
Hi, I'm sorry if this has been asked before, but I would like to know how to create data structures in Clojure that can be used in the same way as the built-in data structures. For example, I can access the elements of a vector by (my-vec 1). How can I implement this interface when creating a da