I am a linguaphile. That is, I love cool language features such as operator overloading, generics, Lambda Expressions, ternary operators, and anything and almost everything about OOPy languages. For that reason, C++ has probably always been my favorite language, but I seldom write C++ anymore. One reason is that I am seldom asked, but another reason is that many more companies have been using Delphi, VB, Java, and C#. Perhaps this is so due to the unforgiving nature of C++ or the perceived productivity of RAD tools. I'm not sure.
However, as a linguaphile, I am thrilled and concerned (for traditional VB programmers) because esoterrorism has found VB9. Esoterrorism is code that is so esoteric it is almost impossible to comprehend and terrorizes code maintainers. The kind of code referred to is Lambda Expressions that use currying. Lambda Expressions are short, inline substitutions for delegates that exist to support LINQ. Currying is chaining Lambda Expressions together where each expression solves a small part of the problem.
If you think generics are/were hard, wait until you tackle Lambda Expressions (and currying). Because this article is on currying, it is assumed you have some understanding of Lambda Expressions. As a refresher, simply think of Lambda Expressions as short, terse inline functions that use the word Function, followed by parentheses with parameters, and a statement that represents the function body. Prune out everything else about a traditional function and you have a Lambda Expression. Here is an example:
Function(x, y) x + y
The preceding is basically a function that accepts two arguments and returns the sum of the two arguments, x and y. Short and sweet. Lambda Expressions such as the preceding are relatively easy once you get used to the short syntax. However, if you begin adding information—that is, you try to do something useful—these expressions get harder to read.
Currying is where you daisy chain expressions together. Each expression accepts one argument and returns the next expression in the chain until the last expression, which returns the result. Think of currying as a Turing Machine a là Lambda Expressions. (Look up Alan Turing for more on Turing machines; wikipedia has some great information.) By currying the above expression, you get:
Function(x) Function(y) x + y
To assign the curried-lambda to a variable, you can use anonymous types or you can match the type with the generic delegate Func. The following statement assigns the Lambda Expression above to a variable named adder.
Dim adder As Func(Of Integer, Func(Of Integer, Integer)) = _ Function(x) Function(y) x + y
As you can see, the syntax begins to get a bit challenging very quickly. In the example above, you are initializing the generic delegate Func with a generic delegate. To invoke add, you use the function operator that reminds me of Hungarian notation arithmetic: