One tactic I've used for this is function-generating functions or, in more
complex situations, closure-generating functions. I'd appreciate thoughts
from gurus on whether this is idiomatic.
For instance:
You have functions do-something-quickly and do-something-elegantly. You want
to call these in your code with (do-something foo), and specify at the top
which implementation is used.
So your library of functions, you could have:
(defn do-something-elegantly [& args] (do stuff))
(defn do-something-quickly [& args] (do-faster stuff))
and here's the important part:
(defn get-do-something [& args]
(if (some #{:quickly} args) ;i.e. if :quickly is among the args
do-something-quickly
do-something-elegantly))
Then the "configuration" at the top of your code consists of:
(def do-something (get-do-something :quickly))
removing the :quickly flag as appropriate.
And you go on to write:
(do-something stuff)
If you want the args to be passed through to do-something-*, it's easy to do
that by stripping the flags from the args in get-do-something and passing
them on. I have a set of simple functions in my library to parse flags.
You can also use this to manage more complex state. For instance, I have a
program that needs to initialize a connection at the top and pass the
connection handle to every function that uses the connection. But I don't
want the user to have to type the name of the connection handle every time
they call one of these functions. I have a closure-generating function that
closes over the connection handle variable while letting other arguments
remain free. It works like this:
(defn get-evaluator [& retained-args]
(fn [& args] (apply project.core/evaluate (concat args retained-args))))
Project.core/evaluate knows to look for the retained-args at the end of the
argument list. My user can then call
(def evaluate (get-evaluator connection-handle))
(evaluate args) ;"knows" to use the connection-handle
Anyone know if there any plans for Clojure to have native support for
passing subsets of arguments through, or for providing native helper
functions for parsing flags?
Garth
On Thu, Jul 16, 2009 at 11:34 AM, Dragan <[email protected]> wrote:
>
> Thanks for the tip, I meant something else.
> Let's say that I want to write a function do-something. There could be
> 2 implementations: do-something-quickly and do-something-elegantly.
> The parameters are the same and there are no differences in their
> "interface". I would like to be able to call it by writing (do-
> something arg) in my code and specify which implementation I want to
> use somewhere else (in the configuration part of the code).
>
> On Jul 16, 4:28 pm, Laurent PETIT <[email protected]> wrote:
> > Certainly multimethods :
> > * introduction :http://clojure.org/runtime_polymorphism
> > * detail :http://clojure.org/multimethods
> >
> > And, also, don't forget the power given to you by first class / higher
> order
> > functions. With them, a lot of "variability" can be placed just in
> > functions, where it was a pain in the ass to do correctly in java (you
> have
> > to first create an interface with a method, then derive the interface).
> And
> > often this was not done at all, and you ended up with a lot of similar
> > functions when their size was too small, etc.
> >
> > HTH,
> >
> > --
> > Laurent
> >
> > 2009/7/16 Dragan <[email protected]>
> >
> >
> >
> > > Hi,
> >
> > > This is another one of my newbie questions, so please be patient if
> > > this is something out of the FP paradigm.
> > > I've noticed that there is a number of "duplicate" functions,
> > > functions that implement the same thing in a different way. In OO
> > > systems, I would use an interface to achieve the indirection.
> > > What should I use in Cilojure?
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---