I thought this was the one that worked?

On Aug 1, 2006, at 6:39 AM, Chad P. wrote:

their
really IS a question for a programming koan, after all. If so, I’m
www.debian.org/

More examples to chew on:

class A
def make_closurish_thing
lambda { puts “Hello” }
end
end

a = A.new
c = a.make_closurish_thing
c.call
def a.puts(*args)
super(“Access of enclosing scope”)
super
end
c.call

class B
def close_me
x = 1
lambda{|x| eval(x) }
end
end

b = B.new
c = b.close_me

c.call(“puts ‘Am I a closure?’”)
c.call(“puts ‘How about now?’; puts x”)

[email protected] wrote in message
news:[email protected]

I guess you could debate whether that block is a closure, since it
where “a” is not visible. Thus, the block was executed outside of the
If you don’t capture it, you can yield to it – but then you’re
yielding to the block, not calling a Proc object derived from the
block.

...and this would be a problem if the definition of a closure is the

calling of a Proc object derived from a block. Alas, that is not the
definition of a closure…
When we yield to the block, we are doing so in our method, which is
not the same scope as the scope where the block was created. Yet, the
block we’re yielding to still has access to that other scope. How?
Because
it is a closure…

It comes down to the fact that blocks are syntactic constructs, while
Procs are first-class objects.

I'm not sure why this has anything to do with anything...

Hi –

On Tue, 1 Aug 2006, Just Another Victim of the Ambient M. wrote:

a = 1
where its context is, that’s irrelevant. What matters is that the block
there’s no issue (hair-splitting or otherwise) about its being a
not the same scope as the scope where the block was created. Yet, the
block we’re yielding to still has access to that other scope. How? Because
it is a closure…

You could say, though, that when you do this:

def x
a = 1
puts a
end

def y
x
end

you’re calling x from your method y, and x has access to local
variables not defined in y, so x must be a closure.

It comes down to the fact that blocks are syntactic constructs, while
Procs are first-class objects.

I’m not sure why this has anything to do with anything…

Consider an if statement:

y = 1
if x
y
end

The code in the middle is flat, as regards scope. Furthermore, you
can’t do anything with it; you can’t send that chunk of code
somewhere, open it up, and find y. It’s just an expression or
statement among other expressions or statements.

A code block has one foot in that camp. When you see:

a = 10
[1,2,3].each {|x| puts x * a }

you’re seeing, in a sense, a flat scope – that is, the variable a
just gets used, as it might if it were in an if statement.

That’s only part of the story, though. The other parts are, first,
that variables created inside the block are not in scope when the
block exits (so its scope is definitely not flat); and, second, the
fact that it’s so easy to convert a block to a Proc that blocks feel
like first-class objects, even though they aren’t.

David

“Chad P.” [email protected] wrote in message
news:[email protected]

  1. If a tree falls in the forest, and there’s no one around to hear
    it, does it still make a sound?

  2. If a lambda has the ability to access its context, but there isn’t
    any context to access, is it still a closure?

How about this:
  1. If a lambda has the ability to access its context and nothing’s
    around to
    use it, does it still make a closure?

    I think 3 is a better characterization than 2. Here’s some code to
    demonstrate:

variable = 2 # this is the context

[1, 2, 3].each do |i|
# it sounds as if you think this is the “context”
puts i + variable

# either way, we're using the context so
# we agree that this is a closure

end

[4, 5, 6].each do |i|
# a context exists here since we
# have access to “variable”
puts i

# but we don't use it.
# are we still a closure?

end

I’m beginning to think the question of whether it’s actually a closure
really IS a question for a programming koan, after all. If so, I’m glad
we’ve at least narrowed the discussion down to this point at last.

I agree that we have finally narrowed down the discussion.  This 

might
have happened sooner if I had actually followed the thread…

It’d be pretty neat to be responsible for the creation of a new
programming koan, anyway. I’m going to go add this to my sig rotation
right now.

Ah, it's over-rated...
Is a loop a loop if it only loops once?
Does a parameter exist if we don't actually access it?

Not to be insulting but there's a part of me that thinks that you 

came
up with this distinction to hide the (embarrassing?) fact that you
didn’t
know that all blocks were closures…

[email protected] wrote in message
news:[email protected]

…and this would be a problem if the definition of a closure is the
def x
a = 1
puts a
end

def y
x
end

you’re calling x from your method y, and x has access to local
variables not defined in y, so x must be a closure.

Now you're narrowly construing my statement to a ludicrous degree.

There is context to what I said, you know?
In your example, method “x” has access to variable “a” because that
variable is in its own context, not its enclosing context. This is a
better example:

a = 1
def x
puts a
end

def y
x
end

Then x _will_ be a closure but, then again, we already knew this...

end

you’re seeing, in a sense, a flat scope – that is, the variable a
just gets used, as it might if it were in an if statement.

That's the whole point of closures.  It looks like variable "a" 

“just
gets used,” even though the process of it being used is quite complex.
If I understand what you mean by “flat,” then it isn’t flat. It
only
looks flat.
Is this an example of “flat” scope?

a = “foo”
b = a + “bar”
puts b

It may look "flat" but scopes have come and gone.
Your example took a block and sent it to the method "each," which 

has
it’s own scope, where the block would have ordinarily had no hope of
accessing the variable “a” except that it was a closure…

That’s only part of the story, though. The other parts are, first,
that variables created inside the block are not in scope when the
block exits (so its scope is definitely not flat); and, second, the
fact that it’s so easy to convert a block to a Proc that blocks feel
like first-class objects, even though they aren’t.

I'm not certain that the other parts of "the story" are relevant.

Incidentally, having variables created inside the block be in scope 

when
the block exits is planned for Ruby2. Indeed, if memory serves me, it
is
Matz’s “most regretting” design decision that it doesn’t already do
this…
So, I guess it will then be “flat.”
Personally, I look forward to this feature…

[email protected] wrote in message
news:[email protected]

It may look “flat” but scopes have come and gone.

I only see one local scope there. Where do you see others?

The scope in the method "+" came and went.  The scope in the method

“puts” came and went as well…

Your example took a block and sent it to the method “each,” which has
it’s own scope, where the block would have ordinarily had no hope of
accessing the variable “a” except that it was a closure…

The part I’m not convinced of is “sent”. It provided a code block,
but it didn’t send it.

It got "sent" as much as anything gets sent around here (in Ruby)...
I already posted this somewhere else on this thread but I guess I'll

post it, again, here. The “each” method in class “Array” might have
been
implemented something like this:

class Array
def each
i = 0
while i < self.size
# this is where the block is executed
# far away from the scope where “a” is defined…
# the block was “sent” here…
yield self[i]

        i += 1
    end
end

end

this is the sample code you provided…

a = 10

the block is defined here but “sent” to the “each” method…

[1, 2, 3].each {|x| puts x * a }

Of course, I doubt anyone would actually implement "Array#each" like

that. They might do something like this:

class Range
def each
i = first
while i <= last
yield i

        i += 1
    end
end

end

class Array
def each
(0…(size - 1)).each { |i| yield self[i] }
end
end

again, this is the sample code you provided…

a = 10
[1, 2, 3].each {|x| puts x * a }

Incidentally, having variables created inside the block be in scope
when
the block exits is planned for Ruby2. Indeed, if memory serves me, it is
Matz’s “most regretting” design decision that it doesn’t already do
this…
So, I guess it will then be “flat.”
Personally, I look forward to this feature…

I’m among the few that like it the way it is, but I think we’re on the
losing end of this decision-making process for 2.0 :slight_smile:

There was a decision-making process?

Hi –

On Tue, 1 Aug 2006, Just Another Victim of the Ambient M. wrote:

def y
x
end

you’re calling x from your method y, and x has access to local
variables not defined in y, so x must be a closure.

Now you’re narrowly construing my statement to a ludicrous degree.

I’m just discussing the topic at hand, I think. It’s really not about
this or that statement; I just like to examine things like this.

def y
x
end

Then x will be a closure but, then again, we already knew this…

But the a inside x and the a outside x are unrelated. Method
definition blocks (with def) aren’t closures.

end

you’re seeing, in a sense, a flat scope – that is, the variable a
just gets used, as it might if it were in an if statement.

That’s the whole point of closures. It looks like variable “a” “just
gets used,” even though the process of it being used is quite complex.
If I understand what you mean by “flat,” then it isn’t flat. It only
looks flat.

Right – see the “other parts of the story”, below.

Is this an example of “flat” scope?

Yes.

a = “foo”
b = a + “bar”
puts b

It may look “flat” but scopes have come and gone.

I only see one local scope there. Where do you see others?

Your example took a block and sent it to the method “each,” which has
it’s own scope, where the block would have ordinarily had no hope of
accessing the variable “a” except that it was a closure…

The part I’m not convinced of is “sent”. It provided a code block,
but it didn’t send it.

That’s only part of the story, though. The other parts are, first,
that variables created inside the block are not in scope when the
block exits (so its scope is definitely not flat); and, second, the
fact that it’s so easy to convert a block to a Proc that blocks feel
like first-class objects, even though they aren’t.

I’m not certain that the other parts of “the story” are relevant.

See above; it’s just different ways of looking at a block, or ways of
looking at different characteristics of a block.

Incidentally, having variables created inside the block be in scope when
the block exits is planned for Ruby2. Indeed, if memory serves me, it is
Matz’s “most regretting” design decision that it doesn’t already do this…
So, I guess it will then be “flat.”
Personally, I look forward to this feature…

I’m among the few that like it the way it is, but I think we’re on the
losing end of this decision-making process for 2.0 :slight_smile:

David

Chad P. [email protected] writes:

returned from foo() and assigned to $baz.
I’m joining the party late, but try this example:

class Example
def initialize
save_for_later do
puts “baz”
end
bar = 1
nil
end

def save_for_later(&block)
  @saved = block
end

def use_saved
 eval("bar += 1", @saved.binding)
end

end

foo = Example.new
foo.use_saved # => 2
foo.use_saved # => 3

From the Wikipedia definition:

[A] closure is a function that refers to free variables in its
lexical context.

Even if the Ruby block itself contains no reference to free variables,
the stored lexical context associated with the block does so
implicitly. In the case of:

array.each do
puts “each!”
end

the block may be get executed immediately w/o referencing free
variables, but the ruby implementation has no way of knowing that.
Thus, every block carries around a lexical context which references
all free variables in that context. Thus, every block is a closure.

Yes, it is:

class B
def initialize(a)
@a = a
end

def each
yield # First block invocation

@a.z = 77   # Changing the scope in
class << @a # which the block was created
  def puts(*args)
    $stderr.puts @z, *args.map { |_a| _a.upcase }
  end
end

yield # Second block invocation

end
end

class A
attr_accessor :z
def f
array = B.new(self)

HERE’S YOUR FRAGMENT

array.each do ||
  puts "Hello world!"
end

YOUR FRAGMENT ENDS HERE

end
end

A.new.f

Procuces:

Hello world!
77
HELLO WORLD!

Ruby does not assume what a method is going to do with a block, it just
creates a closure.

Gennady.

On 8/1/06, Chad P. [email protected] wrote:

On Tue, Aug 01, 2006 at 12:05:50PM +0900, [email protected] wrote:

It’s a closure because it carries the context of its creation with it.
It doesn’t matter whether that context has zero, one, or fifty local
variables; the same thing still happens.

It sounds like what you’re saying is that the lexical scope of the code
block (proc/lambda/blah) is what makes it a closure, and not the
connection with, and OOPish protection/encapsulation of, something that
started outside the code block and went out of scope externally to the
code block.

No, what he’s saying is that what makes it a closure is exactly that
form of encapsulation (capture, I would say) of its surrounding
environment. I think that’s the key distinction here.

You seem set in the belief that a closure is around a specific
variable or group of variables. If those variables don’t exist, then
the closure must not exist. Unfortunately the current definition on
Wikipedia (which I consider useful, but not authoritative) reflects
that type of thinking.

But the definition I learned in the University, have seen in multiple
CS theory texts and to which David is referring here is that the
closure is around an environment, regardless of what variables from
that environment may or may not exist and/or be used. So even if the
environment were empty, or the lambda/proc didn’t reference any
variables from the environment, the lambda/proc is still capturing
that environment, forming a closure around it.

Even then, an empty environment can’t exist in Ruby. There’s always at
least one “variable” in the environment that will be accessible in the
closure: self. Take the following example:

class Foo
def bar
lambda{ baz }
end
end

foo = Foo.new
bar = foo.bar

class << foo
def baz
puts “quux”
end
end

bar.call

prints “quux”

It can’t be argued that the definition of baz is being captured from
its surrounding environment; it didn’t exist when the lambda was
created, and even when it was defined, it was in a different
environment. What the lambda formed a closure around was the
environment. Included in that environment was self. When the
interpreter sees “baz”, it sends (essentially, there’s of course
permission checks and such) “baz” to the self defined in the lambda’s
environment; that is, the self captured by the closure. The method
lookup chain on that object can then find the definition of baz we put
into the singleton class for foo.

So the environment is never empty. The only remaining argument would
be something like “If a lambda/proc doesn’t use any values from it’s
environment (even self, so no receiverless method calls), then it’s
not a closure.” I say it would be a closure anyways, since the
captured environment is still there, still taking up memory, still
accessible, only unused.

$foo->();

From what I learned in my CS courses and my knowledge of how perls
scopes work, that is a closure. Simply because the facilities
provided by a construct aren’t used doesn’t make the construct cease
to be…

That’s the equivalent of this, in Ruby:

def bar
lambda { puts “Hello world!” }
end

foo = bar

foo.call

Again, also a closure. But this one even more specifically: in the
method call to puts, you’re using the value of self captured in the
environment closed over by the lambda.

Jacob F.

On Wed, Aug 02, 2006 at 01:16:39AM +0900, Gennady B. wrote:

Yes, it is:

Great. You just failed to answer the rest of my question, though.
Instead, you built a bunch of code around my piece that has nothing to
do with my question. It was less than strictly helpful.

Also, I would appreciate it if you’d not top post in reply to me –
especially not in TOFU style.

Chad,

Sorry for jumping in late in the game, however I think this can explain
why a block is a closure even when it does not seemingling reference any
variables from the scope it was created in:

class A
def f
proc {
puts “abc”
}
end
end

a = A.new

b = a.f
b.call

class << a
def puts(*args)
$stderr.puts args.map { |_a| _a.upcase }
end
end

b.call

Procuces:

abc
ABC

Closure references not only variables, but anything else in the scope,
like methods, etc. And the redefined method may reference variables in
that scope, that’s why the “full blown” closures are created no matter
what the block [currently] references.

Gennady.

On Tue, Aug 01, 2006 at 10:20:06PM +0900, Just Another Victim of the
Ambient M. wrote:

I think 3 is a better characterization than 2.  Here's some code to 

demonstrate:

I don’t. A closure is a closure whether you use it or not, just as a
remote control for the TV is a remote control for the TV whether you use
it or not. What’s actually relevant to the discussion is more like the
question of whether a remote control for the TV is still a remote
control for the TV if it was built with no TV on which it could operate.

variable = 2 # this is the context

[1, 2, 3].each do |i|
# it sounds as if you think this is the “context”
puts i + variable

# either way, we're using the context so
# we agree that this is a closure

end

Actually . . . no. If that was a closure just because there was a
variable nearby that it could access, then in any script that contains a
lexical variable at the “base level” of the script, and in any script
that has global variables, every scope enclosed within the scope of the
script would be a closure.

I’m beginning to think the question of whether it’s actually a closure
really IS a question for a programming koan, after all. If so, I’m glad
we’ve at least narrowed the discussion down to this point at last.

I agree that we have finally narrowed down the discussion.  This might 

have happened sooner if I had actually followed the thread…

Unfortunately, it was David and possibly one or two others who have been
instrumental in narrowing it down. You’re still talking about things
that don’t seem to bear on what I was saying at all.

Not to be insulting but there's a part of me that thinks that you came 

up with this distinction to hide the (embarrassing?) fact that you didn’t
know that all blocks were closures…

Not to be insulting, but I don’t think you’ve grasped the fact that
there’s more to being a closure than being a language construct that
someone once said was a closure.

On Tue, Aug 01, 2006 at 09:03:50PM +0900, [email protected] wrote:

No; what makes something a closure, as I understand it, is that it
carries the context of its creation with it. Also, there’s actually a
difference between a block on the one hand, and a Proc or lambda on
the other. (See my last couple of posts.)

That’s far too broad to even be a meaningful distinction. It’s
tantamount to claiming that in a program where everything has lexical
scope, everything is a closure – because everything’s “context”
(essentially its scope) is always applicable to it as it was at the
point of its creation with lexical scope.

I think of a closure as a kind of suitcase: you pack it in one place,
and unpack it somewhere else. Even if it’s empty, though, it’s still
a suitcase.

That makes more sense than what you said above (that I quoted above and
to which I responded above): it sounds like we’re back to the koan here.
Specifically, it sounds like what you’re saying here would indicate that
this is a closure:

def foo
bar = 0
lambda { puts bar += 1 }
end

baz = foo

and this is a closure:

def foo
lambda { puts “Hellow world!” }
end

bar = foo

because it does the same things with scope, but this is not a closure:

foo = 0

foo.each do
puts bar += 1
end

because its functionally equivalent to accessing global scope (which
even GWBASIC can do). Unfortunately, I suspect that you would say that
last example actually is a closure, and your “think of a closure”
description above was simply not precise enough.

I’d accept the description you gave above as being koan-compliant, with
the first two being closures within a broad but theoretically applicable
definition of a closure, as long as the apparent meaning of your
definition-by-analogy that excludes the third example is the definition
you’re using. Once you interpret it to include the third definition, I
have a distinct problem with it.

Another way to look at it is this: if you decide that the lambda in
your example above is not a closure, then you have to come up with
separate explanations for everything it does that is closure-like.
If you look at it as a closure, however, there’s nothing unaccounted
for.

. . . and that’s why it makes sense to think of it as fitting a
koan-compliant definition of a closure as you explained above, so long
as what it carries around with it is actually a lexically closed
scope
, even if there’s nothing meaningful within that closed scope.

That still doesn’t make all blocks closures, though.

On Wed, Aug 02, 2006 at 04:10:21AM +0900, Chad P. wrote:

I don’t. A closure is a closure whether you use it or not, just as a
remote control for the TV is a remote control for the TV whether you use
it or not. What’s actually relevant to the discussion is more like the
question of whether a remote control for the TV is still a remote
control for the TV if it was built with no TV on which it could operate.

Okay, that wasn’t quite right, either. It’s more like this:

Is a remote control for the TV still a remote control for the TV if the
TV cannot turn on or off, has only one volume setting, and has only one
channel, and if the remote has no buttons for sending any signals to the
TV, but the remote still has a transmitter and the TV still has a
receiver?

On Wed, 2 Aug 2006, Chad P. wrote:

On Wed, Aug 02, 2006 at 01:16:39AM +0900, Gennady B. wrote:

Yes, it is:

Great. You just failed to answer the rest of my question, though.
Instead, you built a bunch of code around my piece that has nothing to
do with my question. It was less than strictly helpful.

Also, I would appreciate it if you’d not top post in reply to me –
especially not in TOFU style.

http://hypermetrics.com/rubyhacker/clrFAQ.html#tag22 - would be better
for everyone’s bandwidth

-a

On Wed, Aug 02, 2006 at 01:28:45AM +0900, Jacob F. wrote:

environment. I think that’s the key distinction here.

You seem set in the belief that a closure is around a specific
variable or group of variables. If those variables don’t exist, then
the closure must not exist. Unfortunately the current definition on
Wikipedia (which I consider useful, but not authoritative) reflects
that type of thinking.

The current definition on Wikipedia is scattered, vague, and without
salient points on this matter. I’ve read through the thing from end to
end, and the article spends an awful lot of time failing to concretely
define a closure.

But the definition I learned in the University, have seen in multiple
CS theory texts and to which David is referring here is that the
closure is around an environment, regardless of what variables from
that environment may or may not exist and/or be used. So even if the
environment were empty, or the lambda/proc didn’t reference any
variables from the environment, the lambda/proc is still capturing
that environment, forming a closure around it.

That’s a definition of a closure that I would at this point at least not
call “wrong”, even if it doesn’t strike me as particularly “right”
either, because the explanations of closures I’ve seen can potentially
be interpreted in that manner. Even so, I’d say that to fit a strict
definition of a closure, the chunk of code in question needs to exit its
enclosing scope before it’s a closure – else it’s not “closed”.

prints “quux”

Now you’re arguing that there does, indeed, have to be a variable.
What? In any case, this can happen in Ruby:

class Foo
def bar
lambda { puts “quux” }
end
end

foo = Foo.new
bar = foo.bar

bar.call

. . . so I don’t see your point. Cut out the middleman, and it still
works.

On Tue, Aug 01, 2006 at 11:00:12PM +0900, Just Another Victim of the
Ambient M. wrote:

Then x _will_ be a closure but, then again, we already knew this...

If that’s the case, then everything else is a closure too, because
everything else has access to a – at least where lexical or global
scope exists.

On Tue, Aug 01, 2006 at 09:40:05PM +0900, Just Another Victim of the
Ambient M. wrote:

...and this would be a problem if the definition of a closure is the 

calling of a Proc object derived from a block. Alas, that is not the
definition of a closure…
When we yield to the block, we are doing so in our method, which is
not the same scope as the scope where the block was created. Yet, the
block we’re yielding to still has access to that other scope. How? Because
it is a closure…

We’re back to the absurdly broad here:

If any “thing” that has access to another scope is a closure, that makes
every nested “thing” in a language with global and/or lexical scope a
“closure”. That’s a little like defining a “human” as “has four limbs”.

On Tue, Aug 01, 2006 at 11:34:35PM +0900, [email protected] wrote:

Then x will be a closure but, then again, we already knew this…

But the a inside x and the a outside x are unrelated. Method
definition blocks (with def) aren’t closures.

Wait . . . are you serious? Ruby is using dynamic scope here? Ugh.