1

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.

Naz Mir
  • 1,028
  • 1
  • 9
  • 19

1 Answers1

3

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.

Community
  • 1
  • 1
  • Er, are we trying to say that closure == anonymous function here? If so, that's totally incorrect. closure == closure. anonymous function == anonymous function. Completely different animals. – Beetroot-Beetroot Jun 12 '13 at 11:59
  • @Beetroot-Beetroot Well, theoretically they are different, I should have perhaps emphasized the difference better. I think I'll leave the explanation to Wikipedia :P –  Jun 12 '13 at 12:16
  • If someone would like to understand closures and anonymous functions have a look at this question - http://stackoverflow.com/questions/12930272/javascript-closures-vs-anonymous-functions?rq=1 – Naz Mir Jun 12 '13 at 12:53
  • @H2CO3, no they are not *theoretically* different. They are different. – Beetroot-Beetroot Jun 12 '13 at 16:54
  • @Beetroot-Beetroot I know, and I've edited the answer. No need to be nitpicky about that. –  Jun 12 '13 at 17:18
  • H2CO3, personally I would run with something like, "Anonymous functions and closures are a mathematical and/or CS theory concepts, concerned with the nature of subroutines and the scope of their variables", with particular emphasis on "concepts" in the plural. – Beetroot-Beetroot Jun 12 '13 at 17:41
  • 1
    "Now every time a block is used somewhere, that piece of code could be transformed to an equivalent snippet using a function pointer." Not true. A block is equivalent to a function pointer *plus another piece of data*. The example you gave is not a closure, since it does not close over any variables from the outside. Closures have state, that's why it's necessary to include another piece of data. – newacct Jun 12 '13 at 23:40
  • @Beetroot-Beetroot: "Anonymous function" is a syntax. "Closure" is a concept that describes functions that capture variables from the surrounding scope. In many languages, function objects, both anonymous and named, can capture variables from the outside scope; then they are closures. – newacct Jun 12 '13 at 23:42
  • @newacct, (a) don't tell me, tell C3PO, (b) anonymous functions are only superficially about syntax; without the additional feature of being able to pass functions as first class objects (or attach them as event handlers), anonymous functions would be uncallable. – Beetroot-Beetroot Jun 12 '13 at 23:57
  • @Beetroot-Beetroot: I do not disagree that you need to pass functions in order to be useful. But what I am saying that, when you have anonymous functions and named function in a language like Python, anything use of an anonymous function can be replaced with an equivalent local named function. So the only difference is the syntax. – newacct Jun 13 '13 at 00:05