| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # (C) Paul Evans, 2021-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Syntax::Operator::Zip 0.10; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 5 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 5 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 5 | ▶ | use meta 0.004; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | ▶ | no warnings 'meta::experimental'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | require XSLoader; | |||||||||||||||||||
| 17 | 5 | XSLoader::load( __PACKAGE__, our $VERSION ); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | C<Syntax::Operator::Zip> - infix operator to compose two lists together | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | On Perl v5.38 or later: | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | use Syntax::Operator::Zip; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | foreach (@xvals Z @yvals) { | ||||||||||||||||||||
| 30 | my ($x, $y) = @$_; | ||||||||||||||||||||
| 31 | say "Value $x is associated with value $y"; | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | Or on Perl v5.14 or later: | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | use v5.14; | ||||||||||||||||||||
| 37 | use Syntax::Operator::Zip qw( zip ); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | foreach (zip \@xvals, \@yvals) { | ||||||||||||||||||||
| 40 | my ($x, $y) = @$_; | ||||||||||||||||||||
| 41 | say "Value $x is associated with value $y"; | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | This module provides infix operators that compose lists of elements by | ||||||||||||||||||||
| 47 | associating successive elements from each of the input lists, forming a new | ||||||||||||||||||||
| 48 | list. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Support for custom infix operators was added in the Perl 5.37.x development | ||||||||||||||||||||
| 51 | cycle and is available from development release v5.37.7 onwards, and therefore | ||||||||||||||||||||
| 52 | in Perl v5.38 onwards. The documentation of L<XS::Parse::Infix> | ||||||||||||||||||||
| 53 | describes the situation in more detail. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | While Perl versions before this do not support custom infix operators, they | ||||||||||||||||||||
| 56 | can still be used via C<XS::Parse::Infix> and hence L<XS::Parse::Keyword>. | ||||||||||||||||||||
| 57 | Custom keywords which attempt to parse operator syntax may be able to use | ||||||||||||||||||||
| 58 | these. | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | Additionally, earlier versions of perl can still use the function-like | ||||||||||||||||||||
| 61 | wrapper versions of these operators. Even though the syntax appears like a | ||||||||||||||||||||
| 62 | regular function call, the code is compiled internally into the same more | ||||||||||||||||||||
| 63 | efficient operator internally, so will run without the function-call overhead | ||||||||||||||||||||
| 64 | of a regular function. | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub import | ||||||||||||||||||||
| 69 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 4 | my $pkg = shift; | |||||||||||||||||||
| 71 | 4 | my $caller = caller; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 4 | $pkg->import_into( $caller, @_ ); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub unimport | ||||||||||||||||||||
| 77 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
unimport: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 2 | my $pkg = shift; | |||||||||||||||||||
| 79 | 2 | my $caller = caller; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | $pkg->unimport_into( $caller, @_ ); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 4 | ▶ | sub import_into { shift->apply( 1, @_ ) } | ||||||||||||||||||
|
Subroutine
import_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 85 | 2 | ▶ | sub unimport_into { shift->apply( 0, @_ ) } | ||||||||||||||||||
|
Subroutine
unimport_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | sub apply | ||||||||||||||||||||
| 88 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
apply: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||
| 89 | 6 | my $pkg = shift; | |||||||||||||||||||
| 90 | 6 | my ( $on, $caller, @syms ) = @_; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 6 | ▶ | @syms or @syms = qw( Z M ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 6 | my %syms = map { $_ => 1 } @syms; | |||||||||||||||||||
| 95 | 6 | foreach (qw( Z M )) { | |||||||||||||||||||
| 96 | 12 | ▶ | next unless delete $syms{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | $on ? $^H{"Syntax::Operator::Zip/$_"}++ | ||||||||||||||||||||
| 99 | 6 | ▶ | : delete $^H{"Syntax::Operator::Zip/$_"}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 6 | my $callerpkg; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 6 | foreach (qw( zip mesh )) { | |||||||||||||||||||
| 105 | 12 | ▶ | next unless delete $syms{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 4 | ▶ | $callerpkg //= meta::package->get( $caller ); | ||||||||||||||||||
|
Condition66%2/3
$callerpkg //= "meta::package"->get($caller)
MC/DC0%0/2
$callerpkg //= "meta::package"->get($caller)
$callerpkg
"meta::package"->get($caller)
Truth table66%2/3
A //= B
A= $callerpkgB= "meta::package"->get($caller)
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 4 | ▶ | $on ? $callerpkg->add_symbol( '&'.$_ => \&{$_} ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | : $callerpkg->remove_symbol( '&'.$_ ); | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 OPERATORS | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 Z | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | my @result = @lhs Z @rhs; | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # returns [$lhs[0], $rhs[0]], [$lhs[1], $rhs[1]], ... | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Yields a list of array references, each containing a pair of items from the | ||||||||||||||||||||
| 125 | two operand lists. If one of the operand lists is shorter than the other, the | ||||||||||||||||||||
| 126 | missing elements will be filled in with C<undef> so that every array reference | ||||||||||||||||||||
| 127 | in the result contains exactly two items. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | my @result = @alphas Z @betas Z @gammas Z ... | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # returns [$alphas[0], $betas[0], $gammas[0], ...], ... | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | I<Since version 0.10> this module supports list-associative combinations of | ||||||||||||||||||||
| 134 | more than two input lists at once. The result will be composed of parallel | ||||||||||||||||||||
| 135 | items from each of the given input lists. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head2 M | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | my @result = @lhs M @rhs; | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # returns $lhs[0], $rhs[0], $lhs[1], $rhs[1], ... | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Yields a list of the values from its operand lists, rearranged into pairs and | ||||||||||||||||||||
| 144 | flattened. If one of the operand lists is shorter than the other, the missing | ||||||||||||||||||||
| 145 | elements will be filled in with C<undef> so that the result is correctly lined | ||||||||||||||||||||
| 146 | up. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | my @result = @alphas M @betas M @gammas M ... | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | # returns $alphas[0], $betas[0], $gammas[0], ..., $alphas[1], ... | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | I<Since version 0.10> this module supports list-associative combinations of | ||||||||||||||||||||
| 153 | more than two input lists at once. The result will be composed of parallel | ||||||||||||||||||||
| 154 | items from each of the given input lists. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | The result of this operator is useful for constructing hashes from two lists | ||||||||||||||||||||
| 157 | containing keys and values | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | my %hash = @keys M @values; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | This is also useful combined with the multiple variable C<foreach> syntax of | ||||||||||||||||||||
| 162 | Perl 5.36 and above: | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | foreach my ( $alpha, $beta, $gamma ) ( @alphas M @betas M @gammas ) { | ||||||||||||||||||||
| 165 | ... | ||||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 FUNCTIONS | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | As a convenience, the following functions may be imported which implement the | ||||||||||||||||||||
| 173 | same behaviour as the infix operators, though are accessed via regular | ||||||||||||||||||||
| 174 | function call syntax. The lists for these functions to operate on must be | ||||||||||||||||||||
| 175 | passed as references to arrays (either named variables, or anonymously | ||||||||||||||||||||
| 176 | constructed by C<[...]>). | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 zip | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | my @result = zip( \@lhs, \@rhs, ... ); | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | A function version of the L</Z> operator. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | See also L<List::Util/zip>. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head2 mesh | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | my @result = mesh( \@lhs, \@rhs, ... ); | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | A function version of the L</M> operator. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | See also L<List::Util/mesh>. | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 5 | 0x55AA; | |||||||||||||||||||