View mode: basic / threaded / horizontal-split · Log in · Help
October 02, 2007
parameterized lazy expression
Hello,

is it possible somehow, to pass as a lazy argument expression with 
argument/s?

something like
foo((int x) x == 0);

where x will be passed as parameter to the exp inside the foo function:

void foo(lazy bool dg)
{
	if(dg(10)) {...}
}


i'd like not to have to use foo(bool delegate(int x) {return x == 0}) if 
possible

I hope i made myself clear, thanks for advices in advance :)

freeagle
October 02, 2007
Re: parameterized lazy expression
Reply to freeagle,

> Hello,
> 
> is it possible somehow, to pass as a lazy argument expression with
> argument/s?
> 
> something like
> foo((int x) x == 0);
> where x will be passed as parameter to the exp inside the foo
> function:
> 
> void foo(lazy bool dg)
> {
> if(dg(10)) {...}
> }
> i'd like not to have to use foo(bool delegate(int x) {return x == 0})
> if possible
> 
> I hope i made myself clear, thanks for advices in advance :)
> 
> freeagle
> 

the best you can do would be use the short form of delegates

foo((int x) {return x == 0;})

it's 3 char's longer than what you asked for but.


OTOH could the syntax be changed so that a delegate uses a statement rather 
than a block?

foo(bool delegate(int) a, int b);

foo((int x) return x == 0;, 5)
October 02, 2007
Re: parameterized lazy expression
BCS wrote:
> Reply to freeagle,
> 
>> Hello,
>>
>> is it possible somehow, to pass as a lazy argument expression with
>> argument/s?
>>
>> something like
>> foo((int x) x == 0);
>> where x will be passed as parameter to the exp inside the foo
>> function:
>>
>> void foo(lazy bool dg)
>> {
>> if(dg(10)) {...}
>> }
>> i'd like not to have to use foo(bool delegate(int x) {return x == 0})
>> if possible
>>
>> I hope i made myself clear, thanks for advices in advance :)
>>
>> freeagle
>>
> 
> the best you can do would be use the short form of delegates
> 
> foo((int x) {return x == 0;})
> 
> it's 3 char's longer than what you asked for but.
> 
> 
> OTOH could the syntax be changed so that a delegate uses a statement 
> rather than a block?
> 
> foo(bool delegate(int) a, int b);
> 
> foo((int x) return x == 0;, 5)
> 
> 


hmm, well, if thats the shortest possible form...

i wanted to make a template representing mathematical sets, that would 
be defined something like MSet!(int)(x > 0 && x < 10, x*2)
but with the returns and all, it looks weird:
MSet!(int)((int x) {return x > 0 && x < 10; }, (int x) { return x * 2; })

freeagle
October 02, 2007
Re: parameterized lazy expression
Reply to freeagle,

> 
> i wanted to make a template representing mathematical sets, that would
> be defined something like MSet!(int)(x > 0 && x < 10, x*2)
> but with the returns and all, it looks weird:
> MSet!(int)((int x) {return x > 0 && x < 10; }, (int x) { return x * 2;
> })
> freeagle
> 

there is one other option

void foo(inout int i, lazy bool b)
{
 int j = 0;
 do
   i = j++;
 while(dg());
}

use like this

int k;
foo(k, k <= 5);

I would consider that an "ugly hack".
October 03, 2007
Re: parameterized lazy expression
freeagle wrote:
> hmm, well, if thats the shortest possible form...
> 
> i wanted to make a template representing mathematical sets, that would
> be defined something like MSet!(int)(x > 0 && x < 10, x*2)
> but with the returns and all, it looks weird:
> MSet!(int)((int x) {return x > 0 && x < 10; }, (int x) { return x * 2; })
> 
> freeagle

You could always use something like this:

MSet!(int, "$ > 0 && $ < 10", "$ * 2");

Then use CTFE to replace the '$' with whatever symbol it uses
internally, and then string mixin the result.

Not *quite* as clean as you wanted, but at least there aren't any nasty
delegate literals!

	-- Daniel
October 03, 2007
Re: parameterized lazy expression
Daniel Keep Wrote:

> 
> 
> freeagle wrote:
> > hmm, well, if thats the shortest possible form...
> > 
> > i wanted to make a template representing mathematical sets, that would
> > be defined something like MSet!(int)(x > 0 && x < 10, x*2)
> > but with the returns and all, it looks weird:
> > MSet!(int)((int x) {return x > 0 && x < 10; }, (int x) { return x * 2; })
> > 
> > freeagle
> 
> You could always use something like this:
> 
> MSet!(int, "$ > 0 && $ < 10", "$ * 2");
> 
> Then use CTFE to replace the '$' with whatever symbol it uses
> internally, and then string mixin the result.
> 
> Not *quite* as clean as you wanted, but at least there aren't any nasty
> delegate literals!
> 
> 	-- Daniel


thanks Daniel
Top | Discussion index | About this forum | D home