Earlier I posted about some of the things I thought were cool about Python. In that post I gave myself the homework of researching similar capabilities in C#. Here were the two things I assigned myself to do:
- See if C# has lambda function support
- Python allows you to pass functions around just like any old object and let you get a reference to a function by its name at runtime. Write some code to examine this functionality in C#.
When I started to look into the first item I found that the C# 2.0 spec has support for anonymous functions. Let's dig into this a little bit. Here's a sample line from Mark Pilgrim's book on Python, Dive Into Python:
processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
And the description of what that does:
processFunc is now a function, but which function it is depends on the value of the collapse variable. If collapse is true, processFunc(string) will collapse whitespace; otherwise, processFunc(string) will return its argument unchanged.
How would we do this in C#? Since C# 2.0 comes out in a few short months (I think), I'll just worry about that since I'm sure this would be hard if not impossible in 1.0. Well I think we'd do it like so:
delegate string ProcessFunc(string s);
ProcessFunc processFunc = collapse ? delegate(string s) { ***func def*** } : delegate(string s) { return s; };
Some thoughts about the differences: Python lambda functions can only contain one expression while anonymous functions in C# have no such limitation. C# requires defining/typing the delegate beforehand which is something that Python's dynamic typing allows you to avoid. I left out the function code in C# for removing all the whitespace chars in a string. I don't think the built in string library has a function for doing this and writing that code wasn't the point of this exercise.
The second item points to a fundamental difference between the two languages. Functions aren't first class citizens in C#. C# uses delegates and reflection to bypass this shortcoming. After a bit of digging and research I found this article on MethodBase.Invoke(). Although I said I wanted to write sample C# code, I'm too lazy to do it at this point and the article shows it anyway. Here's the python snippet to do the same thing in the "Main" method of the MethodBase.Inoke() article assuming that the classes have already been defined:
print "First Method - " + str(getattr(A(), "method")())
print "\nSecond Method - " + str(getattr(B(), "method")())
Needless to say, the process was much easier in Python. This whole bit got me to thinking about functions and the definition of "first class". Actually, I wonder how first class they are in Python. One thing mentioned in this wiki of first class functions is the ability to write and modify the functions themselves during program execution. At this point I have no clue how to do that in either language and I'm not even sure that you can. I assume that this kind of functionality is offered in Lisp but I'd have to research that too. I'm going to shelf this issue for now, but I'll have to make sure I revisit it again in the coming months.
Comments