Boost logo

Boost Users :

From: Claus Rasmussen (yg-boost-users_at_[hidden])
Date: 2002-11-27 14:00:59


Jaakko Jarvi wrote:

>> Is it possible to discern one-argument lambda expressions from
>> two-argument expressions ? I would like to do something like this:
>
> Currently no. Can you tell a bit more where you would need this kind of
> feature. It is possible to make this possible if there is real need.

I am writing a set of formatting function for list output (or actually
output of ranges of iterators). The signature of one of the functions is
like this:

    format(begin, end, separator, function)

The function can be used like this:

    cout << format(l.begin(), l.end(), ", ", 2 * _1) << endl;

If 'l' is a list holding the values 1, 2, and 3 the output would be:

    2, 4, 6

(the advantage when comparing to ostream_iterator is that format uses
a separator while ostream_iterator uses a terminator)

Now I would like to provide a variation of this function where the lamda
function took two arguments: The element type of the list as before, and
the ostream. That would make it possible to write:

    cout << format(l.begin(), l.end(), ", ", _2 << setw(4) << _1) << endl;

(it becomes more relevant when the element type is eg. a double)

I know I could just ditch the first version, but the primary aim of these
functions is that they should be very easy to use (read: with minimum
typing). And in the overwhelming majority of the cases one would use the
first version. Secondly; the example is just a simplified example. Other
versions of the function take more paramters like this:

    format(begin, end, prefix, separator, postfix)

If it were possible to determine if an argument were a lambda expression
one could write:

    format(
        l.begin(), l.end(),
       _1 << "(" << endl, _1 << "," << endl, _1 << endl << ")")

to generate the following output:

    (
    1,
    2,
    3
    )

Again; this is a simplified example - but you get the idea.

The function are documented here:

    http://www.cc-consult.dk/prj/format2/html/index.html

It is fully tested but the documentation and packaging is still needs
some polish. It do not aim for inclusion in boost BTW - its just one of
my pet projects that I'am using as a show-off for potential employers
while looking for a job.

        -Claus


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net