[lambda] newbie: lambda to help in std::accumulate

I want to compute sum of logarithms so i wrote: #include <cmath> double evaluate(const vector<double>& win_probabls) { return accumulate (win_probabls.begin(), win_probabls.end(), 0, lambda::_1 + log(lambda::_2) ); } but this code doesn't compile. Does anyone know why? -- Michał Nowotka

2009/1/25 Michał Nowotka
I want to compute sum of logarithms so i wrote:
#include <cmath>
double evaluate(const vector<double>& win_probabls) { return accumulate (win_probabls.begin(), win_probabls.end(), 0, lambda::_1 + log(lambda::_2) ); }
but this code doesn't compile. Does anyone know why?
--
Michał Nowotka
'log' is a function, so a call to it in a lambda expression has to be within a bind expression: double evaluate(const std::vector<double>& win_probabls) { using namespace boost::lambda; return accumulate (win_probabls.begin(), win_probabls.end(), 0, _1 + bind(log, _2) ); } Stuart Dootson

Ok, i now I've corrected my method to:
double evaluate(const vector<double>& win_probabls)
{
return accumulate (win_probabls.begin(), win_probabls.end(), 0,
lambda::_1 + lambda::bind(log, lambda::_2) );
}
and got following warning (which i can't understand):
c:\program files\boost\boost_1_36_0\boost\lambda\detail\lambda_traits.hpp(389)
: warning C4180: qualifier applied to function type has no meaning;
ignored
c:\program files\boost\boost_1_36_0\boost\lambda\detail\lambda_traits.hpp(458)
: see reference to class template instantiation
'boost::lambda::detail::bind_traits<T>' being compiled
with
[
T=double (double)
]
c:\users\kott\documents\visual studio
2008\projects\mbi\implementation\src\main.cpp(613) : see reference to
class template instantiation
'boost::lambda::detail::bind_tuple_mapper

AMDG Michał Nowotka wrote:
Ok, i now I've corrected my method to:
double evaluate(const vector<double>& win_probabls) { return accumulate (win_probabls.begin(), win_probabls.end(), 0, lambda::_1 + lambda::bind(log, lambda::_2) ); }
and got following warning (which i can't understand):
c:\program files\boost\boost_1_36_0\boost\lambda\detail\lambda_traits.hpp(389) : warning C4180: qualifier applied to function type has no meaning;
what I'm doing wrong
You're not doing anything wrong. Lambda has never really been warning-proofed for msvc. In Christ, Steven Watanabe

On Thu, Jan 29, 2009 at 4:06 PM, Michał Nowotka
Ok, i now I've corrected my method to:
double evaluate(const vector<double>& win_probabls) { return accumulate (win_probabls.begin(), win_probabls.end(), 0, lambda::_1 + lambda::bind(log, lambda::_2) ); }
what I'm doing wrong?
There is one thing - change the 0 in the accumulate to 0.0, otherwise the accumulator is typed as an integer. That's the source of the C4244 warning, and will also result in the wrong answer being returned. Stuart Dootson
participants (3)
-
Michał Nowotka
-
Steven Watanabe
-
Stuart Dootson