Summary
The C++ functional header provides a way to bind values to function arguments. Here is a way to bind functions to function arguments using a technique I'm calling meta-binders for lack of a better term.
If you want to do the same thing but instead apply a logical xor operation you would have to define a new
binary predicate since the STL does not provide one:
Even though this is easy enough, it brings up the question, can we create a predicate from existing
predicates without declaring a new function or function object. The standard library isn't much
help but it is possible if we introduce the concept of a meta-binder.
A meta-binder binds function to the arguments of another function. An xor function could then be
define inline as follows:
I would be curious if there is an existing name for this technique, probably those more familiar with higher-order functional
programming will have some good nomenclature for me.
Quite clean and clear, no? No need to mess around with binders or (explicit) function objects: this one makes a function object from the expression provided, and the two parameters to it are the "placeholders" _1 and _2. So the last line:
(_1 || _2) && !(_1 && _2)
creates something like:
struct (unnamed) { int operator()(int a,int b) { return (a || b) && !(a && b); } } (unnamed);
As bitwise XOR works the same as logical, for bools, one might also use: "_1 ^ _2"
As you say in the blog: since we're passing functions to functions (std::transform), std::transform (like much of STL) is really a higher-order function, =:) which is just a fancy name for functions that take or return functions. We can't normally pass functions around in C++ (only pointer or reference to them), but we may get the same effect with function objects, as used here.
I don't know if there's a particular name for composing functions like this, though.
Regards.
Terje
P.S: I had to use vector<int>, as vector<bool> is a little... odd. Its use of a proxy as a return value didn't work with BLL.
Note also the cute way to print the values after the transformation. :)
The Library TR has now gone out for voting (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1789.html: "The registration ballot and PDTR ballot for the Library TR closed last Wednesday, and the results of the ballot have not yet been received."), and even though a TR isn't technically part of the next standard, from what I understand, it's pretty sure to end up in the next standard.
And that's just the start... The committee is now working on proposed features like language support for "concepts", DbC and modules (the two latter, like in your Heron language, too). "The future's so bright, I gotta wear shades." ;)
Concepts, DbC and modules, as well as other things, were being presented and discussed at the last standards meeting at Lillehammer (where I also attended).
Regards,
Terje
P.S. The mentioning of Heron reminds me to take a better look at that language, for potentially good ideas for C++ extensions. As you may have found, it can be hard to reach a "critical mass" of users with a new language (there's a reason C++ built from, and was compatible with, C, an already popular language), but by extending an existing popular language, you may be able to improve the situation for millions of people... :)
> P.S. The mentioning of Heron reminds me to take a better > look at that language, for potentially good ideas for C++ > extensions. As you may have found, it can be hard to reach > a "critical mass" of users with a new language (there's a > reason C++ built from, and was compatible with, C, an > already popular language), but by extending an existing > popular language, you may be able to improve the situation > for millions of people... :)
You bring up a good point, and it could be a very worthwhile direction to take Heron. I am considering it seriously, but for the time being Heron is on hold.
> P.S. The mentioning of Heron reminds me to take a better > look at that language, for potentially good ideas for C++ > extensions. As you may have found, it can be hard to reach > a "critical mass" of users with a new language (there's a > reason C++ built from, and was compatible with, C, an > already popular language), but by extending an existing > popular language, you may be able to improve the situation > for millions of people... :)
>You bring up a good point, and it could be a very >worthwhile direction to take Heron. I am considering it >seriously, but for the time being Heron is on hold.
Yes, maybe there was a misunderstanding. What I meant was that since it may be hard to get a large user base for a new language, another direction might be to work on improving one that is already in widespread use (like C++), using one's experience in the new language, if you like.
>What I meant was that since it may be hard to get a large >user base for a new language, another direction might be >to work on improving one that is already in widespread use >(like C++), using one's experience in the new language, if >you like.
Support for "concepts" and DbC are two of the more exciting proposals I think exists (and they may complement each other, as well: both adding compiler/runtime checkable information: one doing compile-time checking of type properties, and another checking the run-time properties, like semantics. This also enables decoupling of call site and definition, and checking each, independent of each other).
I agree there are great things in TR1. I wonder when compilers will catch up with it? Judging by C++98 it could take 3-5 years :-(
IMVHO the most important things missing from the language are Threading (which seems to require the introduction of a memory model for C++ as well as a standard thread library), Modules (the only proposal that is going to reduce build times for C++ projects rather than add to them) and some kind of Networking/Socket library (which seems to be further away from standardisation than ever)
BTW Christopher's original post is very similar to the compose functions in Josutti's book. According to the book there was something like this in the original SGI STL, but was not included in the standard. See the code samples at http://www.josuttis.com
> I agree there are great things in TR1. I wonder when > compilers will catch up with it? Judging by C++98 it could > take 3-5 years :-(
Well, given that this is a pure library extension (i.e. no change to the compiler is needed), and there already exists free versions of more or less all of it (which may need to be slightly changed, to fit the changes done in the standardisation), it shouldn't be that big of a job for vendors to incorporate the free versions in their own libaries, if the want to.
> IMVHO the most important things missing from the language > are Threading (which seems to require the introduction of > a memory model for C++ as well as a standard thread > library)
Work is going on, on a memory model for C++, to start with. Then perhaps get basic threading primitives, and a threading library.