Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Lexical closures

by Corion (Pope)
on Oct 25, 2008 at 08:27 UTC ( #719479=note: print w/replies, xml ) Need Help??


in reply to Re^2: Lexical closures
in thread Lexical closures

Well, it depends on what the closures really close over, respectively when a new instance of the loop variable/value gets created. It seems that in Perl, at least when you use a lexical loop variable, you get a new copy each iteration, while in the other languages, you don't. To test this theory, create a new lexical variable within the loop body in each language and see if that's different, that is, use the local equivalent of the following Perl code to create the closures:

for (0..2) { my $i = $_; push @funcs, sub { $i * $_[0] }; };

You want to force allocation of a new instance of the lexical variable so your subroutine references get a new instance on each way around.

Of course, most of the languages have map, so using it would be more apt.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://719479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2019-07-21 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (7 votes). Check out past polls.

    Notices?