6

What are Closures/Lambda in PHP or JavaScript in layman terms? An Example would be great to aid my understanding. I am assumning Lambda and Closures are the same thing?

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
Imran
  • 11,350
  • 20
  • 68
  • 78

4 Answers4

4

A lambda is an anonymous function. A closure is a function that carries its scope with it. My examples here will be in Python, but they should give you an idea of the appropriate mechanisms.

print map(lambda x: x + 3, (1, 2, 3))

def makeadd(num):
  def add(val):
    return val + num
  return add

add3 = makeadd(3)
print add3(2)

A lambda is shown in the map() call, and add3() is a closure.

JavaScript:

js> function(x){ return x + 3 } // lambda
function (x) {
    return x + 3;
}
js> makeadd = function(num) { return function(val){ return val + num } }
function (num) {
    return function (val) {return val + num;};
}
js> add3 = makeadd(3) // closure
function (val) {
    return val + num;
}
js> add3(2)
5
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

SO already has the answers:

What is a lambda (function)?

How do JavaScript closures work?

Community
  • 1
  • 1
simshaun
  • 21,263
  • 1
  • 57
  • 73
2

Anonymous functions are functions that are declared without a name.

For example (using jQuery):

$.each(array, function(i,v){
    alert(v);
});

The function here is anonymous, it is created just for this $.each call.

A closure is a type of function (it can be used in an anonymous function, or it can be named), where the parameters passed into it are 'captured' and stay the same even out of scope.

A closure (in JavaScript):

function alertNum(a){
    return function(){
        alert(a);
    }
}

The closure returns an anonymous function, but it does not have to be an anonymous function itself.

Continuing on the closure example:

alertOne = alertNum(1);
alertTwo = alertNum(2);

alertOne and alertTwo are functions that will alert 1 and 2 respectively when called.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. Lambda functions allow the quick definition of throw-away functions that are not used elsewhere.

profitphp
  • 8,104
  • 2
  • 28
  • 21
  • And that's not a lambda. – dheerosaur Dec 21 '10 at 16:25
  • yes, it is. Try reading the manual. – profitphp Dec 21 '10 at 16:25
  • Anonymous functions are not necessarily closures. Closures /can/ have names if you want. Anonymous functions allow the quick definition of throw-away functions that are not used elsewhere. Lambdas are the same as closures. – gen_Eric Dec 21 '10 at 16:28
  • 4
    Anonymous functions and closures are not the same. – BoltClock Dec 21 '10 at 16:29
  • 1
    The guy said "in php". Not in a computer science course's typical definition. But thanks for the downvote anyways. – profitphp Dec 21 '10 at 16:29
  • He also said "in JavaScript". – gen_Eric Dec 21 '10 at 16:30
  • 1
    @profitphp: Don't worry, that's a very common misconception. Therefore, I don't think the downvote was warranted. Closure is not the function that is passed in, it is the context that is created and attached to an inner function, whether it's anonymous or not. In PHP, you specify what to add to the closure, in JS, all the local variables are available. In latest versions of Firebug, you can actually inspect that closure object, pretty cool! – Ruan Mendes Apr 22 '11 at 15:50
  • in PHP docs they make closure/lambda same... :( – confiq May 06 '13 at 09:57