Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Closures/Lambda in PHP or Javascript in layman terms? [duplicate]

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?

like image 543
Imran Avatar asked Dec 21 '10 16:12

Imran


People also ask

What are lambdas and closures in Javascript?

A lambda is essentially a function that is defined inline rather than the standard method of declaring functions. Lambdas can frequently be passed around as objects. A closure is a function that encloses its surrounding state by referencing fields external to its body.

Is closure and lambda same?

A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.

What is the closure in PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

Is a lambda function a closure?

Lambda functions may be implemented as closures, but they are not closures themselves. This really depends on the context in which you use your application and the environment. When you are creating a lambda function that uses non-local variables, it must be implemented as a closure.


3 Answers

SO already has the answers:

What is a lambda (function)?

How do JavaScript closures work?

like image 53
simshaun Avatar answered Nov 15 '22 00:11

simshaun


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
like image 26
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 00:11

Ignacio Vazquez-Abrams


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.

like image 27
Rocket Hazmat Avatar answered Nov 14 '22 22:11

Rocket Hazmat