Joel asked: Can your language do this?
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

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. ;-)





'Josh' on Fri, 04 Aug 2006 04:42:18 GMT, sez:

I wonder how many people played with this after Joel's post :)

Two things though - Java is VERY statically typed. It's JavaScript that isn't and they're quite different.

Secondly, you can actually type your anonymous delegate on the fly, i.e.:

new D<string>(delegate(string s) {...

can be replaced with just

delegate(string s) provided the sigs match.

Keep up the great work.

Josh



'Wesner Moise' on Fri, 04 Aug 2006 04:58:06 GMT, sez:

C# syntax is nearly as compact as Javascript, save for typed parameters.

Some caveats:
1)Generics is not necessary at all
2)String concatenation works with any object (as long as one of the parameters is a string)
3)Anonymous delegates are implicitly cast to the right delegate type (so you never need to add a cast).

void Cook( object i1, object i2, Action(object) f )
{
alert("get the " + i1);
f(i1);
f(i2);
}

Cook( "lobster",
"water",
delegate(x) { MessageBox.Show("put " + x + " in pot"); } );
Cook( "chicken",
"coconut",
delegate(x) { MessageBox.Show("boom boom the " + x); } );

Alternatively, you can use a delegate and DynamicInvoke for latebinding:
void Cook( object i1, object i2, Delegate f )
{
alert("get the " + i1);
f.DynamicInvoke(i1);
f.DynamicInvoke(i2);
}



'Wesner Moise' on Fri, 04 Aug 2006 05:01:05 GMT, sez:

Some errors in my last post:
1) Use delegate(object x) { ... } since all parameters require a type.
2) alert needs to be replaced with MessageBox.Show()



'Christian' on Fri, 04 Aug 2006 05:26:52 GMT, sez:

Ruby can:

----
def Cook( i1, i2 )
puts "get the #{i1}"
yield(i1)
yield(i2)
end

Cook( "lobster", "water") { |x| puts("put #{x} in pot") }
Cook( "chicken", "coconut") { |x| puts("boom boom the #{x}") }
----



'lb' on Fri, 04 Aug 2006 10:49:25 GMT, sez:

@Josh, re "Java is VERY statically typed. It's JavaScript that isn't"

sorry Josh, just a typo on my part. fixed.

also @Josh:
I took out the "new D<string>(" bit... yes glad that works without it. I originally had it all quite a bit more over wrought again.

I'm growing to like the way the code looks now.

@wesner:
i originally had f.DynamicInvoke -- except i found i had to wrap the args in an object array -- something like

f.DynamicInvoke(new object[]() = {i1});

how yucky is that?

re:"alert needs to be replaced with MessageBox.Show()"

...or abstracted out:

private void alert(string s) {
MessageBox.Show(s)
}

;-)

@christian: yes yes, we all know ruby is beautifully elegant. Thank you for sharing though -- i was pretty keen to find the ruby syntax for this kind of activity. whenever i see the |x| type syntax i think of 'why the lucky stiff' describing it as a little tube that the parameter slides down on its way into the block...

cheers
lb



'WaterBreath' on Fri, 04 Aug 2006 16:20:33 GMT, sez:

@Christian and lb: I actually would not describe the Ruby syntax as particularly elegant, though it is certainly concise. The reason I would not use the word "elegant" is that although the syntax is very compact, it seems not very mathematical.

The reason I say this is that the anonymous function (called by the "yield" keyword) doesn't quite appear and work like a normal function argument, for two reasons: 1) it has it's own place, outside the argument list and 2) you need a special (and ambiguous) keyword to reference it. (I say it's ambiguous because the first time I saw "yield", I assumed it meant the same thing as "return". Though I understand now that it references the current method "yielding control" to the other block.)

Then there's the fact that the Pragmatic guys, in "Programming Ruby" say it should not be thought of as an argument:

"Some people like to think of the association of a block with a method as a kind of parameter passing. This works on one level, but it isn't really the whole story. You might be better off thinking of the block and the method as coroutines, which transfer control back and forth between themselves."
(found here: http://www.rubycentral.com/book/intro.html)

And if it's not intended to work like a parameter, then it really is not-mathematical. Convenient, yes. Concise, yes. But IMHO it's no more elegant than the slightly more verbose but mathematical C# syntax Mr. Moise posted.



'J' on Fri, 04 Aug 2006 16:50:34 GMT, sez:

Just an FYI. They are not called anonymous delegates. The proper terminology is anonymous methods. The methods are anonymous, not the delegate.



'J' on Fri, 04 Aug 2006 17:09:07 GMT, sez:

Sorry, let me correct what I stated... They are not anonymous delegates. (Obviously it appears people call them what they are not.)



'mike' on Fri, 04 Aug 2006 19:41:32 GMT, sez:

Nikhil took this one on, too:

http://www.nikhilk.net/Entry.aspx?id=137



'Sun Digging Deep for Dynamic Language Support' on Tue, 08 Aug 2006 02:51:40 GMT, sez:

Java ('s virtual machine) isn't always going to be so VERY "statically" typed !!



'ks' on Wed, 09 Aug 2006 02:35:00 GMT, sez:

The powershell answer to this is even better!!

basically anything in "{}" is an anonymous method already (a block actually) -- (provided it's not preceeded by a function name and brackets.)

PS> Function Cook ($i1, $i2, $f)
>> { Alert "Get the $i1"
>> &$f $i1
>> &$f $i2
>> }
PS> Cook Lobster Water {Param($x) Alert "Pot $x"}
Get the Lobster
Pot Lobster
Pot Water
PS> Cook Chicken Coconut {Param($x) Alert "Boom $x"}
Get the Chicken
Boom Chicken
Boom Coconut



'DOA' on Tue, 29 Aug 2006 11:18:58 GMT, sez:

PHP can do it. Who knew....




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Aim for constructiveness. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Also, I get particularly nasty on comment spam. It's not worth even trying to post comment spam here -- your html is escaped, and your links are given a rel='nofollow'. By attempting to post a comment, you understand that if the comment is considered spam, at my absolute discretion, your IP address may be used as the target of a prolonged distributed denial of service attack. Your electricity might suddenly stop working. Your car tyres will go mysteriously flat. You will suffer permanent hairloss. Your dreams will be filled with terrifying monsters. And in any case I reserve the right to record and publish your IP address.

 

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

 

NimbleText - FREE text manipulation and data extraction

NimbleText is a Powerful FREE Tool

Use it for:

  • extracting data from text
  • manipulating text
  • generating code

It makes you look awesome. Use it right now! Go on! Hurry! Don't walk, run!

 

Articles

Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion Mind-boggling Demo of New Gaming Genre, aka Folder-Based Hangman, aka Fun with Recursion
Got CSV in your javascript? Use agnes. Got CSV in your javascript? Use agnes.
I went to write down a book name and founded an internet empire instead. I went to write down a book name and founded an internet empire instead.
NimbleText: Origins NimbleText: Origins
The Windows 8 Mullet The Windows 8 Mullet
Cosby: spontaneous striped background generator Cosby: spontaneous striped background generator
Slides from WDCNZ: Live Coding Asp.net MVC3 Slides from WDCNZ: Live Coding Asp.net MVC3
MVC 3, MVC 3, "Third Times a Charm" references
Custom Errors in ASP.Net MVC: It couldn't be simpler, right? Custom Errors in ASP.Net MVC: It couldn't be simpler, right?
Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold Anatomy of a Domain Hijacking, part 2: The Website Who Came In From The Cold
Anatomy of a Domain Hijacking, part 1 Anatomy of a Domain Hijacking, part 1
secretGeek.net domain has been stolen. The site may go down. secretGeek.net domain has been stolen. The site may go down.
Boring article: 'untrusted domain' issue with SQL Server. Boring article: 'untrusted domain' issue with SQL Server.
Coding While You Commute Coding While You Commute
Test Driven Dentistry Is A Good Thing Test Driven Dentistry Is A Good Thing
The 'less crashy' release of NimbleText The 'less crashy' release of NimbleText
Rethinking Toolbars in Visual Studio (or any IDE) Rethinking Toolbars in Visual Studio (or any IDE)
Where shall we have lunch? Where shall we have lunch?
Setting up email for your microIsv Setting up email for your microIsv
The NO Visual Studio movement: Compiling .net projects in Notepad++ The NO Visual Studio movement: Compiling .net projects in Notepad++
ZeroOne: the editor for programmers who think in binary ZeroOne: the editor for programmers who think in binary
Mercurial workflow for personal projects (with a .net bias) Mercurial workflow for personal projects (with a .net bias)
I see you're using vim. Let me fix that for you. I see you're using vim. Let me fix that for you.
The worst recruitment spam I've ever read The worst recruitment spam I've ever read
A thank you I forgot to say A thank you I forgot to say
My new product, NimbleText, is live My new product, NimbleText, is live
Grabbing the free songs of Jonathan Coulton (with Powershell) Grabbing the free songs of Jonathan Coulton (with Powershell)
Using NimbleSet to compare lists Using NimbleSet to compare lists
Wanted: Wiki Lists (dot org) Wanted: Wiki Lists (dot org)
DOS on Dope: The last MVC web framework you'll ever need DOS on Dope: The last MVC web framework you'll ever need
JSON Query Languages: 5 special purpose editors JSON Query Languages: 5 special purpose editors
What then, is b? What then, is b?
SQLike: A simple editor SQLike: A simple editor
Yet Another BizPlan Generator. Yet Another BizPlan Generator.
HOT GUIDS: A hot or not site for guids HOT GUIDS: A hot or not site for guids
How does life get better? One tiny hack at a time. How does life get better? One tiny hack at a time.
24 things to do, and 100 things *not* to do (yet) for building a MicroISV 24 things to do, and 100 things *not* to do (yet) for building a MicroISV
Venture capital won't kill Jeff Atwood, it will only make him Jeffer. Venture capital won't kill Jeff Atwood, it will only make him Jeffer.
A handy workflow image for newbie mercurial users A handy workflow image for newbie mercurial users
Fractal Feedback, a diversion into recreational programming Fractal Feedback, a diversion into recreational programming
Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe. Hump-Jumping: How the Education of Computer Science can be Saved, err, maybe.
Suggested User Experience Improvements for DiffMerge Suggested User Experience Improvements for DiffMerge
SQL Style Extensions for C# SQL Style Extensions for C#
The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's The Movie Hollywood (And My Wife) Doesn't Want You To See: Weekend at Jacko's
Sysi: the ultimate administrators toolkit Sysi: the ultimate administrators toolkit

Archives .: secretGeek :: Complete Archives
TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.3: true productivity boost

Next Action NextAction
Managing the top of your mind

NimbleText -- World's Simplest Code GeneratorNimbleText -- World's Simplest Code Generator, Text Manipulator, Data Extractor

25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Universal Troubleshooting checklist Universal Troubleshooting Checklist
Top 10 SecretGeek articles Top 10 SecretGeek articles
ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Realtime CSS Editor, in a browser RealTime Online CSS Editor
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker


[powered by Google] 


How to be depressed How to be depressed
You are not inadequate.



Recommended Reading


the little schemer


The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Rhys Parry
Joel Pobar
Thomas White
OJ Reeves
Eric Sink

Aggregated Links

proggit
dzone
hacker news
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
weekly link post

LinkedIn profile
LogEnvy - event logs made sexy
Computer, Unlocked. A rapid computer customization resource
PC Smart Buys - Computer Hardware in Australia
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy