闭包(计算机科学)是什么?
在The Swift programming langauge看到closures,但是不太理解是什么,有什么样的特性,能做什么Objective-C做不到的?编程初学者,求指教
回复内容:
要说 Closure 就得说 Closed Lambda Expression,一个 Closed Lambda Expression 就是没有自由变量的 Lambda Expression,如 λx. x,而 λx. yx 就不是 Closed。Closed Lambda Expression 最好的性质之一就是它的类型必然同构于某个逻辑重言式,如 λx. λy. xy 的类型就是「肯定前件」(α → β) → α → β。那么如何把某个 Open Lambda Expression 给 Enclose 住呢?答案就是把它引用的所有自由变量给保存到什么东西里面,这种保存了自由变量的 Lambda Expression 就是 Closure。在其同构的逻辑一面,则是在相继式左边加入前提。
语法上东西我就不说了。
计算机程序可以粗略的分成,代码+数据。初学者很容易就会将这两者对立起来,会认为代码就是代码,数据就是数据,两者是完全不同的。但实际上,两者可以统一起来的。将代码跟数据统一起来,是学习计算机编程的一道门槛。
可以参考,我以前的回答。回调函数是什么?
将数据保存起来,以后再使用,会觉得很自然。但将代码保持起来,以后再使用,很多人会觉得很别扭,难以理解。都是因为还没有过那道槛。
代码指令执行时候,会处于一定的环境,单纯将代码保存下来,还是不够的,需要将代码所处的环境也保存下来。闭包其实是,将代码跟代码所处于的环境做为一个整体来看待。周围的环境,表现为代码所使用的数据。在有些语言中,这个概念叫代码块(block),匿名函数(lambda)等等。
数据跟代码不再人为割裂开来,统一起来看待。闭包就会是很自然的概念。数据可以传递,从一个地方传递到另一个地方,并且以后再使用。闭包从某个角度来说,也是数据,当然也可以传递,从一个函数传递到另一个函数,也可以保持下来,以后再调用。因为将环境也保持下来了,以后调用的时候,就还原当时的情况,延迟执行,就很容易,很自然地实现了。而延迟执行有什么作用?就是另一个话题了。
function makeCounter() local count = 0 return function() count = count + 1 return count end
说的通俗一点,就是闭包允许一个函数访问声明该函数运行上下文中的变量,甚至可以访问不同运行上文中的变量。
我们用脚本语言来看一下:
<span class="kd">function</span> <span class="nx">funA</span><span class="p">(</span><span class="nx">callback</span><span class="p">){</span> <span class="nx">alert</span><span class="p">(</span><span class="nx">callback</span><span class="p">());</span> <span class="p">}</span> <span class="kd">function</span> <span class="nx">funB</span><span class="p">(){</span> <span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="s2">"Hello World"</span><span class="p">;</span> <span class="c1">// 函数funB的局部变量,函数funA的非局部变量</span> <span class="nx">funA</span><span class="err">(</span> <span class="kd">function</span><span class="err">()</span><span class="p">{</span> <span class="k">return</span> <span class="nx">str</span><span class="p">;</span> <span class="p">}</span> <span class="err">);</span> <span class="p">}</span>
这个比较容易懂 至少JavaScript闭包是指有权访问另一个函数作用域中的变量的函数,其他语言的不知道(逃 可以和对象的概念对比起来理解,简单地说:
对象是带方法的数据,而闭包是带数据的方法
后半句的数据特指外部数据 一块内存区域,存放着可执行代码和一些变量,指针 学过离散没?在某些集合上某些运算的结果始终在这个集合里,这就叫闭包性质…………虽然和这个闭包一点关系都没有=_=。
闭包在函数式语言当中是一个非常有力的工具,但是有点只可意会的感觉。
简单的来说你可以把闭包看成是绑定了某些变量的值的函数,由于函数式语言中函数是一级对象,所以,这样的性质就非常有用了,比如说可以通过在函数里面返回一些闭包来定义一些数据结构什么的。
举个栗子:
这是我学common lisp 的时候写的一个栈
<span class="p">(</span><span class="nb">defun</span> <span class="nv">stackpush</span> <span class="p">(</span><span class="nv">stack</span> <span class="nv">x</span><span class="p">)</span> <span class="p">(</span><span class="nb">cons</span> <span class="nf">#'</span><span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">x</span><span class="p">)</span> <span class="nf">#'</span><span class="p">(</span><span class="k">lambda</span> <span class="p">()</span> <span class="nv">stack</span><span class="p">)))</span> <span class="p">(</span><span class="nb">defun</span> <span class="nv">stackpop</span> <span class="p">(</span><span class="nv">stack</span><span class="err">)</span> <span class="p">(</span><span class="nb">apply</span> <span class="p">(</span><span class="nb">cdr</span> <span class="nv">stack</span><span class="p">)</span><span class="no">nil</span><span class="p">))</span> <span class="p">(</span><span class="nb">defun</span> <span class="nv">stacktop</span> <span class="p">(</span><span class="nv">stack</span><span class="p">)</span> <span class="p">(</span><span class="nb">apply</span> <span class="p">(</span><span class="nb">car</span> <span class="nv">stack</span><span class="p">)</span> <span class="no">nil</span><span class="p">))</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on July 31, Apple issued a press release yesterday (July 30), announcing the launch of a new open source Swift package (swift-homomorphic-encryption) for enabling homomorphic encryption in the Swift programming language. Note: Homomorphic Encryption (HE) refers to an encryption algorithm that satisfies the homomorphic operation properties of ciphertext. That is, after the data is homomorphically encrypted, specific calculations are performed on the ciphertext, and the obtained ciphertext calculation results are processed at the same time. The plaintext after state decryption is equivalent to directly performing the same calculation on the plaintext data, achieving the "invisibility" of the data. Homomorphic encryption technology can calculate encrypted data without leaking the underlying unencrypted data to the operation process.

Vue.js is a popular JavaScript framework for building user interfaces. The Swift language is a programming language used for iOS and macOS application development. In this article, I will explore how to integrate Vue.js with the Swift language for advanced iOS application development and testing. Before we get started, we need to make sure you have the following software and tools installed: Xcode: an integrated development environment for developing and compiling iOS applications. Node.js: used for

How to implement data import and export functions in Swift using MySQL Importing and exporting data is one of the common functions in many applications. This article will show how to use MySQL database to import and export data in Swift language, and provide code examples. To use the MySQL database, you first need to introduce the corresponding library files into the Swift project. You can do this by adding the following dependencies in the Package.swift file: dependencies:[

How to develop real-time chat function using Redis and Swift Introduction: Real-time chat function has become an indispensable part of modern social applications. When developing social applications, we often need to use real-time chat to provide interaction and information exchange between users. In order to meet the requirements of real-time and high availability, we can use Redis and Swift to develop such a function. Introduction to Redis: Redis is an open source in-memory data structure storage system, also known as a data structure server. It provides multiple

Integration of Vue.js and Swift language, suggestions and technical guidance for the development and testing of advanced iOS applications. Introduction The development and testing of mobile applications is a complex field that requires professional skills. The two main technology stacks are Vue.js on the front end and Swift language on the iOS platform. This article will introduce how to integrate Vue.js and Swift language to develop and test advanced iOS applications. Fundamentals and features of Vue.js Vue.js is a tool for building user interfaces

What programming languages are close to Go? In recent years, the Go language has gradually emerged in the field of software development and is favored by more and more developers. Although the Go language itself has the characteristics of simplicity, efficiency and strong concurrency, it sometimes encounters some limitations and shortcomings. Therefore, looking for a programming language that is close to the Go language has become a need. The following will introduce some programming languages close to the Go language and demonstrate their similarities through specific code examples. RustRust is a systems programming language with a focus on safety and concurrency

How to use Redis and Swift to develop recommendation system functions In today's Internet era, recommendation systems have become one of the core functions of many applications. Whether it is e-commerce platforms, social networks or music video websites, recommendation systems are widely used to provide personalized recommended content and help users discover and obtain content that may be of interest to them. To implement an efficient and accurate recommendation system, Redis and Swift are two powerful tools that can be combined to achieve a powerful recommendation function. Redis is a

PHP is a popular server-side scripting language that is widely used to develop web applications. PHP can be used with web servers in various ways, including Apache, Nginx, etc. In this article, we will focus on web servers in PHP, how they work and how to use web servers in PHP. A web server is a network application that receives HTTP requests and sends HTTP responses. Web servers can also handle static files (such as HTM
