Boost logo

Boost Users :

From: Jaakko Jarvi (jajarvi_at_[hidden])
Date: 2002-12-02 11:26:37


Hi Again,

> >> 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)

< snipped a lot of stuff >

Ok, makes sense. So basically you'd need to specialize like this:

template <..., class F>
format(..., unary_lambda_functor<F>);

template <..., class F>
format(..., binary_lambda_functor<F>);

For this I can't see an easy way to do. It is however possible to
implement a traits class to query the arity of the lambda functor,
basically by recursing to all subexpressions. Then you could write your
format function as an interfaces function and dispatch to different format
functions like dispathing based on iterator categories in the standard
library.

Adding the functionality to LL adds a new requirement for all classes
that can act as base classes of lambda functors, and is thus a
library-wide change.
Being able to do a bit of reflection with lambda functors would make sense
though.

I won't rush into implementing this, but I may add it some point.

Cheers, Jaakko

>
> 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
>
>
>
>
> Yahoo! Groups Sponsor
> ADVERTISEMENT
>
> Info: <http://www.boost.org>
> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
> Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
>

-- 
--
-- Jaakko Järvi                       email: jajarvi_at_[hidden]
-- Post Doctoral Fellow               phone: +1 (812) 855-3608
-- Pervasive Technology Labs          fax:   +1 (812) 855-4829
-- Indiana University, Bloomington

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