On Feb 26, 9:24 am, Anand Patil <anand.prabhakar.pa...@gmail.com>
wrote:
> Hi all,
>
> I could use a version of 'partial' that would allow me to:
>
> - Partially apply a function to any of its arguments, not just the
> first one
> - 'Unapply' a partially-applied function from one of its arguments.
>
> Is any such thing already available?
Other people have explained currying and partial application, and why
it doesn't normally spply the feature you want.
Normally, in a lnaguage that supplies partial application, the way to
achieve the effect you want is to use combinators that change the
order of arguments.
A combinator is a function whose arguments are functions, and whose
return value is a function; partial is a combinator.
Suppose you have a function that works like this:
(foo x y)
...and you want to partially apply foo, but the argument you want to
apply it to is the y argument, not the x argument. You can't, because
partially applying foo will apply it to the x argument.
What you need is a combinator that reverses the order of foo's
arguments. You want to call
(swap foo)
and get back a function that works exactly like foo, except that it
taks arguments y x instead of x y.
It turns out that this is easy to write:
(defn swap [f]
(fn [y x]
(f x y)))
swap is the combinator you need in this case. You can get the effect
described above by doing (partial (swap f) y)
So, to get the effect you want, figure out the transformation you
would need on the argument list and write a combinator that implements
it.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---