How can I save the values in lambda expression? Can any one with kindness help me?

View: New views
7 Messages — Rating Filter:   Alert me  

How can I save the values in lambda expression? Can any one with kindness help me?

by fmingu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am using Dev-C++ with boost lambda expression, I wrote:
.....................
int nnumber;
int switchvalue;
typedef std::map<int, int> IIPrimemap;
IIPrimemap primemapvec;
 # define BetaKGre2   1
 # define EqualToOne  2
 #define  ProductOfKPrime  3
......................
  if_then_else(var(nnumber)==1,var(switchvalue)=EqualToOne,
  if_then_else(constant(count_if(primemapvec.begin(),primemapvec.end(),
       bind(&IIPrimemap::value_type::second,_1)>2)>0),
        var(switchvalue)=BetaKGre2,var(switchvalue)=ProductOfKPrime));
  std::cout<<" the var(switchvalue) is  "<<var(switchvalue)<<"\n"; 
...............
and the program ran and showed:
the var(switchvalue) is 
 
without any expression of var(switchvalue).
I do not know how to save the values in lambda expression and where is wrong?
Can I use *var(switchvalue) instead of var(switchvalue)?
Can anyone with kindness help me?
 


 



200万种商品,最低价格,疯狂诱惑你

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AMDG

fmingu wrote:

> I am using Dev-C++ with boost lambda expression, I wrote:
> ....................
> int nnumber;
> int switchvalue;
> typedef std::map<int, int> IIPrimemap;
> IIPrimemap primemapvec;
>  # define BetaKGre2   1
>  # define EqualToOne  2
>  #define  ProductOfKPrime  3
> .....................
>   if_then_else(var(nnumber)==1,var(switchvalue)=EqualToOne,
>   if_then_else(constant(count_if(primemapvec.begin(),primemapvec.end(),
>        bind(&IIPrimemap::value_type::second,_1)>2)>0),
>         var(switchvalue)=BetaKGre2,var(switchvalue)=ProductOfKPrime));
>   std::cout<<" the var(switchvalue) is  "<<var(switchvalue)<<"\n";
> ..............
> and the program ran and showed:
> the var(switchvalue) is
>  
> without any expression of var(switchvalue).
> I do not know how to save the values in lambda expression and where is wrong?
> Can I use *var(switchvalue) instead of var(switchvalue)?
> Can anyone with kindness help me?
>  
Think about it. What exactly is var(switchvalue)?
What does std::cout << var(switchvalue) do?

In Christ,
Steven Watanabe


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by fmingu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As you know, I am a Chinese and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet. So all I have to do is to ask www.boost.org to get answers and go forth.
I think the var(switchvalue) is a pointer to a function from the example code in the book:
..........
std::for_each(vec.begin(),vec.end(),var(m)=_1);
..............
So the operations in m can be performed. Am I right?
But I do not know how to save the values in lambda expression.
Thanks a lot.
 

 

在2009-07-09,"Steven Watanabe" <watanabesj@...> 写道:
>AMDG
>
>fmingu wrote:
>> I am using Dev-C++ with boost lambda expression, I wrote:
>> ....................
>> int nnumber;
>> int switchvalue;
>> typedef std::map<int, int> IIPrimemap;
>> IIPrimemap primemapvec;
>>  # define BetaKGre2   1
>>  # define EqualToOne  2
>>  #define  ProductOfKPrime  3
>> .....................
>>   if_then_else(var(nnumber)==1,var(switchvalue)=EqualToOne,
>>   if_then_else(constant(count_if(primemapvec.begin(),primemapvec.end(),
>>        bind(&IIPrimemap::value_type::second,_1)>2)>0),
>>         var(switchvalue)=BetaKGre2,var(switchvalue)=ProductOfKPrime));
>>   std::cout<<" the var(switchvalue) is  "<<var(switchvalue)<<"\n"; 
>> ..............
>> and the program ran and showed:
>> the var(switchvalue) is 
>>  
>> without any expression of var(switchvalue).
>> I do not know how to save the values in lambda expression and where is wrong? 
>> Can I use *var(switchvalue) instead of var(switchvalue)?
>> Can anyone with kindness help me?
>>   
>
>Think about it. What exactly is var(switchvalue)?
>What does std::cout << var(switchvalue) do?
>
>In Christ,
>Steven Watanabe
>



200万种商品,最低价格,疯狂诱惑你

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by Zachary Turner-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/8 fmingu <fmingu@...>:

> As you know, I am a Chinese and according to my knowledge few book in
> Chinese introduce boost library. I only have the book " Beyond the C++
> Standard Library: An introduction to boost" (written by Bjoern Karlsson,
> translated in Chinese).Only several pages said about lambda. I tried to ask
> www.csdn.net( a Chinese developers' net) and few people answered my
> questions. I have not studied the source code of lambda yet. So all I have
> to do is to ask www.boost.org to get answers and go forth.
> I think the var(switchvalue) is a pointer to a function from the example
> code in the book:
> ..........
> std::for_each(vec.begin(),vec.end(),var(m)=_1);
> ..............
> So the operations in m can be performed. Am I right?
> But I do not know how to save the values in lambda expression.
> Thanks a lot.

Try to do only

std::cout << switchvalue << std::endl;

The value has already been saved, there is no need to use var outside
of the lambda expression.  for example, in the for_each code you
posted, the reason to use var is because

std::for_each(vec.begin(),vec.end(), m=_1);

is not legal.  However, using var it will basically have the same
effect.  Once the lambda function has executed, m directly contains
the updated value and you can access it like any variable.
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AMDG

fmingu wrote:
> As you know, I am a Chinese

As a matter of fact I didn't know.

> and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet.

The official documentation is at http://www.boost.org/libs/lambda.
I believe that there is also an Chinese translation project which
you might want to check.
http://code.google.com/p/boost-doc-zh/

> So all I have to do is to ask www.boost.org to get answers and go forth.
> I think the var(switchvalue) is a pointer to a function from the example code in the book:
>  

var(switchvalue) actually returns a function object which
returns a reference to switchvalue.
A function pointer wouldn't work for numerous reasons.
The point I was trying to make was that
std::cout << var(switchvalue)
doesn't actually do anything. It returns another function object.
So, for example, this will work
(std::cout << var(switchvalue))();
and will print the value of switchvalue.
Not that you'd actually want to write code like
that. The correct way is to leave off the var
as Zachary noted.

> ..........
> std::for_each(vec.begin(),vec.end(),var(m)=_1);
> ..............
> So the operations in m can be performed. Am I right?
> But I do not know how to save the values in lambda expression.
>  

In Christ,
Steven Watanabe


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by fmingu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After your advice,I wrote the following test program:
int main(int argc, char *argv[])
{
  int nnumber=7;
  int switchvalue=1;
  if_then_else(var(nnumber)==6, var(switchvalue)=3
                                ,var(switchvalue)=30);
  std::cout<<" the nnumber is  "<<nnumber<<" and switchvalue is  "<<switchvalue<<"\n";
  system("PAUSE"); 
  return 0;
}
and the result is:
the nnumber is 7 and  and switchvalue is  1
Even
if_then_else(var(nnumber)==6, var(switchvalue)=constant(3)
                                ,var(switchvalue)=constant(30));
get the same result.
the value in the lambda expression did not saved.
Can you tell me why? And How can I correct it?
Thanks a lot.


在2009-07-09,"Steven Watanabe" <watanabesj@...> 写道:
>AMDG
>
>fmingu wrote:
>> As you know, I am a Chinese
>
>As a matter of fact I didn't know.
>
>> and according to my knowledge few book in Chinese introduce boost library. I only have the book " Beyond the C++ Standard Library: An introduction to boost" (written by Bjoern Karlsson, translated in Chinese).Only several pages said about lambda. I tried to ask www.csdn.net( a Chinese developers' net) and few people answered my questions. I have not studied the source code of lambda yet.
>
>The official documentation is at http://www.boost.org/libs/lambda.
>I believe that there is also an Chinese translation project which
>you might want to check.
>http://code.google.com/p/boost-doc-zh/
>
>> So all I have to do is to ask www.boost.org to get answers and go forth. 
>> I think the var(switchvalue) is a pointer to a function from the example code in the book:
>>   
>
>var(switchvalue) actually returns a function object which
>returns a reference to switchvalue.
>A function pointer wouldn't work for numerous reasons.
>The point I was trying to make was that
>std::cout << var(switchvalue)
>doesn't actually do anything. It returns another function object.
>So, for example, this will work
>(std::cout << var(switchvalue))();
>and will print the value of switchvalue.
>Not that you'd actually want to write code like
>that. The correct way is to leave off the var
>as Zachary noted.
>
>> ..........
>> std::for_each(vec.begin(),vec.end(),var(m)=_1);
>> ..............
>> So the operations in m can be performed. Am I right?
>> But I do not know how to save the values in lambda expression.
>>   
>
>In Christ,
>Steven Watanabe
>



200万种商品,最低价格,疯狂诱惑你

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: How can I save the values in lambda expression? Can any one with kindness help me?

by Johan Nilsson-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


"fmingu" <fmingu@...> wrote in message
news:31100020.905591247202195143.JavaMail.coremail@......

>After your advice,I wrote the following test program:
>int main(int argc, char *argv[])
>{
>  int nnumber=7;
>  int switchvalue=1;
>  if_then_else(var(nnumber)==6, var(switchvalue)=3
>                                ,var(switchvalue)=30);
>  std::cout<<" the nnumber is  "<<nnumber<<" and switchvalue is
> "<<switchvalue<<"\n";
>  system("PAUSE");
>  return 0;
>}
>and the result is:
>the nnumber is 7 and  and switchvalue is  1
>Even
>if_then_else(var(nnumber)==6, var(switchvalue)=constant(3)
>                                ,var(switchvalue)=constant(30));
>get the same result.
>
>the value in the lambda expression did not saved.
>Can you tell me why?
if_then_else only creates and returns a lambda function object.

>And How can I correct it?

You need to apply it (note invoking the fn call operator):

if_then_else(var(nnumber)==6, var(switchvalue)=3
                               ,var(switchvalue)=30)();

HTH / Johan



_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users