On the closure of functional programming (Closure) __php

Source: Internet
Author: User
Tags closure

In learning the Golang and Scala languages, the concept of closure has not been very good, until today to see a blog, feel the Epiphany. The following is reproduced in full text:

Original address: http://www.cnblogs.com/Jifangliang/archive/2008/08/05/1260602.htm

Closures are often used in functional programming. What is a closure, and how does it come into being and what is the problem to be solved? Give a literal definition first: Closures are entities that are combined by functions and their associated referencing environments (i.e., closures = functions + reference environments). This is literally hard to understand, especially for programmers who have been programming using imperative languages. This article will be interpreted in conjunction with the instance code.
What is a function
The Earth people all know: The function is only a section of executable code, after compiling "solidify", each function in memory only an instance, get the function of entry point can execute function. In functional programming languages, functions are first-class citizens (first class value: Type I objects, we do not need to use function pointers, delegate operations functions as in imperative languages), functions can be used as arguments or return values of another function, and can be assigned to a variable. A function can nest a definition, that is, you can define another function within one function, and the structure of nested functions creates a closure problem. Such as:
>>> def exfunc (n):
sum = n
Def insfunc ():
return sum + 1
Return Insfunc

>>> MyFunc = Exfunc (10)
>>> MyFunc ()
11
>>> Myanotherfunc = Exfunc (20)
>>> Myanotherfunc ()
21st
>>> MyFunc ()
11
>>> Myanotherfunc ()
21st
>>>

In this program, the function insfunc is an inline function Exfunc function, and is the return value of the Exfunc function. We notice a problem: The local variable referenced in the inline function insfunc to the outer function Sum,ironpython will deal with this problem. Let's take a look at the results of this piece of code. When we call a function obtained by invoking the Exfunc function by different parameters (MyFunc (), Myanotherfunc ()), the resulting result is isolated, i.e. a new local variable sum is generated and saved each time the Exfunc function is invoked. In fact, the Exfunc function returns is the closure of the package.

Referencing the environment
According to the rules of the imperative language, the Exfunc function simply returns the address of the inline function Insfunc, and an error occurs when the Insfunc function is executed because the sum variable cannot be found within its scope. In functional languages, when the body of an inline function refers to a variable outside of the body, the reference environment and function body involved in the definition are packaged into a whole (closure) to return. It is now easy to understand the definition of a reference environment: a reference environment is a collection of all active constraints (the name of a variable and the connection between the object it represents) at a point in the execution of the program. There is no difference between the use of closures and normal function calls.

Because closures package functions and runtime reference environments as a new whole, it solves the problem of nesting in functional programming. As in the preceding code snippet, a new closure instance is returned each time the Exfunc function is called, which is isolated and contains the locale of the different reference environment at the time of the call. Unlike functions, closures can have multiple instances at run time, and different reference environments and the same combination of functions can produce different instances.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.