Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > closures and streams question

Reply
Thread Tools

closures and streams question

 
 
grocery_stocker
Guest
Posts: n/a
 
      02-05-2009
The following code generates even numbers..

[cdalten@localhost oakland]$ more even.pl
#!/usr/bin/perl -w

sub even_number_printer_gen {
# This function returns a reference to an anon. subroutine.
# This anon. subroutine prints even numbers starting from $input.
my($input) = @_;
if ($input % 2) { $input++}; # Next even number, if the given
# number is odd
$rs = sub {
print "$input "; # Using $input,which is a my variable
# declared in an outside scope
$input += 2;
};
return $rs; # Return a reference to the subroutine above
}

my $iterator = even_number_printer_gen(30);
# $iterator now points to a closure.
# Every time you call it, it prints the next successive even number.
for ($i = 0; $i < 10; $i++) {
&$iterator();
}
print "\n";

[cdalten@localhost oakland]$ ./even.pl
30 32 34 36 38 40 42 44 46 48
[cdalten@localhost oakland]$

The thing I'm not seeing why using clousres would be important in this
case. Can someone enlighten me?
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      02-05-2009
In article
<eb1db4f1-8e2e-45ea-a459->,
grocery_stocker <> wrote:

> The following code generates even numbers..
>
> [cdalten@localhost oakland]$ more even.pl
> #!/usr/bin/perl -w
>
> sub even_number_printer_gen {
> # This function returns a reference to an anon. subroutine.
> # This anon. subroutine prints even numbers starting from $input.
> my($input) = @_;
> if ($input % 2) { $input++}; # Next even number, if the given
> # number is odd
> $rs = sub {
> print "$input "; # Using $input,which is a my variable
> # declared in an outside scope
> $input += 2;
> };
> return $rs; # Return a reference to the subroutine above
> }
>
> my $iterator = even_number_printer_gen(30);
> # $iterator now points to a closure.
> # Every time you call it, it prints the next successive even number.
> for ($i = 0; $i < 10; $i++) {
> &$iterator();
> }
> print "\n";
>
> [cdalten@localhost oakland]$ ./even.pl
> 30 32 34 36 38 40 42 44 46 48
> [cdalten@localhost oakland]$
>
> The thing I'm not seeing why using clousres would be important in this
> case. Can someone enlighten me?


One reason would be so you could define two such generators. Try:

my $iter1 = even_number_printer_gen(30);
my $iter2 = even_number_printer_gen(100);
for (my $i = 0; $i < 10; $i++) {
&$iter1();
$iter2->();
}
print "\n";

__OUTPUT__

30 100 32 102 34 104 36 106 38 108 40 110 42 112 44 114 46 116 48 118

Because the subroutine is defined as a closure, there are two values of
the lexical variable $input defined instead of a single, shared one.

Note the alternate method of calling the second generator function.

--
Jim Gibson
 
Reply With Quote
 
 
 
 
smallpond
Guest
Posts: n/a
 
      02-05-2009
On Feb 5, 4:44*pm, grocery_stocker <cdal...@gmail.com> wrote:
> The following code generates even numbers..
>
> [cdalten@localhost oakland]$ more even.pl
> #!/usr/bin/perl -w
>
> sub even_number_printer_gen {
> * * # This function returns a reference to an anon. subroutine.
> * * # This anon. subroutine prints even numbers starting from $input.
> * * my($input) = @_;
> * * if ($input % 2) { $input++}; *# Next even number, if the given
> * * * * * * * * * * * * * * * * * # number is odd
> * * $rs = sub {
> * * * * print "$input "; *# Using $input,which is a my variable
> * * * * * * * * * * * * * * * * * # declared in an outside scope
> * * * * $input *+= 2;
> * * };
> * * return $rs; * # Return a reference to the subroutine above
>
> }
>
> my $iterator = even_number_printer_gen(30);
> # $iterator now points to a closure.
> # Every time you call it, it prints the next successive even number.
> for ($i = 0; $i < 10; $i++) {
> * * &$iterator();}
>
> print "\n";
>
> [cdalten@localhost oakland]$ ./even.pl
> 30 32 34 36 38 40 42 44 46 48
> [cdalten@localhost oakland]$
>
> The thing I'm not seeing why using clousres would be important in this
> case. Can someone enlighten me?



perl 5.10 has static "state" variables, you only need the closure in
5.8.

Why is it important? Can you be enlightened? Those aren't perl
questions. As an exercise, maybe try to access and print the value
of
$input inside your for loop.

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      02-05-2009
grocery_stocker <> wrote:

> The thing I'm not seeing why using clousres would be important in this
> case. Can someone enlighten me?



The biggies are allowing multiple iterators and data encapsulation
as in the other followups.

See also:

http://en.wikipedia.org/wiki/Closure_(computer_science)


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about closures Alex K Javascript 14 02-12-2011 04:43 PM
Closures / lambda question Aldric Giacomoni Ruby 12 10-30-2009 02:41 PM
Using closures and partial functions to eliminate redundant code Matthew Wilson Python 6 09-27-2007 09:14 AM
a question about jibbering.com FAQ essay on Javascript closures lkrubner@geocities.com Javascript 2 11-13-2005 07:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57