[lambda] Variable assignment from lambda expression
Hi all, I have been trying out boost::lambda for the first time, and I did encounter something which I can't seem to solve. Simplified, what I'd like to do amounts to this: list<int> intlist; // fill up list someway int n; for_each( intlist.begin(), intlist.end(), n = _1 ); Obviously, this fails because it implies a cast from placeholder to int, which isn't defined. Is there some way to do this using lambda? -- Alex Borghgraef
the var function makes a lambda functor out of a variable, so you can write the expression as: var(n) = _1 Best, Jaakko On Sep 26, 2005, at 9:13 AM, Alexander Borghgraef wrote:
Hi all,
I have been trying out boost::lambda for the first time, and I did encounter something which I can't seem to solve. Simplified, what I'd like to do amounts to this:
list<int> intlist; // fill up list someway int n; for_each( intlist.begin(), intlist.end(), n = _1 );
Obviously, this fails because it implies a cast from placeholder to int, which isn't defined. Is there some way to do this using lambda?
-- Alex Borghgraef_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 9/26/05, jarvi
the var function makes a lambda functor out of a variable, so you can write the expression as:
var(n) = _1
Thanks, I should have seen that one in the manual. Posting at the end of a work day is obviously not a good idea :-) I've got another lambda related question though. My example was a useless simplification, what I'm really trying to do is to read data from an STL vector into a legacy matrix class which doesn't have an iterator. for_each( raster.begin(), raster.end(), ( var( out2( h - 1 - c / w, c % w ) ) = _1, ++var( c ) ) ); -- Alex Borghgraef
On 9/26/05, jarvi
the var function makes a lambda functor out of a variable, so you can write the expression as:
var(n) = _1
Thanks, I should have seen that one in the manual. Posting at the end of a work day is obviously not a good idea :-) I've got another lambda related question though. My example was a useless simplification, what I'm really trying to do is to read data from a container v into a legacy matrix class which doesn't have an iterator. I can do this with a for loop: std::vector<char> v; MyMatrix<char> m; // Has h and w members for height and width, and element access using operator()( int i, int j ) std::vector<char>::iterator v_it = v.begin(); for ( long c = 0; c < m.h * m.w; ++c ) { m( c / m.w , c % m.w ) = *v_it; ++v_it; } This works perfectly. I figured it should be possible to do the same thing using for_each and boost::lambda, so I tried this approach: long c = 0; for_each( v.begin(), v.end(), ( var( m( c / m.w, c % m.w ) ) = _1, ++var( c ) ) ); This also fills MyMatrix, but with different values than the for loop. Which seems surprising to me. As far as I can tell, these two approaches should be equivalent, but apparently they're not. Anyone care to explain to me why? P.S. Sorry for the unfinished post which got through. Please ignore that one. -- Alex Borghgraef
On 9/27/05, Alexander Borghgraef
On 9/26/05, jarvi
wrote: the var function makes a lambda functor out of a variable, so you can write the expression as:
var(n) = _1
Thanks, I should have seen that one in the manual. Posting at the end of a work day is obviously not a good idea :-) I've got another lambda related question though. My example was a useless simplification, what I'm really trying to do is to read data from a container v into a legacy matrix class which doesn't have an iterator. I can do this with a for loop:
std::vector<char> v; MyMatrix<char> m; // Has h and w members for height and width, and element access using operator()( int i, int j ) std::vector<char>::iterator v_it = v.begin(); for ( long c = 0; c < m.h * m.w; ++c ) { m( c / m.w , c % m.w ) = *v_it; ++v_it; }
This works perfectly. I figured it should be possible to do the same thing using for_each and boost::lambda, so I tried this approach:
long c = 0; for_each( v.begin(), v.end(), ( var( m( c / m.w, c % m.w ) ) = _1, ++var( c ) ) );
This also fills MyMatrix, but with different values than the for loop. Which seems surprising to me. As far as I can tell, these two approaches should be equivalent, but apparently they're not. Anyone care to explain to me why?
P.S. Sorry for the unfinished post which got through. Please ignore that one.
-- Alex Borghgraef
That's not going to work because the operator() call in the loop (i.e. m(...)) is evaluated when the lambda expression is created, not each time the lambda expression is evaluated. Try replacing var( m( c / m.w, c % m.w ) ) with bind(&MyMatrix::operator(), var(m), var(c) / m->*&MyMatrix::w, var(c) % m->*&MyMatrix::w) This makes all accesses to m & c lazy (I think), so should update m as you want. Stuart Dootson
On 9/27/05, Stuart Dootson
That's not going to work because the operator() call in the loop (i.e. m(...)) is evaluated when the lambda expression is created, not each time the lambda expression is evaluated.
Try replacing
var( m( c / m.w, c % m.w ) )
with
bind(&MyMatrix::operator(), var(m), var(c) / m->*&MyMatrix::w, var(c) % m->*&MyMatrix::w)
This makes all accesses to m & c lazy (I think), so should update m as you want.
Ok, that fixes it. Now, hardcore algorithmism aside, I don't see any advantage in this over the for loop: it's slower, and can hardly be called an improvement in readability. So I think I'll stick to the old-fashioned syntax in this case. Still, interesting to know that boost::lambda actually can do this. -- Alex Borghgraef
"Alexander Borghgraef"
Alexander Borghgraef wrote:
Ok, that fixes it. Now, hardcore algorithmism aside, I don't see any advantage in this over the for loop: it's slower, and can hardly be called an improvement in readability. So I think I'll stick to the old-fashioned syntax in this case. Still, interesting to know that boost::lambda actually can do this.
YMMV. In this case, I'd use BOOST_FOREACH. Cheers, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
On 9/29/05, Joel de Guzman
YMMV. In this case, I'd use BOOST_FOREACH.
Where do I find this? I've grepped my /usr/include/boost dir for BOOST_FOREACH, without success, and documentation seems to be hard to find as well, there's a foreach lib on the boost-consulting site, but the html file is lost, and on the boost.org http://boost.org site, there's no reference to it anymore. -- Alex Borghgraef
Alexander Borghgraef wrote:
On 9/29/05, *Joel de Guzman*
mailto:joel@boost-consulting.com> wrote: YMMV. In this case, I'd use BOOST_FOREACH.
Where do I find this? I've grepped my /usr/include/boost dir for BOOST_FOREACH, without success, and documentation seems to be hard to find as well, there's a foreach lib on the boost-consulting site, but the html file is lost, and on the boost.org http://boost.org site, there's no reference to it anymore.
It's a newly accepted library. You'd have to get it from the CVS. It's a simple single header utility. I'm not sure where you can find an onlne doc. I'm sure there is somewhere. I'm CC'ing Eric. Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
Alexander Borghgraef wrote:
On 9/29/05, *Joel de Guzman*
mailto:joel@boost-consulting.com> wrote: YMMV. In this case, I'd use BOOST_FOREACH.
Where do I find this? I've grepped my /usr/include/boost dir for BOOST_FOREACH, without success, and documentation seems to be hard to find as well, there's a foreach lib on the boost-consulting site,
That's it.
but the html file is lost,
What do you mean?
and on the boost.org http://boost.org site, there's no reference to it anymore.
The foreach.zip file in the File Vault on boost-consulting.com is what you want: http://www.boost-consulting.com/vault/index.php?directory=Algorithms The documentation is online at: http://boost-sandbox.sf.net/libs/foreach -- Eric Niebler Boost Consulting www.boost-consulting.com
On 9/29/05, Eric Niebler
but the html file is lost,
What do you mean?
On http://boost-consulting.com/boost/libs/libraries.htm there is a link to the foreach library, but when you follow it to http://boost-consulting.com/boost/doc/html/foreach.html you'll get a 404 error.
and on the boost.org http://boost.org http://boost.org site, there's no
reference to it anymore.
Well, apparently that should be 'yet', not 'anymore'. Sorry, I thought it was an old lib, not a new one :-) The foreach.zip file in the File Vault on boost-consulting.comhttp://boost-consulting.comis what
you want:
http://www.boost-consulting.com/vault/index.php?directory=Algorithms
The documentation is online at:
Thanks, I found the vault in the meantime. -- Alex Borghgraef
participants (6)
-
Alexander Borghgraef
-
Eric Niebler
-
jarvi
-
Jeff Flinn
-
Joel de Guzman
-
Stuart Dootson