| 1 | package List::Gather; # git description: 0.13-9-g4c4e5d3 | ||||||||||
| 2 | # ABSTRACT: Construct lists procedurally without temporary variables | ||||||||||
| 3 | |||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 7 | our $VERSION = '0.14'; | |||||||||
| 8 | |||||||||||
| 9 | 7 | ▶ | use Devel::CallParser; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | ▶ | use Devel::CallChecker; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 7 | ▶ | use XSLoader; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | XSLoader::load( | |||||||||
| 15 | __PACKAGE__, | ||||||||||
| 16 | $VERSION, | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | require B::Hooks::EndOfScope | ||||||||||
| 20 | 7 | ▶ | unless _QPARSE_DIRECTLY(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 7 | my @keywords; | |||||||||
| 23 | 7 | ▶ | BEGIN { @keywords = qw(gather take gathered) } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 7 | ▶ | use Sub::Exporter -setup => { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | exports => [@keywords], | ||||||||||
| 27 | groups => { default => [@keywords] }, | ||||||||||
| 28 | 7 | }; | |||||||||
| 29 | |||||||||||
| 30 | #pod =head1 SYNOPSIS | ||||||||||
| 31 | #pod | ||||||||||
| 32 | #pod use List::Gather; | ||||||||||
| 33 | #pod | ||||||||||
| 34 | #pod my @list = gather { | ||||||||||
| 35 | #pod while (<$fh>) { | ||||||||||
| 36 | #pod next if /^\s*$/; | ||||||||||
| 37 | #pod next if /^\s*#/; | ||||||||||
| 38 | #pod last if /^(?:__END__|__DATA__)$/; | ||||||||||
| 39 | #pod take $_ if some_predicate($_); | ||||||||||
| 40 | #pod } | ||||||||||
| 41 | #pod | ||||||||||
| 42 | #pod take @defaults unless gathered; | ||||||||||
| 43 | #pod }; | ||||||||||
| 44 | #pod | ||||||||||
| 45 | #pod =head1 DESCRIPTION | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod This module provides a C<gather> keyword that allows lists to be constructed | ||||||||||
| 48 | #pod procedurally, without the need for a temporary variable. | ||||||||||
| 49 | #pod | ||||||||||
| 50 | #pod Within the block controlled by a C<gather> any call to C<take> pushes that | ||||||||||
| 51 | #pod call's argument list to an implicitly created array. | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod C<gather> returns the list of values taken during its block's execution. | ||||||||||
| 54 | #pod | ||||||||||
| 55 | #pod =head1 EXAMPLES | ||||||||||
| 56 | #pod | ||||||||||
| 57 | #pod my @interesting_child_nodes = gather for my $n (@nodes) { | ||||||||||
| 58 | #pod take $n->all_children | ||||||||||
| 59 | #pod if $n->is_interesting; | ||||||||||
| 60 | #pod }; | ||||||||||
| 61 | #pod | ||||||||||
| 62 | #pod my @last_10_events = gather { | ||||||||||
| 63 | #pod while ($log->has_event) { | ||||||||||
| 64 | #pod take $log->next_event; | ||||||||||
| 65 | #pod } | ||||||||||
| 66 | #pod | ||||||||||
| 67 | #pod shift gathered while gathered > 10; | ||||||||||
| 68 | #pod }; | ||||||||||
| 69 | #pod | ||||||||||
| 70 | #pod my @search_results = gather { | ||||||||||
| 71 | #pod $user_interface->register_status_callback(sub { | ||||||||||
| 72 | #pod sprintf "Searching... Found %d matches so far", scalar gathered; | ||||||||||
| 73 | #pod }); | ||||||||||
| 74 | #pod | ||||||||||
| 75 | #pod wait_for_search_results(sub { | ||||||||||
| 76 | #pod my ($result) = @_; | ||||||||||
| 77 | #pod take $result; | ||||||||||
| 78 | #pod }, @search_terms); | ||||||||||
| 79 | #pod | ||||||||||
| 80 | #pod $user_interface->register_status_callback(sub { | ||||||||||
| 81 | #pod sprintf "Found a total of %d", scalar gathered; | ||||||||||
| 82 | #pod }); | ||||||||||
| 83 | #pod }; | ||||||||||
| 84 | #pod | ||||||||||
| 85 | #pod my @leaf_nodes = gather { | ||||||||||
| 86 | #pod $graph->visit_all_nodes_recursively(sub { | ||||||||||
| 87 | #pod my ($node) = @_; | ||||||||||
| 88 | #pod take $node if $node->is_leaf; | ||||||||||
| 89 | #pod }); | ||||||||||
| 90 | #pod }; | ||||||||||
| 91 | #pod | ||||||||||
| 92 | #pod =func gather | ||||||||||
| 93 | #pod | ||||||||||
| 94 | #pod gather { ... } | ||||||||||
| 95 | #pod gather({ ... }) | ||||||||||
| 96 | #pod gather STMT | ||||||||||
| 97 | #pod | ||||||||||
| 98 | #pod Executes the block it has been provided with, collecting all arguments passed to | ||||||||||
| 99 | #pod C<take> calls within it. After execution, the list of values collected is | ||||||||||
| 100 | #pod returned. | ||||||||||
| 101 | #pod | ||||||||||
| 102 | #pod Note that block C<gather> executes is equivalent to a C<do BLOCK>. It is neither | ||||||||||
| 103 | #pod a code nor a loop. Loop control keywords, such as C<next> and C<last>, as well | ||||||||||
| 104 | #pod as C<return> will behave accordingly. | ||||||||||
| 105 | #pod | ||||||||||
| 106 | #pod Parens around the C<gather> block are optional. | ||||||||||
| 107 | #pod | ||||||||||
| 108 | #pod =func take | ||||||||||
| 109 | #pod | ||||||||||
| 110 | #pod take LIST | ||||||||||
| 111 | #pod | ||||||||||
| 112 | #pod Collects a C<LIST> of values within the C<gather> block it has been compiled in. | ||||||||||
| 113 | #pod | ||||||||||
| 114 | #pod C<take> returns all its arguments. | ||||||||||
| 115 | #pod | ||||||||||
| 116 | #pod C<take> calls outside of the lexical scope of a C<gather> block are compile time | ||||||||||
| 117 | #pod errors. Calling C<take> is only legal within the dynamic scope its associated | ||||||||||
| 118 | #pod C<gather> block. | ||||||||||
| 119 | #pod | ||||||||||
| 120 | #pod =func gathered | ||||||||||
| 121 | #pod | ||||||||||
| 122 | #pod gathered | ||||||||||
| 123 | #pod | ||||||||||
| 124 | #pod Returns the list of items collected so far during the execution of a C<gather> | ||||||||||
| 125 | #pod block. | ||||||||||
| 126 | #pod | ||||||||||
| 127 | #pod C<gathered> calls outside of the lexical scope of a C<gather> block are compile | ||||||||||
| 128 | #pod time errors. Calling C<gathered> outside of the dynamic scope of its associated | ||||||||||
| 129 | #pod C<gather> block is legal. | ||||||||||
| 130 | #pod | ||||||||||
| 131 | #pod =head1 SEE ALSO | ||||||||||
| 132 | #pod | ||||||||||
| 133 | #pod =for :list | ||||||||||
| 134 | #pod = L<Syntax::Keyword::Gather> | ||||||||||
| 135 | #pod A non-lexical gather/take implementation that's otherwise very similar to this one | ||||||||||
| 136 | #pod = L<Perl6::GatherTake> | ||||||||||
| 137 | #pod An experimental implementation of a lazily evaluating gather/take | ||||||||||
| 138 | #pod = L<Perl6::Take> | ||||||||||
| 139 | #pod A very simple gather/take implementation without lexical scoping | ||||||||||
| 140 | #pod = L<Perl6::Gather> | ||||||||||
| 141 | #pod Like L<Syntax::Keyword::Gather>, but reliant on L<Perl6::Export> | ||||||||||
| 142 | #pod = L<List::Gen> | ||||||||||
| 143 | #pod A comprehensive suit list generation functions featuring a non-lexical gather/take | ||||||||||
| 144 | #pod | ||||||||||
| 145 | #pod =head1 ACKNOWLEDGEMENTS | ||||||||||
| 146 | #pod | ||||||||||
| 147 | #pod =for :list | ||||||||||
| 148 | #pod * Andrew Main (Zefram) E<lt>zefram@fysh.orgE<gt> | ||||||||||
| 149 | #pod for providing his input in both the design and implementation of this module, | ||||||||||
| 150 | #pod and writing much of the infrastructure that made this module possible in the | ||||||||||
| 151 | #pod first place | ||||||||||
| 152 | #pod * Arthur Axel "fREW" Schmidt E<lt>frioux+cpan@gmail.comE<gt> | ||||||||||
| 153 | #pod for his input on various aspects of this module as well as the many tests of his | ||||||||||
| 154 | #pod L<Syntax::Keyword::Gather> module that this module shamelessly stole | ||||||||||
| 155 | #pod * Dave (autarch) Rolsky <autarch@urth.org> and Jesse (doy) Luehrs E<lt>doy@tozt.netE<gt> | ||||||||||
| 156 | #pod for helping to improve both documentation and test coverage | ||||||||||
| 157 | #pod | ||||||||||
| 158 | #pod =cut | ||||||||||
| 159 | |||||||||||
| 160 | 7 | 1; | |||||||||
| 161 | |||||||||||
| 162 | __END__ | ||||||||||