views:

75

answers:

4

In thinking about blocks, I've always wondered why the

thingers.each { |thing|

example is actually interesting (since there's another, built-in way to do it). In most modern languages there's a way to iterate through a collection and apply some inline code to it. But then I thought that maybe the for (Thing thing : things) { syntax is really a type of block, even in languages like Java-sans-Groovy where there are none.

So the question is: Is a for-each loop a type of block, albeit with a fixed syntax?

Edit: This question is confusing but yet got some attention so I can't delete it. Anyway... by Blocks I mean "closures" and not just code blocks. As far as why I tagged this as language agnostic is that I'm not interested in how a for-each is implemented under-the-hood. I'm more interested in whether, from a programmer's perspective, a for-each could be considered a freebie closure in languages that doesn't have them, like Java.

A: 

Please elaborate. My definition of a "block" is a section of code that's grouped together. In this case there's a lot of stuff in Java can be a "block"

If you are talking about ruby-style blocks, which are really closures, then Java doesn't support them (not in any sane fashion anyways). foreach is a control structure, and behaves differently from closures.

On your updated question: It's easy to use closures as control structures, but the other way around is harder, if not impossible. A closure is a first class function, which allows you to do some pretty cool stuff with it - not just iterating through elements.

quantumSoup
Hmmm... that was my definition of block too, but now I mean blocks as in "blocks coming soon to Objetive-C (closures)" or "Ruby blocks are cool" (closures). I think I mean closures.
Yar
+3  A: 

This is most definitely not language agnostic (as it is tagged).

For example, in Ruby, you are passing a closure or block to the .each method. The semantics are defined by the language.

In C#, foreach compiles to a call to IEnumerable.GetEnumerator and the use of the IEnumerator.MoveNext method. It just depends on the language.

EDIT:

Edit: This question is confusing but yet got some attention so I can't delete it. Anyway... by Blocks I mean "closures" and not just code blocks. As far as why I tagged this as language agnostic is that I'm not interested in how a for-each is implemented under-the-hood. I'm more interested in whether, from a programmer's perspective, a for-each could be considered a freebie closure in languages that doesn't have them, like Java.

No, you are thinking only of one use case for a closure. You can't pass a control structure to a method as you can a closure in some languages. A control structure is not a first class first class data type like a closure is in some languages. In the case of "run this code on each object in a collection" yes, they are semantically similar. However, that it only one example of what you can do with a closure.

You say that you w

Ed Swangren
Hi Ed, thanks for your answer. You have 13K so you can change the tags or the question as you see fit. Had you not answered it, and gotten upvoted, I could've deleted it, which is no longer possible. I am not really asking how the thing gets implemented. I'm asking if, from a programmer's perspective, control structures and closures are ever essentially identical.
Yar
I like this, "You can't pass a control structure to a method as you can a closure in some languages." I didn't think of that: that's part of the joy of closures is that they can be assigned to variables and passed about.
Yar
+1  A: 

As Swangren says, this question is not language agnostic as tagged. Not unless there's some general "IT theory" definition of a "block" that I'm not aware of.

In Java, a "block" is a series of statements enclosed in { }. So a FOR statement normally operates on a block. The purpose of a block is to delimit what is included within certain language constructs, like IF statements and loops.

I believe C and C++ have similar definitions of a "block".

Most languages have a similar concept.

Not sure if this helps.

Jay
A: 
foreach ($foo as $bar) {
    $baz = 'Hello, World!';
}

echo $baz; // 'Hello, World!', "leaked" from the "closure"

Nope, sorry, foreach loops are not lambdas, closures or functions in languages where they aren't explicitly such.

deceze
PHP is a mess. That does not happen in C++ or Java.
quantumSoup
@quantum It does not happen in languages that have *block scopes*. That doesn't make languages that have function or object scopes messy, it just makes them different. *(That's not to say that PHP isn't a mess, just not for this reason. ;))*
deceze