Perl6::Gather 0.04
Sponsored Links
Perl6::Gather 0.04 Ranking & Summary
File size:
0.005 MB
Platform:
Any Platform
License:
Perl Artistic License
Price:
Downloads:
838
Date added:
2007-07-09
Publisher:
Damian Conway
Perl6::Gather 0.04 description
Perl6::Gather is a Perl module that implements the Perl 6 gather/take control structure in Perl 5.
SYNOPSIS
use Perl6::Gather;
@list = gather {
# Try to extract odd numbers and odd number names...
for (@data) {
if (/(one|three|five|nine)$/) { take qq{$_}; }
elsif (/^d+$/ && $_ %2) { take; }
}
# But use the default set if there arent any of either...
take @defaults unless gathered;
}
BACKGROUND
Perl 6 provides a new control structure -- gather -- that allows lists to be constructed procedurally, without the need for a temporary variable. Within the block/closure controlled by a gather any call to take pushes that calls argument list to an implicitly created array. take returns the number of elements it took.
At the end of the blocks execution, the gather returns the list of values stored in the array (in a list context) or a reference to the array (in a scalar context).
For example, instead of writing:
# Perl 6 code...
print do {
my @wanted;
for <> -> $line {
push @wanted, $line if $line ~~ /D/;
push @wanted, -$line if some_other_condition($line);
}
push @wanted, EOF;
@wanted;
};
in Perl 6 we can write:
# Perl 6 code...
print gather {
for <> -> $line {
take $line if $line ~~ /D/;
take -$line if some_other_condition($line);
}
take EOF;
}
and instead of:
$text = do {
my $string;
for <> {
next if /^#|^s*$/;
last if /^__[DATA|END]__n$/;
$string .= $_;
}
$string;
};
we could write:
$text = ~gather {
for <> {
next if /^#|^s*$/;
last if /^__[DATA|END]__n$/;
take;
}
}
As the above example implies, if take is called without any arguments, it takes the current topic.
There is also a third function -- gathered -- which returns a reference to the implicit array being gathered. This is useful for handling defaults:
@odds = gather {
for @data {
take if $_ % 2;
take to_num($_) if /[one|three|five|nine]$/;
}
take 1,3,5,7,9 unless gathered;
}
Its also handy for creating the implicit array by some process more complex than by simple sequential pushing. For example, if we needed to prepend a count of non-numeric items:
@odds = gather {
for @data {
take if $_ %2;
take to_num($_) if /[one|three|five|nine]$/;
}
unshift gathered, +grep(/[a-z]/i, @data);
}
Conceptually gather/take is the generalized form from which both map and grep derive. That is, we could implement those two functions as:
sub map ($transform is Code, *@list) {
return gather { for @list { take $transform($_) } };
}
sub grep ($selected is Code|Rule, *@list) {
return gather { for @list { take when $selected } }
}
A gather is also a very handy way of short-circuiting the construction of a list. For example, suppose we wanted to generate a single sorted list of lines from two sorted files, but only up to the first line they have in common. We could gather the lines like this:
my @merged_diff = gather {
my $a = < $fh_a >;
my $b = < $fh_b >;
loop {
if defined all $a,$b {
if $a eq $b { last } # Duplicate means end of list
elsif $a lt $b { take $a; $a = < $fh_a >; }
else { take $b; $b = < $fh_b >; }
}
elsif defined $a { take $a; $a = < $fh_a >; }
elsif defined $b { take $b; $b = < $fh_b >; }
else { last }
}
}
SYNOPSIS
use Perl6::Gather;
@list = gather {
# Try to extract odd numbers and odd number names...
for (@data) {
if (/(one|three|five|nine)$/) { take qq{$_}; }
elsif (/^d+$/ && $_ %2) { take; }
}
# But use the default set if there arent any of either...
take @defaults unless gathered;
}
BACKGROUND
Perl 6 provides a new control structure -- gather -- that allows lists to be constructed procedurally, without the need for a temporary variable. Within the block/closure controlled by a gather any call to take pushes that calls argument list to an implicitly created array. take returns the number of elements it took.
At the end of the blocks execution, the gather returns the list of values stored in the array (in a list context) or a reference to the array (in a scalar context).
For example, instead of writing:
# Perl 6 code...
print do {
my @wanted;
for <> -> $line {
push @wanted, $line if $line ~~ /D/;
push @wanted, -$line if some_other_condition($line);
}
push @wanted, EOF;
@wanted;
};
in Perl 6 we can write:
# Perl 6 code...
print gather {
for <> -> $line {
take $line if $line ~~ /D/;
take -$line if some_other_condition($line);
}
take EOF;
}
and instead of:
$text = do {
my $string;
for <> {
next if /^#|^s*$/;
last if /^__[DATA|END]__n$/;
$string .= $_;
}
$string;
};
we could write:
$text = ~gather {
for <> {
next if /^#|^s*$/;
last if /^__[DATA|END]__n$/;
take;
}
}
As the above example implies, if take is called without any arguments, it takes the current topic.
There is also a third function -- gathered -- which returns a reference to the implicit array being gathered. This is useful for handling defaults:
@odds = gather {
for @data {
take if $_ % 2;
take to_num($_) if /[one|three|five|nine]$/;
}
take 1,3,5,7,9 unless gathered;
}
Its also handy for creating the implicit array by some process more complex than by simple sequential pushing. For example, if we needed to prepend a count of non-numeric items:
@odds = gather {
for @data {
take if $_ %2;
take to_num($_) if /[one|three|five|nine]$/;
}
unshift gathered, +grep(/[a-z]/i, @data);
}
Conceptually gather/take is the generalized form from which both map and grep derive. That is, we could implement those two functions as:
sub map ($transform is Code, *@list) {
return gather { for @list { take $transform($_) } };
}
sub grep ($selected is Code|Rule, *@list) {
return gather { for @list { take when $selected } }
}
A gather is also a very handy way of short-circuiting the construction of a list. For example, suppose we wanted to generate a single sorted list of lines from two sorted files, but only up to the first line they have in common. We could gather the lines like this:
my @merged_diff = gather {
my $a = < $fh_a >;
my $b = < $fh_b >;
loop {
if defined all $a,$b {
if $a eq $b { last } # Duplicate means end of list
elsif $a lt $b { take $a; $a = < $fh_a >; }
else { take $b; $b = < $fh_b >; }
}
elsif defined $a { take $a; $a = < $fh_a >; }
elsif defined $b { take $b; $b = < $fh_b >; }
else { last }
}
}
Perl6::Gather 0.04 Screenshot
Perl6::Gather 0.04 Keywords
Perl 6
Gather 0.04
control structure
Perl module
Line If
Perl 5
Perl
b
gather
line
list
6
Perl6::Gather
Perl6Gather
Perl6::Gather 0.04
Libraries
Bookmark Perl6::Gather 0.04
Perl6::Gather 0.04 Copyright
WareSeeker periodically updates pricing and software information of Perl6::Gather 0.04 full version from the publisher, so some information may be slightly out-of-date. You should confirm all information before relying on it. Software piracy is theft, Using crack, password, serial numbers, registration codes, key generators is illegal and prevent future development of Perl6::Gather 0.04 Edition. Download links are directly from our publisher sites, torrent files or links from rapidshare.com, yousendit.com or megaupload.com are not allowed
Featured Software
Want to place your software product here?
Please contact us for consideration.
Contact WareSeeker.com
Related Information
Perl 6 rules
perl 64 bit
.5 perl 6
this week in perl 6 july 20 26 2005
gathering of mustangs
java control structures
magic the gathering
gathering tables
perl 6 tutorial
control structures
linensnthings
perlane
listin diario
gatherer
c++ control structures
gathering of nations radio
global gathering
perl 6.0
Related Software
Perl6::Take is a Perl module to gather/take in Perl 5. Free Download
Perl6::Parameters is a module with Perl 6-style prototypes with named parameters. Free Download
Perl6::Placeholders is a Perl 6 implicitly declared parameters for Perl 5. Free Download
Perl6::Contexts - array and hash variables turn into references to themselves when used in non-numeric scalar context. Free Download
PLJava is Perl module that will embed Perl into Java. Free Download
Preppi is a simple graphical EPP client for Unix and Linux systems. Free Download
Perl6::Export::Attrs - the Perl 6 is export(...) trait as a Perl 5 attribute. Free Download
Perl6::Classes project contains first class classes in Perl 5. Free Download
Latest Software
Popular Software
Favourite Software