Using lambda to count elements in list of lists

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using lambda to count elements in list of lists

Daniel Lidström
Hello!

I'm trying to use Boost.Lambda to count the total number of elements
in a list of lists. This example shows what I'm trying to do:

#include <boost/lambda/lambda.hpp>
#include <algorithm>
#include <list>

using namespace std;
using namespace boost::lambda;

int main()
{
   int count = 0;
   list<list<int> > ll;
   for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
}

This will not compile: (13): error C2039: 'size': is not a member of
'boost::lambda::lambda_functor<T>'
Is this possible to do?
Thanks in advance!

Hälsningar,
Daniel

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Using lambda to count elements in list of lists

Thorsten Ottosen
Daniel Lidström wrote:

> Hello!
>
> I'm trying to use Boost.Lambda to count the total number of elements
> in a list of lists. This example shows what I'm trying to do:
>
> #include <boost/lambda/lambda.hpp>
> #include <algorithm>
> #include <list>
>
> using namespace std;
> using namespace boost::lambda;
>
> int main()
> {
>    int count = 0;
>    list<list<int> > ll;
>    for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
> }
>
> This will not compile: (13): error C2039: 'size': is not a member of
> 'boost::lambda::lambda_functor<T>'
> Is this possible to do?

AFAICT, no. Not unless the type of _1 implements all possible member
functions in the world :-)

This is the single biggest problem of a non-langauage solution to lambdas.

-Thorsten

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Using lambda to count elements in list of lists

Stuart Dootson
In reply to this post by Daniel Lidström
On 2/1/06, Daniel Lidström <[hidden email]> wrote:

> Hello!
>
> I'm trying to use Boost.Lambda to count the total number of elements
> in a list of lists. This example shows what I'm trying to do:
>
> #include <boost/lambda/lambda.hpp>
> #include <algorithm>
> #include <list>
>
> using namespace std;
> using namespace boost::lambda;
>
> int main()
> {
>    int count = 0;
>    list<list<int> > ll;
>    for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
> }
>
> This will not compile: (13): error C2039: 'size': is not a member of
> 'boost::lambda::lambda_functor<T>'
> Is this possible to do?
> Thanks in advance!
>
> Hälsningar,
> Daniel
>

You'll need to use lambda's bind to access the size member function:

   for_each(ll.begin(), ll.end(), var(count)+=bind(&list<int>::size, _1));

You might might find std::accumulate a better match for your requirements:

   count = accumulate(ll.begin(), ll.end(), 0, _1 + bind(&list<int>::size, _2));

Stuart Dootson

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Using lambda to count elements in list of lists

Sebastian Redl
In reply to this post by Daniel Lidström
Daniel Lidström wrote:

>   for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
>
>This will not compile: (13): error C2039: 'size': is not a member of
>'boost::lambda::lambda_functor<T>'
>Is this possible to do?
>Thanks in advance!
>  
>
Yes, but you need binding syntax:

typedef list< list< int > > lli;
for_each(ll.begin(), ll.end(), var(count) += bind(&lli::size, _1));

Sebastian Redl
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Using lambda to count elements in list of lists

Peter Dimov
In reply to this post by Thorsten Ottosen
Thorsten Ottosen wrote:
> Daniel Lidström wrote:

>> int main()
>> {
>>    int count = 0;
>>    list<list<int> > ll;
>>    for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
>> }
>>
>> This will not compile: (13): error C2039: 'size': is not a member of
>> 'boost::lambda::lambda_functor<T>'
>> Is this possible to do?
>
> AFAICT, no. Not unless the type of _1 implements all possible member
> functions in the world :-)

    for_each( ll.begin(), ll.end(), count += bind( &list<int>::size, _1 ) );

works for me.

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Using lambda to count elements in list of lists

Daniel Lidström
In reply to this post by Daniel Lidström
Sebastian Redl scribbled:

> Yes, but you need binding syntax:
>
> typedef list< list< int > > lli;
> for_each(ll.begin(), ll.end(), var(count) += bind(&lli::size, _1));

Thanks to both Sebastian and Stuart!

Hälsningar,
Daniel

_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users