www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem about lambda expressions

reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
Hello again! I'm learning D, and I encountered a problem.
I tried this code:
http://ideone.com/hkpT6
It works well. (Have no idea why codepad.org failed to compile it)
I tried to write a lambda instead of function f, but I got 
nothing printed.
Did I make something wrong?
Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
Sorry for my poor English :)
Mar 27 2012
next sibling parent reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
Oh, I also tried:
     void seq_apply(Params..., Args...)(void delegate(Params) 
func, Args args)
But I got a error:
     variadic template parameter must be last
Does it mean that there can only be one variadic template 
parameter? How to fix it?
Thanks
Mar 27 2012
next sibling parent reply dennis luehring <dl.soluz gmx.net> writes:
Am 27.03.2012 15:52, schrieb Tongzhou Li:
 Oh, I also tried:
       void seq_apply(Params..., Args...)(void delegate(Params)
 func, Args args)
 But I got a error:
       variadic template parameter must be last
 Does it mean that there can only be one variadic template
 parameter? How to fix it?
 Thanks
just a question: how on earth should the compiler seperate your 2 variadic parameters??? t( a,b,c,x,y,z ) -> where Params Start/End, where Args???? magic?
Mar 27 2012
parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Tuesday, 27 March 2012 at 14:12:38 UTC, dennis luehring wrote:
 Am 27.03.2012 15:52, schrieb Tongzhou Li:
 Oh, I also tried:
      void seq_apply(Params..., Args...)(void delegate(Params)
 func, Args args)
 But I got a error:
      variadic template parameter must be last
 Does it mean that there can only be one variadic template
 parameter? How to fix it?
 Thanks
just a question: how on earth should the compiler seperate your 2 variadic parameters??? t( a,b,c,x,y,z ) -> where Params Start/End, where Args???? magic?
Well... It's a question...
Mar 28 2012
prev sibling parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 03/27/12 15:52, Tongzhou Li wrote:
 Oh, I also tried:
     void seq_apply(Params..., Args...)(void delegate(Params) func, Args args)
 But I got a error:
     variadic template parameter must be last
 Does it mean that there can only be one variadic template parameter? How to
fix it?
I'm not sure what exactly you're trying to do, but maybe this will help: void seq_apply(Func, Args...)(Func func, Args args) { import std.traits; alias ParameterTypeTuple!Func Params; enum ArgNum = Params.length-1; func(args[0], args[1 .. ArgNum + 1]); static if (args.length > ArgNum + 1) { seq_apply(func, args[ArgNum + 1 .. args.length]); } } artur
Mar 27 2012
parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Tuesday, 27 March 2012 at 14:54:11 UTC, Artur Skawina wrote:
 On 03/27/12 15:52, Tongzhou Li wrote:
 Oh, I also tried:
     void seq_apply(Params..., Args...)(void delegate(Params) 
 func, Args args)
 But I got a error:
     variadic template parameter must be last
 Does it mean that there can only be one variadic template 
 parameter? How to fix it?
I'm not sure what exactly you're trying to do, but maybe this will help: void seq_apply(Func, Args...)(Func func, Args args) { import std.traits; alias ParameterTypeTuple!Func Params; enum ArgNum = Params.length-1; func(args[0], args[1 .. ArgNum + 1]); static if (args.length > ArgNum + 1) { seq_apply(func, args[ArgNum + 1 .. args.length]); } } artur
Yes, this is what I want to do. Thanks!
Mar 28 2012
prev sibling next sibling parent reply "Kenji Hara" <k.hara.pg gmail.com> writes:
On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:
 Hello again! I'm learning D, and I encountered a problem.
 I tried this code:
 http://ideone.com/hkpT6
 It works well. (Have no idea why codepad.org failed to compile 
 it)
 I tried to write a lambda instead of function f, but I got 
 nothing printed.
 Did I make something wrong?
 Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
 Sorry for my poor English :)
(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); } This lambda expression returns *a delegate has no parameter*. Instead: (obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1)) or: (obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }
Mar 27 2012
parent reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Tuesday, 27 March 2012 at 15:21:57 UTC, Kenji Hara wrote:
 On Tuesday, 27 March 2012 at 13:42:30 UTC, Tongzhou Li wrote:
 Hello again! I'm learning D, and I encountered a problem.
 I tried this code:
 http://ideone.com/hkpT6
 It works well. (Have no idea why codepad.org failed to compile 
 it)
 I tried to write a lambda instead of function f, but I got 
 nothing printed.
 Did I make something wrong?
 Compiler used: DMD32 D Compiler v2.058 (Win7 SP1 x64)
 Sorry for my poor English :)
(obj x, int a0, int a1) => { x.setxxx(a0); x.setyyy(a1); } This lambda expression returns *a delegate has no parameter*. Instead: (obj x, int a0, int a1) => (x.setxxx(a0), x.setyyy(a1)) or: (obj x, int a0, int a1){ x.setxxx(a0); x.setyyy(a1); }
I understood. But why it compiles instead of giving an error? What does the complier do when I write the wrong code?
Mar 28 2012
parent reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
 I understood. But why it compiles instead of giving an error? 
 What does the complier do when I write the wrong code?
Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
Mar 28 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 03/28/2012 10:28 AM, Tongzhou Li wrote:
 On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
 I understood. But why it compiles instead of giving an error? What
 does the complier do when I write the wrong code?
Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
(int a) => {return a;} Is the same thing as (int a){return ()=>a;}
Mar 28 2012
parent reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:
 On 03/28/2012 10:28 AM, Tongzhou Li wrote:
 On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
 I understood. But why it compiles instead of giving an error? 
 What
 does the complier do when I write the wrong code?
Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
(int a) => {return a;} Is the same thing as (int a){return ()=>a;}
I understand. { return a; } is a function which requires no params right?
Mar 28 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 03/28/2012 01:13 PM, Tongzhou Li wrote:
 On Wednesday, 28 March 2012 at 10:17:15 UTC, Timon Gehr wrote:
 On 03/28/2012 10:28 AM, Tongzhou Li wrote:
 On Wednesday, 28 March 2012 at 08:22:25 UTC, Tongzhou Li wrote:
 I understood. But why it compiles instead of giving an error? What
 does the complier do when I write the wrong code?
Oh, I mean if I write the wrong code, what objectcode does the compiler generate? My English is not good, sorry.
(int a) => {return a;} Is the same thing as (int a){return ()=>a;}
I understand. { return a; } is a function which requires no params right?
Yes.
Mar 28 2012
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/27/2012 06:42 AM, Tongzhou Li wrote:
 Hello again! I'm learning D, and I encountered a problem.
 I tried this code:
 http://ideone.com/hkpT6
 It works well. (Have no idea why codepad.org failed to compile it)
 I tried to write a lambda instead of function f, but I got nothing 
printed. The lambda syntax (=>) is only for when there is a single return statement in the function literal: http://dlang.org/expression.html#Lambda As Kenji Hara has shown, you can have more than one expression in the return statement by taking advantage of the comma operator but personally I would use something else instead: the longer syntax, a local function, etc. Ali
Mar 27 2012