为什么我们需要块,函数文字,在编程语言中的闭包? [英] Why do we need blocks, function literals, closures in programming languages?

查看:106
本文介绍了为什么我们需要块,函数文字,在编程语言中的闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C中编程,我知道一点Scala,Python和Javascript。



虽然我对Obj-C中的块感到舒服,但我想知道他们解决了什么具体问题,我无法用早期版本的语言。也是 - 块,闭包,函数文字,命名函数,匿名函数 - 一个和同样的事情?



如果你能回答一些代码示例,将是巨大的。

解决方案

首先,为了回答标题中的问题:


$ b $


为什么我们需要块,函数字面量和编程语言中的闭包? 1:我们不。 Brainfuck 是没有他们的图灵完成。




他们解决什么具体问题? / p>

他们不解决特定问题。如果你对Objective-C很舒服,你肯定听说过函数指针,对吧?现在每次在某处使用块时,该段代码可以使用函数指针转换为等效的代码片段。 闭包(块,lambda函数等)可以 用于创建它们的位置,不同于正常全局函数。考虑下面两段代码(关于Cocoa网络API发明的两个假想方法):

  void url_callback data,size_t length)
{
NSLog(@接收数据%p的大小为%zu,数据,长度);
}

[connection sendAsyncRequestWithHandlerFPtr:& url_callback];

  [connection sendAsyncRequestWithHandlerLambda:^(void * data,size_t length){
NSLog(@Received data%p of size%zu,data,length);
}];

当然,在第二个版本中,任何人读代码都是显而易见的。在第一个,你必须向上和向下滚动到函数的实现(如果有!),这样你可以了解当收到一些数据时会发生什么。


是块,闭包,函数文字,命名函数,匿名函数 - 一个又一样的东西?


不,他们不是。 (Quite。)



关闭匿名功能是数学和/或CS理论概念 - 它们描述子程式,其中

块是一个特定实现的闭包,因为由Apple在C(以及因此Objective-C)编程语言的扩展中实现。



命名函数表达式是一个结合了闭包和全局函数的优点的JavaScript特性。


I program in Objective-C, I know a bit of Scala, Python and Javascript.

While I'm comfortable with blocks in Obj-C, I would like to know what specific problem do they solve that I couldn't with the earlier versions of the language. Also - are blocks, closures, function literals, named functions, anonymous functions - one and the same thing?

If you can answer with some code examples that would be great.

解决方案

First of all, in order to answer the question in the title:

Why do we need blocks, function literals, closures in programming languages?

Answer #1: we don't. Brainfuck is Turing-complete without them.

Answer #2: because we're lazy and closures are convenient and easy to use.

What specific problem do they solve?

They solve no specific problem. If you are comfortable with Objective-C, you have surely heard of function pointers, right? Now every time a block is used somewhere, that piece of code could be transformed to an equivalent snippet using a function pointer. The great achievement that closures bring to the programmer is readability. Closures (blocks, lambda functions, etc.) can be (and are) used where they are created, unlike "normal" global functions. Consider the following two pieces of code (two imaginary methods invented with regards to the Cocoa networking APIs):

void url_callback(void *data, size_t length)
{
    NSLog(@"Received data %p of size %zu", data, length);
}

[connection sendAsyncRequestWithHandlerFPtr:&url_callback];

versus

[connection sendAsyncRequestWithHandlerLambda:^(void *data, size_t length) {
    NSLog(@"Received data %p of size %zu", data, length);
}];

Of course, in the second one it is obvious to whoever reads the code what it does. In the first one, you have to scroll up and down to get to the implementation of the function (if any!) just so you can understand what happens when some data is received.

Are blocks, closures, function literals, named functions, anonymous functions - one and the same thing?

No, they aren't. (Quite.)

Closures and anonymous functions are a mathematical and/or CS theory concept - they descibe subroutines which are first-class values.

Blocks are a particular implementation of closures, as realized by Apple in an extension to the C (and consequentially to the Objective-C) programming language.

Named function expressions are a JavaScript feature that combine the advantages of closures and global functions.

这篇关于为什么我们需要块,函数文字,在编程语言中的闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆