What is the difference between a “close” and a “block”?

I found that many people use the words "closure" and block interchangeability. Most of these people cannot explain what they are talking about.

Some Java programmers (even from very expensive consultants) talk about anonymous inner classes as “blocks” and “closures,” but I know this is not true. (You cannot pass mutable variables from the scope of the method in which they are defined ...)

I'm looking for:

  • exact, computer science block definition
  • exact, computer science circuit definition
  • and an explanation of the difference between the two.

I would really like to see links, articles or books on these links .

+42
computer-science programming-languages theory
Nov 28 '09 at 12:18
source share
6 answers

While a block is just a piece of code that can be compiled by statements and declarations, but nothing else, closure is not a real first-class object, a variable that has its own block as a value.

The main difference is that the block simply groups the commands together (for example, the body of the while statement), and the closure is a variable containing some code that can be executed.

If you have a closure, you can pass it as a parameter to functions, generate and expand it, and basically name it!

Closure c = { println 'Hello!' } /* now you have an object that contains code */ c.call() 

Of course, closures are more powerful, they are variables and can be used to determine the user behavior of objects (while usually you had to use interfaces or other OOP approaches in programming).

You can think of closure as a function that contains what this function does inside itself.

Blocks are useful because they allow you to define areas of variables. Usually, when you define a variable within the scope, you can redefine the external definitions without any problems, and new definitions will only exist during the execution of the block.

 for (int i = 0; i < 10; ++i) { int t = i*2; printf("%d\r\n", t); } 

t defined inside the block (body of the for statement) and will only last inside this block.

+27
Nov 28 '09 at 12:25
source share

A block is something syntactical - a logical unit of statements (more related to scope than to closure).

 if (Condition) { // Block here } else { // Another block } 

Closing is associated with anonymous functions or classes - an anonymous (functional) object, part of the code tied to the environment (with its variables).

 def foo() { var x = 0 return () => { x += 1; return x } } 

Here foo returns a closure! The local variable x maintained through closure even after the completion of foo and can be increased by calling the returned anonymous function.

 val counter = foo() print counter() // Returns 2 print counter() // Return 3 

Note that only Ruby in which the block and closure are handled similarly, since the Ruby block causes a closure:

 (1..10).each do |x| px end 

There, each method is passed a closure function (taking the x parameter), which is called a block in Ruby.

+16
Nov 28 '09 at 12:29
source share

There is a lot of confusion because there are terms with several definitions and several different things that come together simply because they usually occur together.

Firstly, we have a “block”. This is just a lexical piece of code that makes one - the body of the loop, for example. If the language actually has a block area, then variables can be defined that exist only within this code fragment.

Secondly, we call the code as a value type. In functional languages, these are function values, sometimes called "funs", "anonymous functions" (because the function is found in the value, and not in the name to which it is assigned, you do not need a name to call them), or "lambda" (from the operator used to create them in the calculus of the Lambda Church). They can be called “closures,” but they are not automatically true closures; in order to qualify, they must encapsulate (“close”) the lexical area surrounding their creation, that is, variables defined outside the scope of the function itself, but within its definition are still available when the function is called, even if the calling point is after how the reference variable would otherwise go out of scope and be reprocessed.

However, you may have callable code values ​​that are not entire functions. Smalltalk calls these "block locks," and Ruby calls them "procs." But most rubists simply call them "blocks" because they are an updated version of what is created with the { ... } or do ... end syntax. What sets them apart from lambdas (or "closing functions") is that they do not introduce a new level of routine. If the code in the block closure cube calls return , it returns from the external function / method the closure of the block inside, and not just the block itself.

This behavior is critical to maintaining what RD Tenent called the "correspondence principle", which states that you should be able to replace any code with a built-in function that contains this code in the body and call it right away. For example, in Javascript you can replace this:

  x = 2; 

with this:

  (function(){x = 2;})(); 

My example is not very interesting, but the ability to perform such a conversion, without affecting the behavior of the program, plays a key role in functional refactoring. The problem is that once you have built in the return , the principle no longer holds.

That's why Ruby has both procs and lambdas - a constant source of confusion for beginners. Both procs and lambdas are objects of the Proc class, but they behave differently, as described above: a return simply returns from the body of the lambda, but returns from the method surrounding proc. (Also, not related to the demarcation that I draw here, lambdas checks arity and complains if called with the wrong number of arguments. You can indicate what type you have by calling .lambda? On the object.)

Javascript currently only has function closures, although there is a suggestion in the table to introduce block locks for the language.

+4
May 9 '14 at 3:05
source share

A loud bearded man speaks of closure and blocks:

http://martinfowler.com/bliki/Closure.html

At some point, he says that a closure is a block that can be passed as an argument to a method.

+2
Nov 29 '09 at 11:36
source share

The terms that you use are most often used these days in Ruby, although the constructs previously appeared in Algol, Smalltalk, and Scheme. I would call the Ruby standard if it were.

I'm not sure I can answer your exact question, but I can illustrate. My apologies if you already know this ...

 def f &x yield x end def g y = "block" t = f { p "I'm a #{y}" } y = "closure" t end t = g t.call 

BUT...

 $ ruby exam.rb "I'm a block" "I'm a closure" $ 

Thus, a block is an anonymous functional code sequence attached to a method call. It has been used throughout the Ruby API. When you easily create an anonymous function, it turns out that they are useful for all kinds of things.

But note that after f returns, then g returns, we hold it in the block, returning it from f (as x ), and then from g (as t ). Now we call the block a second time. Again, note that g() back. But the block refers to a local variable in the function instance (and scope) that no longer exists ?! And he gets a new y value ?!

Thus, the closure of a functionally similar object that closes in its lexical area. They are quite difficult to implement, since they destroy the do-it-with-a stack model, which is so useful for local variables in instances of function calls.




1. Ruby has different tastes for objects such as closures; this is just one of them.

+1
Nov 29 '09 at 1:46
source share

5

This is an integer.

Int workDaysInAWeek = 5

This is an integer variable, and it can be set to another integer. (If circumstances do not allow you to change this value, it can be called a constant.)

While the numbers, blocks, and locks mentioned above are algorithms. The difference between blocks and closures, respectively, is also equivalent to the above.

0
Jan 14 '16 at
source share



All Articles