Perlude.pm

filter: Statementstmt 86.9% 127 / 146 Branchbran 85.7% 60 / 70 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 79.1% 38 / 48 Podpod 94.7% 18 / 19 totaltotal 83.5% 248 / 297 CC 42 SCAR 11.6
CC
42
Cov
85%
__ANON__
12.0
range
6.0
fold
6.0
CRAP 3.2
« Perlude/Lazy.pm ↑ Summary Perlude/Open.pm »
1 package Perlude;
2 14 ▶use Perlude::Open;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Exporter qw< import >;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 our @EXPORT = qw<
9 fold unfold
10 takeWhile take drop
11 filter apply
12 now
13 cycle range
14 tuple
15 concat concatC concatM
16 records lines
17 pairs
18 nth
19 chunksOf
20 open_file
21 >;
22  
23 # ABSTRACT: Shell and Powershell pipes, haskell keywords mixed with the awesomeness of perl. forget shell scrpting now!
24  
25 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 14 our $VERSION = '0.61';
28  
29 2 ▶sub pairs ($) {
Subroutine pairs: called CC=3 SCAR=11.1
Pod documented
30 2 my ( $ref ) = @_;
31 2 ▶ my $isa = ref $ref or die "'$ref' isn't a ref";
Branch: 1 branch
BranchTrueFalseTotal
my $isa = ref $ref or die "'${ref}' isn't a ref" 1 1 2
32  
33 # TODO: use reftypes here!
34 1 ▶ if ($isa eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($isa eq 'HASH') { } 1 0 1
35 6 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
36 6 my @pair;
37 6 ▶ while ( @pair = each %$ref ) { return \@pair }
Branch: 1 branch
BranchTrueFalseTotal
if (@pair = each %$ref) 5 1 6
38 ()
39 1 }
40 1 }
41 # elsif ($isa eq 'ARRAY') {
42 # my $index = 1;
43 # sub {
44 # return if $index > @$ref;
45 # my $r =
46 # [ $$ref[$index-1]
47 # , $$ref[$index] ];
48 # $index+=2;
49 # $r;
50 # }
51 # }
52 0 else { die "can't pair this kind of ref: $isa" }
53 }
54  
55 # sub pairs (&$) {
56 # my ( $do, $on ) = @_;
57 # sub {
58 # while ( @$_ = each %$on ) { return $do->() }
59 # ()
60 # }
61 # }
62  
63 # private helpers
64 90 ▶sub _buffer ($) {
Subroutine _buffer: called CC=1 SCAR=0.0
65 90 my ($i) = @_;
66 90 my @b;
67 50281 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
68 50281 ▶ return shift @b if @b;
Branch: 1 branch
BranchTrueFalseTotal
if @b 1 50280 50281
69 50280 @b = ( $i->() );
70 50280 ▶ return @b ? shift @b : ();
Branch: 1 branch
BranchTrueFalseTotal
@b ? : 50218 62 50280
71 }
72 90 }
73  
74 # interface with the Perl world
75 41 ▶sub unfold (@) {
Subroutine unfold: called CC=1 SCAR=0.0
Pod documented
76 41 my @array = @_;
77 159 ▶ sub { @array ? shift @array : () }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@array ? : 124 35 159
78 41 }
79  
80 96 ▶sub fold ($) {
Subroutine fold: called CC=6 SCAR=17.9
Pod documented
81 96 my ( $i ) = @_;
82 96 my @v;
83 96 ▶ unless (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 23 73 96
84 23 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 6 17 23
85 6 my $n = 0;
86 6 ▶ $n += @v while @v = $i->();
Branch: 1 branch
BranchTrueFalseTotal
@v = &$i() and do { $n += @v } 13 6 19
87 6 return $n;
88 } else {
89 17 ▶ undef while @v = $i->();
Branch: 1 branch
BranchTrueFalseTotal
@v = &$i() and do { undef } 25 17 42
90 17 return;
91 }
92 }
93 73 my @r;
94 73 ▶ push @r, @v while @v = $i->();
Branch: 1 branch
BranchTrueFalseTotal
@v = &$i() and do { push @r, @v } 30160 73 30233
95 73 @r;
96 }
97  
98 # stream consumers (lazy)
99 4 ▶sub takeWhile (&$) {
Subroutine takeWhile: called CC=1 SCAR=0.0
Pod documented
100 4 my ($cond, $i ) = @_;
101 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
102 7 ▶ ( my @v = $i->() ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@v) = &$i() or return 6 1 7
103 6 ▶ return $cond->() ? @v : () for @v;
Branch: 1 branch
BranchTrueFalseTotal
&$cond() ? : 3 3 6
104 }
105 4 }
106  
107 3 ▶sub filter (&$) {
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
108 3 my ( $cond, $i ) = @_;
109 3 $i = _buffer $i;
110 10006 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
111 10006 while (1) {
112 20009 ▶ ( my @v = $i->() ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@v) = &$i() or return 20007 2 20009
113 20007 ▶ $cond->() and return @v for @v;
Branch: 1 branch
BranchTrueFalseTotal
&$cond() and return @v 10004 10003 20007
114 }
115 }
116 3 }
117  
118 72 ▶sub take ($$) {
Subroutine take: called CC=1 SCAR=0.0
Pod documented
119 72 my ( $n, $i ) = @_;
120 72 $i = _buffer $i;
121 30212 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
122 30212 ▶ $n-- > 0 or return;
Branch: 1 branch
BranchTrueFalseTotal
$n-- > 0 or return 30179 33 30212
123 30179 $i->()
124 }
125 72 }
126  
127 12 ▶sub drop ($$) {
Subroutine drop: called CC=1 SCAR=0.0
Pod documented
128 12 my ( $n, $i ) = @_;
129 12 $i = _buffer $i;
130 12 fold take $n, $i;
131 12 $i;
132 }
133  
134 3 ▶sub apply (&$) {
Subroutine apply: called CC=1 SCAR=0.0
Pod documented
135 3 my ( $code, $i ) = @_;
136 10007 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 10007 ▶ ( my @v = $i->() ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@v) = &$i() or return 10006 1 10007
138 10006 (map $code->(), @v)[0];
139 }
140 3 }
141  
142 # stream consumers (exhaustive)
143 5 ▶sub now (&$) {
Subroutine now: called CC=2 SCAR=6.9
Pod documented
144 5 my ( $code, $i ) = @_;
145 5 my @b;
146 5 while (1) {
147 32 ▶ ( my @v = $i->() ) or return pop @b;
Branch: 1 branch
BranchTrueFalseTotal
my(@v) = &$i() or return pop @b 27 5 32
148 27 @b = map $code->(), @v;
149 }
150 }
151  
152 0 ▶sub records {
Subroutine records: not called CC=1 SCAR=6.9
Pod documented
153 0 my $source = shift;
154 0 ▶ sub { <$source> // () }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
readline $source // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
readline $source // ()
readline $source ()
Truth table0%0/3
A // B
A= readline $sourceB= ()
ABresult
1X1
011
000
155 0 }
156  
157  
158 3 ▶sub lines {
Subroutine lines: called CC=1 SCAR=0.0
Pod documented
159 3 my $fh = &open_file;
160 3 my $line;
161 12 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
162 12 ▶ return unless defined ( $line = <$fh> );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($line = readline $fh) 3 9 12
163 9 chomp $line;
164 9 $line;
165 }
166 3 }
167  
168 1 ▶sub concat {
Subroutine concat: called CC=1 SCAR=0.0
Pod documented
169 1 my ($s, @ss) = @_; # streams
170 1 my @v;
171 11 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
172 11 while (1) {
173 12 ▶ @v = $s->() and return @v;
Branch: 1 branch
BranchTrueFalseTotal
@v = &$s() and return @v 10 2 12
174 2 ▶ $s = shift @ss or return ();
Branch: 1 branch
BranchTrueFalseTotal
$s = shift @ss or return () 1 1 2
175 }
176 }
177 1 }
178  
179 0 ▶sub concatC ($) {
Subroutine concatC: not called CC=2 SCAR=17.9
Pod documented
180 0 my $ss = shift; # stream
181 0 ▶ my ($s) = $ss->() or return sub {()};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
my($s) = &$ss() or return sub { (); } 0 0 0
182 0 my @v;
183 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
184 0 while (1) {
185 0 ▶ @v = $s->() and return @v;
Branch: 1 branch
BranchTrueFalseTotal
@v = &$s() and return @v 0 0 0
186 0 ▶ $s = $ss->() or return ();
Branch: 1 branch
BranchTrueFalseTotal
$s = &$ss() or return () 0 0 0
187 }
188 }
189 0 }
190  
191 0 ▶sub concatM (&$) {
Subroutine concatM: not called CC=1 SCAR=6.9
Pod documented
192 0 my ( $apply, $stream ) = @_;
193 0 ▶ concatC apply {$apply->()} $stream;
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 }
195  
196 # stream generators
197 2 ▶sub cycle (@) {
Subroutine cycle: called CC=2 SCAR=7.0
Pod documented
198 2 ▶ (my @ring = @_) or return sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
my(@ring) = or return sub { } 2 0 2
199 2 my $index = -1;
200 10010 ▶ sub { $ring[ ( $index += 1 ) %= @ring ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
201 2 }
202  
203 14 ▶sub range {
Subroutine range: called CC=6 SCAR=18.0
Pod documented
204 14 ▶ my $begin = shift // croak "range begin undefined";
Condition50%1/2
shift() // croak("range begin undefined")
l!l
140
MC/DC0%0/1
shift() // croak("range begin undefined")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
205 14 my $end = shift;
206 14 ▶ my $step = shift // 1;
Condition100%2/2
shift() // 1
l!l
95
MC/DC100%1/1
shift() // 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
207  
208 0 ▶ return sub { () } if $step == 0;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 14 14
209  
210 14 $begin -= $step;
211 14 ▶ if (defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end) { } 9 5 14
212 9 ▶ if ($step > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step > 0) { } 6 3 9
213 22 ▶ sub { (($begin += $step) <= $end) ? ($begin) : () }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
($begin += $step) <= $end ? : 16 6 22
214 6 } else {
215 8 ▶ sub { (($begin += $step) >= $end) ? ($begin) : () }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
($begin += $step) >= $end ? : 5 3 8
216 3 }
217 } else {
218 15 ▶ sub { ($begin += $step) }
Subroutine __ANON__: called CC=1 SCAR=0.0
219 5 }
220 }
221  
222  
223 6 ▶sub tuple ($$) {
Subroutine tuple: called CC=2 SCAR=6.9
Pod undocumented
224 6 my ( $n, $i ) = @_;
225 6 ▶ croak "$n is not a valid parameter for tuple()" if $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 3 3 6
226 3 $i = _buffer $i;
227 9 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
228 9 my @v = fold take $n, $i;
229 9 ▶ @v ? \@v : ();
Branch: 1 branch
BranchTrueFalseTotal
@v ? : 6 3 9
230 }
231 3 }
232  
233 0 ▶sub nth {
Subroutine nth: not called CC=1 SCAR=6.9
Pod documented
234 0 my ( $n, $s ) = @_;
235 0 $n--;
236 0 take 1, drop $n, $s
237 }
238  
239 1 ▶sub chunksOf ($$;$) {
Subroutine chunksOf: called CC=3 SCAR=11.0
Pod documented
240  
241 1 my ( $n, $src, $offset ) = @_;
242 1 ▶ $n > 1 or die "chunksOf must be at least 1 (don't forget unfold)";
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 or die "chunksOf must be at least 1 (don't forget unfold)" 1 0 1
243 1 ▶ $offset //= 0;
Condition50%1/2
$offset //= 0
l!l
01
MC/DC0%0/1
$offset //= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
244  
245 1 my ( $end , $exhausted , $from, $to )
246 = ( $#$src , 0 );
247  
248 4 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
249 4 ▶ return if $exhausted;
Branch: 1 branch
BranchTrueFalseTotal
if $exhausted 2 2 4
250  
251 2 ( $from , $offset )=
252 ( $offset , $offset + $n );
253  
254 2 ▶ $end <= ($to = $offset - 1) and do {
Branch: 1 branch
BranchTrueFalseTotal
$end <= ($to = $offset - 1) and do { $exhausted = 1; $to = $end } 1 1 2
255 1 $exhausted=1;
256 1 $to = $end;
257 };
258  
259 2 [ @{$src}[$from..$to] ];
260 }
261 1 }
262  
263  
264 14 1;
265  
« Perlude/Lazy.pm ↑ Summary Perlude/Open.pm »