page 1 of 2
Comments: 25 | Views: 810

I've always found the lack of a comma operator irritating, but I recently realized I can simulate one, or at least its most common use (and in general put statement blocks wherever single statements or expressions are expected) by (ab)using the lambda syntax like so:

 

for((()=>{int i = 1; int j = 1})(); j < 10000; (()=>{i = j; j = i + j})()) {
   Console.WriteLine(i.ToString());
}

what do you think, sirs?   if you were my manager, would you have me lynched if you found the above in my code?

PaoloM
PaoloM
Hypermediocrity

if you were my manager, would you have me lynched if you found the above in my code?

Probably Smiley

 

If there is another way to accomplish what you need, you should always strive for readability and ease of maintenance.

Blue Ink
Blue Ink
C you

Lynched? No. I'm against unnecessary violence, especially when a fellow programmer is involved. But I wouldn't rule out some tarring and feathering. Smiley

 

1) I cannot get your code compiled on my machine, and I'm extremely puzzled by the notion of variables declared in the body of a lambda expression being in scope later...

2) The only scenario in which I ever found the comma operator useful is the for loop and given that C# already allows commas there, I don't see the need of complicating the code (technically, according to the C# specs, commas in a for are separators, not operators, but it amounts pretty much to the same thing)

CannotResolveSymbol
CannotResolveSymbol
{insert caption here}

No, not really.

 

I can't tell what that does at a glance.

CannotResolveSymbol
CannotResolveSymbol
{insert caption here}

Note to self:  caffeine and lambda expressions don't mix Tongue Out

Minh
Minh
WOOH! WOOH!

It's ugly as sin, especially compared to

 

int i = 1; j = 1;

 

do

{

   Console.WriteLine(i);

 

   int k = i;

   i = j;

   j = i + k;

}

while (j < 10000);

 

BTW, if I were your manager, and found this in your code, we're having words...

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

I find this discussion rather baffling.

 

for( int i = 1, j = 1; j < 10000; i = j, j = i + j )
{
    Console.WriteLine(i);
}

 

This works fine in C#. So what is the OP's problem?

Dr Herbie
Dr Herbie
Horses for courses

"So what is the OP's problem?"

 

Too much caffeine and "lambda-itis". Tongue Out

 

Herbie

Dr Herbie
Dr Herbie
Horses for courses

Well, in it's any consolation, you go up in my estimation for recognising your own fail.

 

Herbie

Someone should make a version of C++ that removes the comma operator (except in for statements) or at least issues a warning when it's used.

 

Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...

Or at least removes the ability to overload that operator, because that's a bundle of fun when someone does that. Smiley

exoteric
exoteric
I : Next<I>

More fun. Factor out the "doubles" loop and use side-effecting LINQ expressions for console output

1.Doubles().To(10000).Out().Do();

Wink

Dodo
Dodo
I'm your creativity creator™ :)

I find it to be rather questionable why you would want to do that. Everything in one line? Bad, very bad. Except if your compiler uses fewer assembly instructions or allocates less memory, there's no point in doing that. Today's compilers are smart, I've heard.

exoteric
exoteric
I : Next<I>

It's just for fun, I don't think contextfree would use that stuff IRL Wink

figuerres
figuerres
???

Teach a man to fish and he will feed himself.

teach a programmer to use a rope and he will hang himself.

 

keep the rope locked up around programmers!

 

 

exoteric
exoteric
I : Next<I>

Teach a man Brainf* and he'll go f* himself Big Smile

 

- Actually, Erik mentioned that all that's needed is the K-combinator (as in the SKI combinator calculus). If that's the case, then Brainf* is massive overkill. So K is like the assembly language of functional programming.

blowdart
blowdart
Peek-a-boo

Lambdas are rapidly turning into the regular expressions of .NET ...

staceyw
staceyw
Before C# there was darkness...

Another reason I don't like single line gumbo is debug stepping.  It is much easier when things are on seperate lines - besides being easier to reason about in general.  A downside of multi-line lambdas also, you break on the whole thing, not inside. 

stevo_
stevo_
Human after all

Ugh..

Minh
Minh
WOOH! WOOH!

Dang he actually convinced me there is no , operator... I knew I've seen it

page 1 of 2
Comments: 25 | Views: 810
Microsoft Communities