|Written by Ian Elliot|
|Tuesday, 14 January 2014|
Page 1 of 2
Lambda expressions sound advanced and impressive and just about every language has them or is adding them. Put simply lambda expressions are something of a buzz word, so much that it's difficult to know what they really are all about.
First what is a Lambda expression?
Lambda expressions were invented by Church and Kleene, two great computer scientists, back in the 1930s. In practice this is fairly irrelevant in the sense that when most programmers use the term Lambda expression they mean a function of a number of parameters that returns a single result and can be passed to other functions.
The parameters and single result clearly aren’t a problem but what about passing a function to other functions?
Functions As Objects
What this means in practice is that a variable can store data or code with the only real difference being that code can be executed using, for example the invocation operator ().
For example, the definition:
Contrast this to what happens in many other languages where functions are something completely different from an object and are usually thought of as being constituent parts of an object aka methods. This approach is what makes it necessary to introduce lambda expressions or something similar in these other languages. Put simply we need lambda expressions to provide us with functions that have a life for their own outside of objects and so that they can be passed around like objects.
The alternative way of defining a function as a function expression makes it much clearer that it is more like an object:
You really can think of test as being a variable that just happens to hold a reference to a function object.
Of course a function object is special among objects in that it has a chunk of code, a method if you like, that you can invoke using the invocation operator on the object.
That is test is a reference to an object and
calls the function you defined. It really can help to think of this as the object's default method. If you don't see what this idea is about consider adding a method to the function object:
You can now call myMethod using the usual:
and this makes
look even more like a call to a default method.
As a function is just an object you can do anything you can do to an object to a function.
For example you can assign one function to another using standard syntax:
and this works perfectly.
Notice that while test and test2 are both references to the same function object and hence the same code,
In short variables that happen to reference a function object behave like perfectly standard reference variables.
In particular if you redefine test a new function object and block of code is created on the heap and test is set to point to it. The second variable, test2 still points to the original block of code and hence the old version of the function.
For example following:
test is a function that prints Hello2 but test2 is still the original function that prints Hello1, as you can confirm by using:
You can use this function reference mechanism to create something that looks like C#’s multi-target delegate function.
To call multiple functions with a single call you could use something like:
and to call all three functions one after another you would use:
This looks like just calling the three functions one after another but of course func1, func2 and func3 are variables and hence you can dynamically change what functions are called.
You can pass parameters if you want to and pass them on to the sequence of called functions. This is all particularly useful if you want to implement a “callback” that invokes multiple functions. If you want to allow users to add callback functions all you need to do is setup a stack or an array to store them as they are added.
Given you can pass an object, i.e. as a parameter, into a function it now seems entirely reasonable that you can pass a variable that just happens to reference a function object into another function.
For example, the Array object has a method that will sort the array into order:
What is perhaps slightly less well known is that the sort method can also take an optional function which it will use to compare the values of the array. This function is defined as:
and it has to return a negative value if a<b, 0 if a==b and a positive value if a>b.
sorts the list of strings into order based on their length alone.
Other languages that don't have first class functions have to resort to delegate types - object wrappers for functions - or lambda expressions.
|Last Updated ( Tuesday, 28 October 2014 )|