the c++ boost bind library and clojure’s partial function are very similar. For example:
int x = 8;
bind(std::less<int>(), _1, 9)(x); // x < 9
This is similar to clojure’s partial function:
((partial > 9) 8)
The difference is that partial only allows the first n parameters to be bound, whereas boost::bind allows placeholders indicating which parameters are bound and which are unbound. So boost::bind is actually much more general and useful:
bind(f, _2, _1)(x, y); // f(y, x)
bind(g, _1, 9, _1)(x); // g(x, 9, x)
I’m wondering if there is something similar to boost::bind in clojure (or clojure-contrib)? And why partial was not written to be more general (and useful) as boost::bind is?
This is a fairly frequently asked question of Clojure, more often couched in terms of the threading macros -> and ->> as to why they also don’t allow arbitrary placeholders.
The reason given for those also applies here in my opinion: idiomatic Clojure functions generally breakdown into ones that lend themselves to having either their first or their last arguments supplied separately, not a mixture.
In other words, developers generally try to code functions so that they are amenable to ->, ->> and/or partial.
Given the reader macro for anonymous functions, it’s fairly easy to create the placeholder versions when they’re needed, for less idiomatic situations: