Perlude/Lazy.pm

filter: Statementstmt 93.6% 118 / 126 Branchbran 88.5% 62 / 70 Conditioncond 80.0% 8 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 94.7% 36 / 38 Podpod 0.0% 0 / 15 totaltotal 86.0% 228 / 265 CC 42 SCAR 12.6
CC
42
Cov
91%
__ANON__
8.0
range
6.0
fold
6.0
CRAP 3.5
↑ Summary Perlude.pm »
1 package Perlude::Lazy;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Exporter qw< import >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 our $VERSION = '0.0';
8 11 our @EXPORT = qw<
9  
10 enlist unfold
11 fold
12 takeWhile take drop
13 filter apply
14 now
15 cycle range
16 tuple
17 lines
18 concat
19  
20 >;
21  
22 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # End-of-list value: always return itself, with no data
25 {
26 11 my $NIL;
27 7 ▶ $NIL = sub { $NIL };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 54 ▶ sub NIL() { $NIL }
Subroutine NIL: called CC=1 SCAR=0.0
Pod undocumented
29 }
30  
31 # interface with the Perl world
32 11 ▶sub enlist (&) {
Subroutine enlist: called CC=1 SCAR=0.0
Pod undocumented
33 68 my ($i) = @_;
34 68 my ( $l, @b );
35 40286 ▶ $l = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
36 40286 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 44 40242 40286
37 44 my $n = shift;
38 44 ▶ return ( $l, @b[ 0 .. $n - 1 ] ) if @b >= $n; # there's enough
Branch: 1 branch
BranchTrueFalseTotal
if @b >= $n 29 15 44
39 15 push @b, my @v = $i->(); # need more
40 15 ▶ push @b, @v = $i->() while @b < $n && @v; # MOAR
Branch: 1 branch
BranchTrueFalseTotal
@b < $n and @v and do { push @b, @v = &$i() } 260 15 275
Condition100%3/3
@b < $n and @v
!ll&&!rl&&r
87260
MC/DC100%2/2
@b < $n and @v
@b < $n @v
Truth table100%3/3
A and B
A= @b < $nB= @v
ABresult
0X0
100
111
41 15 ▶ return ( $l, @b < $n ? @b : @b[ 0 .. $n - 1 ] ); # give it a peek
Branch: 1 branch
BranchTrueFalseTotal
@b < $n ? : 7 8 15
42 }
43 else {
44 40242 ▶ return ( $l, shift @b ) if @b; # use the buffer first
Branch: 1 branch
BranchTrueFalseTotal
if @b 18 40224 40242
45 40224 push @b, $i->(); # obtain more items
46 40224 ▶ return @b ? ( $l, shift @b ) : NIL;
Branch: 1 branch
BranchTrueFalseTotal
@b ? : 40175 49 40224
47 }
48 68 };
49 }
50  
51 1 ▶sub concat {
Subroutine concat: called CC=1 SCAR=0.0
Pod undocumented
52 1 my ($l, @ls)= @_;
53 1 my @v;
54 my $r;
55 11 ▶ $r = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
56 11 ▶ while ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 12 1 13
57 12 ( $l, @v ) = $l->();
58 12 ▶ return ($r,@v) if @v;
Branch: 1 branch
BranchTrueFalseTotal
if @v 10 2 12
59 2 $l = shift @ls;
60 }
61 1 };
62 1 $r
63 }
64  
65 45 ▶sub unfold (@) {
Subroutine unfold: called CC=1 SCAR=0.0
Pod undocumented
66 45 my @array = @_;
67 45 ▶ enlist { @array ? shift @array : () };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@array ? : 142 43 185
68 }
69  
70 83 ▶sub fold ($) {
Subroutine fold: called CC=6 SCAR=17.9
Pod undocumented
71 83 my ($l) = @_;
72 83 my @v;
73 83 ▶ unless (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 23 60 83
74 23 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 6 17 23
75 6 my $n = 0;
76 6 ▶ $n += @v while 1 < ( ( $l, @v ) = $l->() );
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, @v) = &$l()) and do { $n += @v } 13 6 19
77 6 return $n;
78 }
79 else {
80 # The real lazy one: when called in scalar context, values are
81 # ignored:
82 # undef while defined ( $l = $l->() );
83 # But producers must be able to handle that
84 # So keep that for later and use the eager implementation for now
85 17 ▶ undef while 1 < ( ( $l, @v ) = $l->() );
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, @v) = &$l()) and do { undef } 25 17 42
86 17 return;
87 }
88 }
89 60 my @r;
90 60 ▶ push @r, @v while 1 < ( ( $l, @v ) = $l->() );
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, @v) = &$l()) and do { push @r, @v } 30128 60 30188
91 60 @r;
92 }
93  
94 # stream consumers (lazy)
95 5 ▶sub takeWhile (&$) {
Subroutine takeWhile: called CC=1 SCAR=0.0
Pod undocumented
96 5 my ( $cond, $l ) = @_;
97 5 my $m;
98 10 ▶ $m = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
99 10 ▶ 1 < ( ( $l, my @v ) = $l->() ) or return ($l);
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, my @v) = &$l()) or return $l 9 1 10
100 0 ▶ return $cond->() ? ( $m, @v ) : ( sub { ( $l, @v ) } ) for @v;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
&$cond() ? : 5 4 9
101 5 };
102 }
103  
104 3 ▶sub filter (&$) {
Subroutine filter: called CC=1 SCAR=0.0
Pod undocumented
105 3 my ( $cond, $l ) = @_;
106 3 my $m;
107 10006 ▶ $m = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
108 10006 while (1) {
109 20009 ▶ 1 < ( ( $l, my @v ) = $l->() ) or return ($l);
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, my @v) = &$l()) or return $l 20007 2 20009
110 20007 ▶ $cond->() and return ($m, @v) for @v;
Branch: 1 branch
BranchTrueFalseTotal
&$cond() and return $m, @v 10004 10003 20007
111 }
112 3 };
113 }
114  
115 71 ▶sub take ($$) {
Subroutine take: called CC=1 SCAR=0.0
Pod undocumented
116 71 my ( $n, $l ) = @_;
117 71 my $m;
118 30206 ▶ $m = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
119 30206 ▶ $n-- > 0 or return ($l);
Branch: 1 branch
BranchTrueFalseTotal
$n-- > 0 or return $l 30174 32 30206
120 30174 ▶ 1 < ( ( $l, my @v ) = $l->() ) or return ($l);
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, my @v) = &$l()) or return $l 30135 39 30174
121 30135 ( $m, @v );
122 }
123 71 }
124  
125 12 ▶sub drop ($$) {
Subroutine drop: called CC=1 SCAR=0.0
Pod undocumented
126 12 my ( $n, $l ) = @_;
127 12 fold take $n, $l;
128 12 $l;
129 }
130  
131 2 ▶sub apply (&$) {
Subroutine apply: called CC=1 SCAR=0.0
Pod undocumented
132 2 my ( $code, $l ) = @_;
133 2 my $m;
134 10004 ▶ $m = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
135 10004 ▶ 1 < ( ( $l, my @v ) = $l->() ) or return $l;
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, my @v) = &$l()) or return $l 10003 1 10004
136 10003 ( $m, map $code->(), @v );
137 }
138 2 }
139  
140 # stream consumers (exhaustive)
141 4 ▶sub now (&$) {
Subroutine now: called CC=2 SCAR=6.9
Pod undocumented
142 4 my ( $code, $l ) = @_;
143 4 my @b;
144 4 while (1) {
145 29 ▶ 1 < ( ( $l, my @v ) = $l->() ) or return pop @b;
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, my @v) = &$l()) or return pop @b 25 4 29
146 25 @b = map $code->(), @v;
147 }
148 }
149  
150 # stream generators
151 2 ▶sub cycle (@) {
Subroutine cycle: called CC=2 SCAR=7.0
Pod undocumented
152 2 ▶ (my @ring = @_) or return NIL;
Branch: 1 branch
BranchTrueFalseTotal
my(@ring) = or return &NIL() 2 0 2
153 2 my $index = -1;
154 10010 ▶ enlist { $ring[ ( $index += 1 ) %= @ring ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
155 2 }
156  
157 14 ▶sub range ($$;$) {
Subroutine range: called CC=6 SCAR=18.0
Pod undocumented
158 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
159 14 my $end = shift;
160 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
161  
162 14 ▶ return NIL if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 14 14
163  
164 14 $begin -= $step;
165 14 my $l;
166 return $l = defined $end
167 ? $step > 0
168 22 ▶ ? sub { ( ( $begin += $step ) <= $end ) ? ( $l, $begin ) : ($l) }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
($begin += $step) <= $end ? : 16 6 22
169 8 ▶ : sub { ( ( $begin += $step ) >= $end ) ? ( $l, $begin ) : ($l) }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
($begin += $step) >= $end ? : 5 3 8
170 14 ▶ : sub { ( $l, $begin += $step ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
defined $end ? : 9 5 14
$step > 0 ? : 6 3 9
171 }
172  
173  
174 6 ▶sub tuple ($$) {
Subroutine tuple: called CC=2 SCAR=6.9
Pod undocumented
175 6 my ( $n, $l ) = @_;
176 6 ▶ croak "$n is not a valid parameter for tuple()" if $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 3 3 6
177 3 my $m;
178 9 ▶ $m = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
179 9 $l = take $n, $l;
180 9 my (@r, @v);
181 9 ▶ push @r, @v while 1 < ( ( $l, @v ) = $l->() );
Branch: 1 branch
BranchTrueFalseTotal
1 < (($l, @v) = &$l()) and do { push @r, @v } 18 9 27
182 9 ▶ @r ? ( $m, \@r ) : ( $l )
Branch: 1 branch
BranchTrueFalseTotal
@r ? : 6 3 9
183 }
184 3 }
185  
186 1 ▶sub lines {
Subroutine lines: called CC=3 SCAR=15.7
Pod undocumented
187 # private sub that coerce path to handles
188 1 ▶ state $fh_coerce = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
189 1 my $v = shift;
190 1 ▶ return $v if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 1 0 1
191 0 open my ($fh),$v;
192 0 $fh;
193 1 };
194 1 my $fh = $fh_coerce->( pop );
195  
196 # only 2 forms accepted for the moment
197 # form 1: lines 'file'
198 1 ▶ @_ or return enlist { <$fh> // () };
Subroutine __ANON__: called CC=2 SCAR=7.4
Branch: 1 branch
BranchTrueFalseTotal
@_ or return &enlist(sub { readline $fh // (); } ) 0 1 1
Condition66%2/3
readline $fh // ()
l!l&&r!l&&!r
301
MC/DC50%1/2
readline $fh // ()
readline $fh ()
Truth table66%2/3
A // B
A= readline $fhB= ()
ABresult
1X1
011
000
199  
200 # confess if not 2nd form
201 0 ▶ $_[0] eq 'chomp' or confess 'cannot handle parameters ' , join ',', @_ ;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "chomp" or confess("cannot handle parameters ", join(",", @_)) 0 0 0
202  
203 # lines chomp => 'file'
204 0 ▶ enlist {
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 ▶ defined (my $v = <$fh>) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $v = readline $fh) or return 0 0 0
206 0 chomp $v;
207 0 $v;
208 }
209  
210 0 }
211  
212 11 1;
213  
214 =head1 Perlude::Lazy
215  
216 An experimentation of implementing real lazy lists in Perl5.
217 For real world usecases, please use Perlude instead.
218  
219 =head1 SYNOPSIS
220  
221 Haskell prelude miss you when you write perl stuff? Perlude is a port of the
222 most common keywords. Some other keywords where added when there is no haskell
223 equivalent.
224  
225 Example: in haskell you can write
226  
227 nat = [0..]
228 is_even x = ( x `mod` 2 ) == 0
229 evens = filter is_even
230 main = mapM_ print
231 $ take 10
232 $ evens nat
233  
234 in perlude, the same code will be:
235  
236 use Perlude;
237 my $nat = enlist { state $x = 0; $x++ };
238 sub is_even { ($_ % 2) == 0 }
239 sub evens { filter {is_even} shift }
240 traverse {say} take 10, evens $nat
241  
242 =head1 FUNCTIONS
243  
244 all the Perlude documentation is relevent. just replace sub by enlist
245  
246 =cut
247  
↑ Summary Perlude.pm »