File Coverage

blib/lib/Syntax/Operator/Zip.pm
Criterion Covered Total %
statement 31 34 91.1
branch 7 8 87.5
condition n/a
subroutine 8 9 88.8
pod 1 2 50.0
total 47 53 88.6


line stmt bran cond sub pod time code
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 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Zip 0.04;
7              
8 4     4   170663 use v5.14;
  4         30  
9 4     4   19 use warnings;
  4         5  
  4         89  
10              
11 4     4   16 use Carp;
  4         7  
  4         792  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - infix operator to compose two lists together
19              
20             =head1 SYNOPSIS
21              
22             On a suitably-patched perl:
23              
24             use Syntax::Operator::Zip;
25              
26             foreach (@xvals Z @yvals) {
27             my ($x, $y) = @$_;
28             say "Value $x is associated with value $y";
29             }
30              
31             Or, on a standard perl:
32              
33             use v5.14;
34             use Syntax::Operator::Zip qw( zip );
35              
36             foreach (zip \@xvals, \@yvals) {
37             my ($x, $y) = @$_;
38             say "Value $x is associated with value $y";
39             }
40              
41             =head1 DESCRIPTION
42              
43             This module provides infix operators that compose two lists of elements by
44             associating successive elements from the left and right-hand lists together,
45             forming a new list.
46              
47             =cut
48              
49             sub import
50             {
51 4     4   30 my $class = shift;
52 4         10 my $caller = caller;
53              
54 4         14 $class->import_into( $caller, @_ );
55             }
56              
57             sub import_into
58             {
59 4     4 0 7 my $class = shift;
60 4         8 my ( $caller, @syms ) = @_;
61              
62 4 100       18 @syms or @syms = qw( Z );
63              
64 4         10 my %syms = map { $_ => 1 } @syms;
  5         22  
65 4 100       25 $^H{"Syntax::Operator::Zip/Z"}++ if delete $syms{Z};
66              
67 4         10 foreach (qw( zip mesh )) {
68 4     4   26 no strict 'refs';
  4         6  
  4         520  
69 8 100       20 *{"${caller}::$_"} = \&{$_} if delete $syms{$_};
  2         7  
  2         5  
70             }
71              
72 4 50       1144 croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms;
  0            
73             }
74              
75             =head1 OPERATORS
76              
77             =head2 Z
78              
79             my @result = @lhs Z @rhs;
80              
81             # returns [$lhs[0], $rhs[0]], [$lhs[1], $rhs[1]], ...
82              
83             Yields a list of array references, each containing a pair of items from the
84             two operand lists. If one of the operand lists is shorter than the other, the
85             missing elements will be filled in with C so that every array reference
86             in the result contains exactly two items.
87              
88             =head2 M
89              
90             my @result = @lhs M @rhs;
91              
92             # returns $lhs[0], $rhs[0], $lhs[1], $rhs[1], ...
93              
94             Yields a list of the values from its operand lists, rearranged into pairs and
95             flattened. If one of the operand lists is shorter than the other, the missing
96             elements will be filled in with C so that the result is correctly lined
97             up.
98              
99             The result of this operator is useful for constructing hashes from two lists
100             containing keys and values
101              
102             my %hash = @keys M @values;
103              
104             =cut
105              
106             =head1 FUNCTIONS
107              
108             As a convenience, the following functions may be imported which implement the
109             same behaviour as the infix operators, though are accessed via regular
110             function call syntax. The two lists for these functions to operate on must be
111             passed as references to arrays (either named variables, or anonymously
112             constructed by C<[...]>).
113              
114             =head2 zip
115              
116             my @result = zip( \@lhs, \@rhs );
117              
118             A function version of the L operator.
119              
120             See also L.
121              
122             =head2 mesh
123              
124             my @result = mesh( \@lhs, \@rhs );
125              
126             A function version of the L operator.
127              
128             See also L.
129              
130             =cut
131              
132             =head1 AUTHOR
133              
134             Paul Evans
135              
136             =cut
137              
138             0x55AA;