Lambda Expressions and Currying in VB9

Introduction

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.

As a linguaphile, I’d like to introduce currying with Lambda Expressions and show you how it can quickly get out of hand. As a pragmastist and one who designs and codes software for a living, I am not sure I can tell you how to use currying. I say this to ensure proper expectations this article may have marginal practical value at best, but it should help round out your understanding of Lambda Expressions and help you in any games of technophile one-upsmanship.

Currying Basics

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:

Adder(5)(4)

More by Author

Must Read