18
may
2008

Lambda Functions - Does your language support it?

What are Lambda functions? Basically lambdas are functions that can be treated as anonymous or as objects. It depends on the language that is being used. They are very useful and at the core of the Functional Programming paradigm. So what languages can you create lambas in? Here is a few;

JavaScript

var foo = function(x, y){
    return x + y;
}
alert(foo(2, 4));

PHP

<?php

$foo = create_function('$x,$y', 'return $x + $y;');
echo $foo(2,4);

Python

foo = lambda x, y: x + y
print foo(2,4)

These are of course very simple examples and don’t show the full potential. I’ve been a fan of lambdas for a while now and wanted to see what languages supported them. If you know of others you want me to add, post them in the comments. I know many other languages can, I just posted up the languages I'm familiar with.

I think out of all the examples I’ve looked at so far I like the style of them most in either JavaScript of Python. The main with Python's implementation is that it doesn't support multiple line lambda's, however you can just as easily reference functions themselves and pass them around as objects so its not a big loss.

Short url - Related tags: javascript, lambda, language-features, php, python

blog comments powered by Disqus