View mode: basic / threaded / horizontal-split · Log in · Help
February 09, 2009
Re: Lambda syntax, etc
bearophile:
> auto r1 = range.find((ArrayType1!(typeof(range)) x) { return x.weight > 100; });

Better:
auto r1 = range.find((BaseType1!(typeof(range)) x) { return x.weight > 100; });

Of course, you have to specify range twice there, and that's not good.

Bye,
bearophile
February 09, 2009
Re: Lambda syntax, etc
bearophile wrote:
> grauzone Wrote:
>> I really like this one, but I'd prefer something like
>>  > auto r = find(range, {a -> a.Weight > 100});
>>
>> Note the -> instead of the =>, to avoid any ambiguities with the 
>> comparison operator.
> 
> Let's play more; then what do you think about (all the following are legal):
> 
> auto r1 = range.find({ x -> x.weight > 100 });
> auto r2 = range.find({ x :: return x.weight > 100; });
> auto r3 = range.find({ x :: stmt1(x); stmt2; });
> auto r4 = range.find({ x, y :: stmt1; return foo(y); });
> 
> I like those enough, they seem balanced, uniform, not too much error-prone, they have only one visual chunk, short enough and easy to write :-)

Agreed.

Especially I like that "normal" and "functional" uses have distinct 
syntax. This is much better than the proposal, to allow omission of the 
return statement, and to return last value of the last expression 
statement instead (like in (x){if(x>0) x+=1; x;}).

> Note that this syntax:
> auto r1 = range.find({ x -> x.weight > 100 });
> using my dlibs is equivalent to the following:
> auto r1 = range.find((ArrayType1!(typeof(range)) x) { return x.weight > 100; });
> 
> Bye,
> bearophile
February 10, 2009
Re: Lambda syntax, etc
"grauzone" <none@example.net> wrote in message 
news:gmpgod$fej$1@digitalmars.com...
> bearophile wrote:
>> Let's play more; then what do you think about (all the following are 
>> legal):
>>
>> auto r1 = range.find({ x -> x.weight > 100 });
>> auto r2 = range.find({ x :: return x.weight > 100; });
>> auto r3 = range.find({ x :: stmt1(x); stmt2; });
>> auto r4 = range.find({ x, y :: stmt1; return foo(y); });
>>
>> I like those enough, they seem balanced, uniform, not too much 
>> error-prone, they have only one visual chunk, short enough and easy to 
>> write :-)
>
> Agreed.
>
> Especially I like that "normal" and "functional" uses have distinct 
> syntax. This is much better than the proposal, to allow omission of the 
> return statement, and to return last value of the last expression 
> statement instead (like in (x){if(x>0) x+=1; x;}).
>

I'd rather have everything use "::" (consistent), and then say that the 
right-hand side of :: can be either A: one or more statements ("normal") or 
B: an expression ("functional").
February 19, 2009
Re: Lambda syntax, etc
Denis Koroskin Wrote:

> >> Could be used as follows:
> >>
> >> foo( (i) { ++i; } );
> >
> > Holy shi-
> > Now feel some real power, Luke. :)
> >
> > foo( i => ++i );
> >
> >>     foo((i){ ++i; }); // error
> >>     foo((i){ ++i; return;}); // unambiguous
> >>     foo((i){ return ++i;}); // unambiguous
> 
> That was just an example. Those short lambdas are often used as predicates. Compare:
> 
> findAll(array, (i) { i > 3; });
> findAll(array, (int i) { return i > 3; });

array.filter((x){x>3})
array.filter(x => x>3)
array.map(dep => dep.director)

I've run into one article. It points out that C# operator => can be viewed as convertion, so operations working with such convertions look rather natural and expressive.
Next ›   Last »
1 2 3
Top | Discussion index | About this forum | D home