Joel asked: Can your language do this?
And he gave this example to illustrate his point:
function Cook( i1, i2, f )
{
alert("get the " + i1);
f(i1);
f(i2);
}
Cook( "lobster",
"water",
function(x) { alert("put " + x + " in pot"); } );
Cook( "chicken",
"coconut",
function(x) { alert("boom boom the " + x); } );
Which outputs something like:
get the lobster
put lobster in pot
put water in pot
get the chicken
boom boom chicken
boom boom coconut.Simple, yeh?
Now how would you go about doing this in C#?
I can't find a way to do it without defining a delegate first. Albeit, a generic delegate... but it's still not quite as 'elegant'.
private delegate void D<T>(T x);
Set up your cook function -- almost the same (except we use generic types, to mimic javascript's non-static typing.
private void Cook<T1>(T1 i1, T1 i2, D<T1> f)
{
MessageBox.Show("get the " + i1);
f(i1);
f(i2);
}
And then rather than passing in anonymous functions, we can pass in anonymous delegates...
Cook("lobster",
"water",
delegate(string x) { MessageBox.Show("put " + x + " in pot"); });
Cook("chicken",
"coconut",
delegate(string x) { MessageBox.Show("boom boom the " + x); });
Your turn. ;-)
Next → ← PreviousMy book "Choose Your First Product" is available now.
It gives you 4 easy steps to find and validate a humble product idea.