Perl6::Gather

Perl6::Gather is a Perl module that implements the Perl 6 'gather/take' control structure in Perl 5.
Download

Perl6::Gather Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Damian Conway
  • Publisher web site:
  • http://search.cpan.org/~dconway/

Perl6::Gather Tags


Perl6::Gather Description

Perl6::Gather is a Perl module that implements the Perl 6 'gather/take' control structure in Perl 5. 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 aren't any of either... take @defaults unless gathered; }BACKGROUNDPerl 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 call's argument list to an implicitly created array. take returns the number of elements it took.At the end of the block's 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 /^____n$/; $string .= $_; } $string; };we could write: $text = ~gather { for { next if /^#|^s*$/; last if /^____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 /$/; } take 1,3,5,7,9 unless gathered; }It's 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 /$/; } unshift gathered, +grep(//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 } } }Requirements:· Perl Requirements: · Perl


Perl6::Gather Related Software