[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 <mmmnow@gmail.com>:
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<T0,T1,T2,T3,T4,T5,T6,T7,T8,T9>' being compiled with [ T0=double (double), T1=boost::lambda::placeholder2_type, T2=boost::tuples::null_type, T3=boost::tuples::null_type, T4=boost::tuples::null_type, T5=boost::tuples::null_type, T6=boost::tuples::null_type, T7=boost::tuples::null_type, T8=boost::tuples::null_type, T9=boost::tuples::null_type ] c:\users\kott\documents\visual studio 2008\projects\mbi\implementation\src\main.cpp(698) : warning C4267: 'initializing' : conversion from 'size_t' to 'const int', possible loss of data c:\program files\microsoft visual studio 9.0\vc\include\numeric(45) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data c:\program files\microsoft visual studio 9.0\vc\include\numeric(54) : see reference to function template instantiation '_Ty std::_Accumulate<std::_Vector_const_iterator<double,_Alloc>,_Ty,_Fn2>(_InIt,_InIt,_Ty,_Fn2)' being compiled with [ _Ty=int, _Alloc=std::allocator<double>, _Fn2=boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::arithmetic_action<boost::lambda::plus_action>,boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<1>>,boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2,double>>,boost::tuples::tuple<double (__cdecl &)(double),const boost::lambda::lambda_functor<boost::lambda::placeholder<2>>,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>>, _InIt=std::_Vector_const_iterator<double,std::allocator<double>> ] c:\users\kott\documents\visual studio 2008\projects\mbi\implementation\src\main.cpp(613) : see reference to function template instantiation '_Ty std::accumulate<std::_Vector_const_iterator<double,_Alloc>,int,boost::lambda::lambda_functor<T>>(_InIt,_InIt,_Ty,_Fn2)' being compiled with [ _Ty=int, _Alloc=std::allocator<double>, T=boost::lambda::lambda_functor_base<boost::lambda::arithmetic_action<boost::lambda::plus_action>,boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<1>>,boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2,double>>,boost::tuples::tuple<double (__cdecl &)(double),const boost::lambda::lambda_functor<boost::lambda::placeholder<2>>,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>, _InIt=std::_Vector_const_iterator<double,std::allocator<double>>, _Fn2=boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::arithmetic_action<boost::lambda::plus_action>,boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::placeholder<1>>,boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,boost::lambda::function_action<2,double>>,boost::tuples::tuple<double (__cdecl &)(double),const boost::lambda::lambda_functor<boost::lambda::placeholder<2>>,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type,boost::lambda::detail::bind_traits<boost::tuples::null_type>::type>>>,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type,boost::tuples::null_type>>> ] what I'm doing wrong?
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 <mmmnow@gmail.com> 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) ); }
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