blib/lib/List/Gen/Haskell.pm

filter: Statementstmt 40.0% 100 / 250 Branchbran 4.5% 2 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 16.0% 14 / 87 Podpod 50.0% 1 / 2 totaltotal 30.5% 117 / 383 SCAR 54.4
CC
27
Cov
35%
__ANON__
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 230.0
« lib/List/Gen/Lazy.pm ↑ Summary blib/lib/List/Gen/Lazy/Builtins.pm »
1 package List::Gen::Haskell;
2 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶ use lib '../../';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use List::Gen::Lazy '*';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 sub DEBUG () {0}
8 1 our @EXPORT = qw (lazy L lazyx Lx fn now seq map_ unzip unzipn x_xs);
9 1 our @EXPORT_OK;
10 my $setup_export = sub {
11 1 ▶ @EXPORT_OK = keys %{{map {$_ => 1} @EXPORT, @List::Gen::EXPORT_OK}}
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 };
13 1 our %EXPORT_TAGS = (base => \@EXPORT, all => \@EXPORT_OK);
14 BEGIN {
15 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ *import = *List::Gen::import;
Subroutine BEGIN: called CC=2 SCAR=6.9
17 1 *{uc $_} = *{'List::Gen::'.uc} for 'version'
18 }
19 1 our ($a, $b);
20 1 ▶ no if ($] > 5.012), warnings => 'illegalproto';
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22
23 =head1 NAME
24
25 List::Gen::Haskell - the haskell prelude in perl5
26
27 =head1 SYNOPSIS
28
29 this module provides most of the functions in the haskell prelude that pertain
30 to working with lists.
31
32 # haskell: fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
33
34 use List::Gen::Haskell;
35
36 $fibs = lazy 0, 1, zipWith {&sum} $fibs, tail $fibs;
37
38 print "@$fibs[0 .. 10]\n"; # prints '0 1 1 2 3 5 8 13 21 34 55'
39
40 C< lazy > provides the generic behavior of haskell's lazy list concatenation
41 (the C< : > and C< ++ > operators). none of its elements are touched until they
42 are needed. in general, all the functions in this package defer their execution
43 until something is required of them. they also only touch their arguments at
44 the latest possible time, which is how the co-recursion above works.
45
46 the functions in this module are a bit more flexible (perlish) than those in
47 haskell. in most cases where a function expects a single generator, a list of
48 values and/or generators can be provided, which will be preprocessed by
49 C< lazy > into a single generator.
50
51 when loaded, most of the functions in this package become methods for all
52 generators. if a method of the same name already exists, the method from this
53 package will be prefixed with C< hs_ >. so C< filter > is a method named
54 C<< ->hs_filter(...) >>
55
56 this library currently does not have the best performance due to over-caching
57 of many internal generators. a future update will address this by replacing
58 those generators with cache-less generator streams.
59
60 =cut
61
62 sub let {
63 my ($name, $code) = splice @_, 0, 2;
64
65 $_ = &fn (ref eq 'ARRAY' ? @$_ : $_, @_) for $code;
66
67 if (DEBUG) {
68 my $next = $code;
69 $code = sub {Carp::cluck "$name @_"; goto &$next}
70 }
71 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 *$name = *{ucfirst $name} = $code;
73
74 *{'List::Gen::erator::'
75 .('hs_' x defined &{'List::Gen::erator::'.$name})
76 .$name} = sub {
77 0 ▶ push @_, shift; goto &$code
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 };
79 push @EXPORT, $name, ucfirst $name;
80 }
81
82 my $pred = sub {
83 0 ▶ local *_ = (@_ > 1 ? \$_[1] : \undef);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
84 0 $_[0]->($_)
85 1 };
86
87 0 ▶ sub map_ (&) {&map(@_, $_)}
Subroutine map_: not called CC=1 SCAR=6.9
Pod undocumented
88
89
90 =head1 FUNCTIONS
91
92 all of these functions are available with a ucfirst name, since many clash with
93 perl builtin names.
94
95 =head2 Utility Operations
96
97 =over 4
98
99 =item x_xs C< [GENERATOR] >
100
101 C< x_xs > is a convenience function that returns the head and tail of a passed
102 in generator. C< x_xs > uses C< $_ > without an argument.
103
104 =cut
105
106 0 ▶ sub x_xs (;$) {(@_, $_)[0]->x_xs}
Subroutine x_xs: not called CC=1 SCAR=6.9
Pod documented
107
108
109 =item seq C< LIST >
110
111 forces immediate evaluation of the elements in C< LIST > and returns the list
112
113 =cut
114
115 1 ▶ BEGIN {*seq = \&now}
Subroutine BEGIN: called CC=1 SCAR=0.0
116
117
118 =item flip C< {CODE} >
119
120 C< flip > converts C< CODE > into a function that takes it's arguments reversed
121
122 my $idx = \&head . flip \&drop;
123
124 $idx->($gen, 5) == $gen->$idx(5) == $gen->get(5)
125
126 =cut
127
128 0 ▶ let flip => sub (&) {goto &List::Gen::Function::flip};
Subroutine __ANON__: not called CC=1 SCAR=1.2
129
130
131 =back
132
133 =head2 List operations
134
135 =over 4
136
137 =item Map C< {CODE} LIST >
138
139 Map f xs is the list obtained by applying f to each element of xs, i.e.,
140
141 map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn]
142 map f [x1, x2, ...] == [f x1, f x2, ...]
143
144 $x = &map(sub {$_**2}, $gen);
145 $x = Map {$_**2} $gen;
146
147 that usage is the same as C< List::Gen::gen >, but this is something that
148 C< gen > can't do:
149
150 my $pow_2 = Map {$_**2}; # partial application, needs at least 1 more
151 # argument to evaluate, but can be passed a list
152 my $ints = <0..>;
153
154 my $squares = $ints->$pow_2;
155
156 say "@$squares[0 .. 10]"; # 0 1 4 9 16 25 36 49 64 81 100
157
158 and this:
159
160 my $src;
161 my $square_of_src = Map {$_ ** 2} $src;
162
163 $src = <1.. by 2>;
164
165 say "@$square_of_src[0 .. 4]"; # 1 9 25 49 81
166
167 =cut
168
169 0 ▶ let map => sub (&@) {&gen(shift, &lazy)};
Subroutine __ANON__: not called CC=1 SCAR=1.2
170
171
172 =item filter C< {CODE} LIST >
173
174 filter, applied to a predicate and a list, returns the list of those elements
175 that satisfy the predicate; i.e.,
176
177 filter p xs = [ x | x <- xs, p x]
178
179 =cut
180
181 0 ▶ let filter => sub (&@) {&List::Gen::filter(shift, &lazy)};
Subroutine __ANON__: not called CC=1 SCAR=1.2
182
183
184 =item head C< GENERATOR >
185
186 Extract the first element of a list, which must be non-empty.
187
188 =cut
189
190 0 ▶ let head => sub {$_[0]->head};
Subroutine __ANON__: not called CC=1 SCAR=1.2
191
192
193 =item Last C< GENERATOR >
194
195 Extract the last element of a list, which must be finite and non-empty.
196
197 =cut
198
199 0 ▶ let last => sub {$_[0]->last};
Subroutine __ANON__: not called CC=1 SCAR=1.2
200
201
202 =item tail C< GENERATOR >
203
204 Extract the elements after the head of a list, which must be non-empty.
205
206 =cut
207
208 0 ▶ let tail => sub {$_[0]->tail};
Subroutine __ANON__: not called CC=1 SCAR=1.2
209
210
211 =item init C< GENERATOR >
212
213 Return all the elements of a list except the last one. The list must be
214 non-empty.
215
216 =cut
217
218 let init => sub {
219 0 ▶ my $xs = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 take($xs->apply->size - 1, $xs)
221 1 };
222
223
224 =item null C< GENERATOR >
225
226 Test whether a list is empty.
227
228 =cut
229
230 0 ▶ let null => sub {$_[0]->apply->size == 0};
Subroutine __ANON__: not called CC=1 SCAR=1.2
231
232
233 =item Length C< GENERATOR >
234
235 =cut
236
237 0 ▶ let length => sub {$_[0]->apply->size};
Subroutine __ANON__: not called CC=1 SCAR=1.2
238
239
240 =item Reverse C< LIST >
241
242 reverse xs returns the elements of xs in reverse order. xs must be finite.
243
244 =cut
245
246 0 ▶ let reverse => sub {&list->reverse};
Subroutine __ANON__: not called CC=1 SCAR=1.2
247
248
249 =back
250
251 =head2 Reducing lists (folds)
252
253 =over 4
254
255 =item foldl C< {CODE} ITEM LIST >
256
257 foldl, applied to a binary operator, a starting value (typically the
258 left-identity of the operator), and a list, reduces the list using the binary
259 operator, from left to right:
260
261 foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
262
263 The list must be finite.
264
265 =item foldl1 C< {CODE} LIST >
266
267 C< foldl1 > is a variant of C< foldl > that has no starting value argument, and
268 thus must be applied to non-empty lists.
269
270 =cut
271
272 let foldl => sub (&@@) {
273 0 ▶ my ($code, $z) = splice @_, 0, 2;
Subroutine __ANON__: not called CC=1 SCAR=6.9
274 &lazy->do(sub {
275 0 ▶ $z = $code->(now $z, $_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 0 });
277 0 $z
278 1 };
279
280 let foldl1 => sub (&@) {
281 0 ▶ my $code = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 0 my $xs = &lazy;
283 0 my $init = 1;
284 0 my $x;
285 $xs->do(sub {
286 0 ▶ if ($init) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($init) { } 0 0 0
287 0 undef $init;
288 0 $x = now $_;
289 } else {
290 0 $x = $code->(now $x, $_)
291 }
292 0 });
293 0 $x
294 1 };
295
296
297 =item foldr C< {CODE} ITEM LIST >
298
299 foldr, applied to a binary operator, a starting value (typically the
300 right-identity of the operator), and a list, reduces the list using the binary
301 operator, from right to left:
302
303 foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
304
305 =item foldr1 C< {CODE} LIST >
306
307 C< foldr1 > is a variant of C< foldr > that has no starting value argument, and
308 thus must be applied to non-empty lists.
309
310 =cut
311
312 let foldr => sub (&@@) {
313 0 ▶ my ($code, $z) = splice @_, 0, 2;
Subroutine __ANON__: not called CC=1 SCAR=6.9
314 &lazy->reverse->do(sub {
315 0 ▶ $z = $code->(now $_, $z)
Subroutine __ANON__: not called CC=1 SCAR=6.9
316 0 });
317 0 $z
318 1 };
319
320 let foldr1 => sub (&@) {
321 0 ▶ my $code = shift;
Subroutine __ANON__: not called CC=2 SCAR=16.1
322 0 my $xs = &lazy;
323 0 $xs->apply;
324 0 my $i = $xs->size - 1;
325 0 my $x = $xs->get($i--);
326 0 ▶ while ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 0 0
327 0 $x = $code->(now $xs->get($i--), $x)
328 }
329 $x
330 0 };
331
332
333 =back
334
335 =head3 Special folds
336
337 =over 4
338
339 =item And C< LIST >
340
341 and returns the conjunction of a Boolean list. For the result to be True, the
342 list must be finite; False, however, results from a False value at a finite
343 index of a finite or infinite list.
344
345 =cut
346
347 let and => sub {
348 0 ▶ not &lazy->do(sub {$_ or done 1})
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ or done(1) 0 0 0
349 0 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
350
351
352 =item Or C< LIST >
353
354 or returns the disjunction of a Boolean list. For the result to be False, the
355 list must be finite; True, however, results from a True value at a finite index
356 of a finite or infinite list.
357
358 =cut
359
360 let or => sub {
361 0 ▶ &lazy->do(sub {$_ and done 1});
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ and done(1) 0 0 0
362 1 };
363
364
365 =item any C< {CODE} LIST >
366
367 Applied to a predicate and a list, any determines if any element of the list
368 satisfies the predicate.
369
370 =cut
371
372 let any => sub (&@) {
373 0 ▶ my $p = shift;
Subroutine __ANON__: not called CC=1 SCAR=4.6
374 0 ▶ &lazy->do(sub {$p->($_) and done 1})
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&$p($_) and done(1) 0 0 0
375 0 };
376
377
378 =item all C< {CODE} LIST >
379
380 Applied to a predicate and a list, all determines if all elements of the list
381 satisfy the predicate.
382
383 =cut
384
385 let all => sub (&@) {
386 0 ▶ my $p = shift;
Subroutine __ANON__: not called CC=1 SCAR=4.6
387 0 ▶ not &lazy->do(sub {$p->($_) or done 1})
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
&$p($_) or done(1) 0 0 0
388 0 };
389
390
391 =item sum C< LIST >
392
393 The sum function computes the sum of a finite list of numbers.
394
395 =cut
396
397 0 ▶ let sum => sub {&lazy->sum};
Subroutine __ANON__: not called CC=1 SCAR=1.2
398
399
400 =item product C< LIST >
401
402 The product function computes the product of a finite list of numbers.
403
404 =cut
405
406 0 ▶ let product => sub {&lazy->product};
Subroutine __ANON__: not called CC=1 SCAR=1.2
407
408
409 =item concat C< GENERATOR >
410
411 Concatenate a list of lists.
412
413 =cut
414
415 let concat => sub {
416 0 ▶ my $xs = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
417 0 my $xp = 0;
418 0 my $self;
419 my $at;
420 0 my $sequence;
421 0 ▶ my $ret = mutable gen {$at = $_; $sequence->size > $_ or done; $sequence->get($_)};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$sequence->size > $_ or done() 0 0 0
422 $sequence = sequence mutable gen {
423 0 ▶ $xp < @$xs or done;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$xp < @$xs or done() 0 0 0
424 0 my $seq = $self->sequence;
425 0 ▶ splice @$seq, $#$seq, 0, map {List::Gen::isagen or &makegen(\@$_)} $$xs[$xp++];
Branch: 1 branch
BranchTrueFalseTotal
&List::Gen::isagen() or &makegen(\@$_) 0 0 0
426 0 $self->rebuild;
427 0 $sequence->get($at)
428 0 } 1;
429 0 $self = tied @$sequence;
430 0 $ret
431 1 };
432
433
434 =item concatMap C< {CODE} LIST >
435
436 Map a function over a list and concatenate the results.
437
438 =cut
439
440 0 ▶ let concatMap => sub (&@) {concat(&gen(shift, &lazy))};
Subroutine __ANON__: not called CC=1 SCAR=1.2
441
442
443 =item maximum C< LIST >
444
445 maximum returns the maximum value from a list, which must be non-empty, finite,
446 and of an ordered type. It is a special case of maximumBy, which allows the
447 programmer to supply their own comparison function.
448
449 =cut
450
451 0 ▶ let maximum => sub {&lazy->max};
Subroutine __ANON__: not called CC=1 SCAR=1.2
452
453
454 =item minimum C< LIST >
455
456 minimum returns the minimum value from a list, which must be non-empty, finite,
457 and of an ordered type. It is a special case of minimumBy, which allows the
458 programmer to supply their own comparison function.
459
460 =cut
461
462 0 ▶ let minimum => sub {&lazy->min};
Subroutine __ANON__: not called CC=1 SCAR=1.2
463
464
465 =back
466
467 =head2 Building lists
468
469 =head3 Scans
470
471 =over 4
472
473 =item scanl C< {CODE} LIST >
474
475 scanl is similar to foldl, but returns a list of successive reduced values from
476 the left:
477
478 scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
479
480 Note that
481
482 last (scanl f z xs) == foldl f z xs.
483
484 =cut
485
486 let scanl => sub (&@) {
487 0 ▶ my $code = shift;
Subroutine __ANON__: not called CC=1 SCAR=5.6
488 0 my $gen = &lazy;
489 iterate_multi {
490 0 ▶ my $ret = $_ == 0 ? $gen->get(0) : $code->(self($_ - 1), $gen->get($_));
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_ == 0 ? : 0 0 0
491 0 ▶ done $ret if $_ + 1 >= $gen->size;
Branch: 1 branch
BranchTrueFalseTotal
if $_ + 1 >= $gen->size 0 0 0
492 0 $ret
493 }->recursive
494 0 };
495
496
497 =item scanr C< {CODE} LIST >
498
499 scanr is the right-to-left dual of scanl. Note that
500
501 head (scanr f z xs) == foldr f z xs.
502
503 =cut
504
505 let scanr => sub (&@) {
506 0 ▶ my $code = shift;
Subroutine __ANON__: not called CC=1 SCAR=5.6
507 0 my $gen = &lazy;
508 iterate_multi {
509 0 ▶ my $ret = $_ == 0 ? $gen->get($gen->size - 1) : $code->($gen->get($gen->size - ($_+1)), self($_ - 1));
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_ == 0 ? : 0 0 0
510 0 ▶ done $ret if $_ + 1 >= $gen->size;
Branch: 1 branch
BranchTrueFalseTotal
if $_ + 1 >= $gen->size 0 0 0
511 0 $ret
512 }->recursive
513 0 };
514
515
516 =back
517
518 =head3 Infinite lists
519
520 =over 4
521
522 =item iterate C< {CODE} ITEM >
523
524 iterate f x returns an infinite list of repeated applications of f to x:
525
526 iterate f x == [x, f x, f (f x), ...]
527
528 =cut
529
530 let iterate => sub (&@) {
531 0 ▶ my ($code, $x) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
532 0 ▶ &List::Gen::iterate(sub {$code->($_)})->from($x)->scalar;
Subroutine __ANON__: not called CC=1 SCAR=6.9
533 1 };
534
535
536 =item repeat C< ITEM >
537
538 repeat x is an infinite list, with x the value of every element.
539
540 =cut
541
542 let repeat => sub {
543 0 ▶ my $x = $_[0];
Subroutine __ANON__: not called CC=1 SCAR=3.5
544 0 ▶ gen {$x}
Subroutine __ANON__: not called CC=1 SCAR=6.9
545 0 };
546
547
548 =item hs_repeat C< ITEM >
549
550 my $repeat; $repeat = lazy $x, $repeat;
551
552 =cut
553
554 let hs_repeat => sub {
555 0 ▶ my ($x, $xs) = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
556 0 $xs = lazy $x, $xs
557 1 };
558
559
560 =item replicate C< NUM ITEM >
561
562 replicate n x is a list of length n with x the value of every element.
563
564 =cut
565
566 let hs_replicate => sub ($@) {
567 0 ▶ take(shift, &hs_repeat)
Subroutine __ANON__: not called CC=1 SCAR=6.9
568 1 };
569
570 let replicate => sub ($@) {
571 0 ▶ my ($n, $x) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
572 0 ▶ gen {$x} $n
Subroutine __ANON__: not called CC=1 SCAR=6.9
573 1 };
574
575
576 =item cycle C< LIST >
577
578 cycle ties a finite list into a circular one, or equivalently, the infinite
579 repetition of the original list. It is the identity on infinite lists.
580
581 =cut
582
583 0 ▶ let cycle => sub {&lazy->cycle};
Subroutine __ANON__: not called CC=1 SCAR=1.2
584
585
586 =item hs_cycle C< LIST >
587
588 hs_cycle ties a finite list into a circular one, or equivalently, the infinite
589 repetition of the original list. It is the identity on infinite lists.
590
591 it is defined in perl as:
592
593 my $cycle; $cycle = lazy $xs, $cycle;
594
595 =cut
596
597 let hs_cycle => sub {
598 0 ▶ my ($xs, $cycle) = &lazy;
Subroutine __ANON__: not called CC=1 SCAR=6.9
599 0 $cycle = lazy $xs, $cycle
600 1 };
601
602
603 =back
604
605 =head3 Sublists
606
607 =over 4
608
609 =item take C< NUM LIST >
610
611 take n, applied to a list xs, returns the prefix of xs of length n, or xs itself
612 if n > length xs:
613
614 take 3 [1,2,3,4,5] == [1,2,3]
615 take 3 [1,2] == [1,2]
616 take 3 [] == []
617 take (-1) [1,2] == []
618 take 0 [1,2] == []
619
620 =cut
621
622 let take => sub (@@) {
623 0 ▶ my $n = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
624 0 &lazy->take($n)
625 1 };
626
627
628 =item drop C< NUM LIST >
629
630 drop n xs returns the suffix of xs after the first n elements, or [] if n >
631 length xs:
632
633 drop 3 [1,2,3,4,5] == [4,5]
634 drop 3 [1,2] == []
635 drop 3 [] == []
636 drop (-1) [1,2] == [1,2]
637 drop 0 [1,2] == [1,2]
638
639
640 =cut
641
642 let drop => sub (@@) {
643 0 ▶ my $n = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
644 0 &lazy->drop($n)
645 1 };
646
647
648 =item splitAt C< NUM LIST >
649
650 splitAt n xs returns a tuple where first element is xs prefix of length n and
651 second element is the remainder of the list:
652
653 splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5])
654 splitAt 1 [1,2,3] == ([1],[2,3])
655 splitAt 3 [1,2,3] == ([1,2,3],[])
656 splitAt 4 [1,2,3] == ([1,2,3],[])
657 splitAt 0 [1,2,3] == ([],[1,2,3])
658 splitAt (-1) [1,2,3] == ([],[1,2,3])
659
660 It is equivalent to (take n xs, drop n xs).
661
662 =cut
663
664 0 ▶ let splitAt => sub {take(@_), drop(@_)}, 2, 2;
Subroutine __ANON__: not called CC=1 SCAR=1.2
665
666
667 =item takeWhile C< {CODE} LIST >
668
669 takeWhile, applied to a predicate p and a list xs, returns the longest prefix
670 (possibly empty) of xs of elements that satisfy p:
671
672 take_while (< 3) [1,2,3,4,1,2,3,4] == [1,2]
673 take_while (< 9) [1,2,3] == [1,2,3]
674 take_while (< 0) [1,2,3] == []
675
676 =cut
677
678 let takeWhile => sub (&@) {
679 0 ▶ my ($p, $xs) = (shift, &lazy);
Subroutine __ANON__: not called CC=1 SCAR=3.5
680 0 ▶ $xs->take_while(sub {$p->($_)})
Subroutine __ANON__: not called CC=1 SCAR=6.9
681 0 };
682
683
684 =item dropWhile C< {CODE} LIST >
685
686 dropWhile p xs returns the suffix remaining after take_while p xs:
687
688 dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3]
689 dropWhile (< 9) [1,2,3] == []
690 dropWhile (< 0) [1,2,3] == [1,2,3]
691
692 =cut
693
694 let dropWhile => sub (&@) {
695 0 ▶ my ($p, $xs) = (shift, &lazy);
Subroutine __ANON__: not called CC=1 SCAR=3.5
696 0 ▶ $xs->drop_while(sub {$p->($_)})
Subroutine __ANON__: not called CC=1 SCAR=6.9
697 0 };
698
699
700 =item span C< {CODE} LIST >
701
702 span, applied to a predicate p and a list xs, returns a tuple where first
703 element is longest prefix (possibly empty) of xs of elements that satisfy p and
704 second element is the remainder of the list:
705
706 span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4])
707 span (< 9) [1,2,3] == ([1,2,3],[])
708 span (< 0) [1,2,3] == ([],[1,2,3])
709
710 span p xs is equivalent to (takeWhile p xs, dropWhile p xs)
711
712 =cut
713
714 let span => sub (&@) {
715 0 ▶ my $p = shift;
Subroutine __ANON__: not called CC=1 SCAR=3.5
716 0 ▶ &lazy->span(sub {$p->($_)})
Subroutine __ANON__: not called CC=1 SCAR=6.9
717 0 }, 2, 2;
718
719
720 =item break C< {CODE} LIST >
721
722 break, applied to a predicate p and a list xs, returns a tuple where first
723 element is longest prefix (possibly empty) of xs of elements that do not satisfy
724 p and second element is the remainder of the list:
725
726 break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4])
727 break (< 9) [1,2,3] == ([],[1,2,3])
728 break (> 9) [1,2,3] == ([1,2,3],[])
729
730 break p is equivalent to span (not . p)
731
732 =cut
733
734 let break => sub (&@) {
735 0 ▶ my $p = shift;
Subroutine __ANON__: not called CC=1 SCAR=3.5
736 0 ▶ &lazy->span(sub {not $p->($_)})
Subroutine __ANON__: not called CC=1 SCAR=6.9
737 0 }, 2, 2;
738
739
740 =back
741
742 =head3 Searching lists
743
744 =over 4
745
746 =item elem C< ITEM LIST >
747
748 elem is the list membership predicate, usually written in infix form, e.g.,
749 x `elem` xs.
750
751 =cut
752
753 let elem => sub (@@) {
754 0 ▶ my $x = shift;
Subroutine __ANON__: not called CC=3 SCAR=21.6
755 0 ▶ $x eq $_ and return 1 for @{&lazy};
Branch: 1 branch
BranchTrueFalseTotal
$x eq $_ and return 1 0 0 0
756 return ()
757 0 };
758
759
760 =item notElem C< ITEM LIST >
761
762 notElem is the negation of elem.
763
764 =cut
765
766 let notElem => sub (@@) {
767 0 ▶ my $x = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
768 0 my $xs = &lazy;
769 0 ▶ $x eq $_ and return () for @$xs;
Branch: 1 branch
BranchTrueFalseTotal
$x eq $_ and return () 0 0 0
770 0 return 1
771 1 };
772
773
774 =back
775
776 =head3 Zipping and unzipping lists
777
778 =over 4
779
780 =item zip C< LIST >
781
782 zip takes 2+ lists and returns a single interleaved list. If one input list is
783 short, excess elements of the longer lists are discarded. unlike the haskell
784 version, the zip returns a flat generator.
785
786 C<zip> is the same as C<zipWith {\@_}>
787
788 =cut
789
790 let zip => sub (@@) {
791 0 ▶ List::Gen::zipwith {\@_} @_
Subroutine __ANON__: not called CC=1 SCAR=6.9
792 0 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
793
794
795 =item zipWith C< {CODE} LIST >
796
797 zipWith generalizes zip by zipping with the function given as the first
798 argument, instead of a tupling function. For example, zipWith (+) is applied to
799 two lists to produce the list of corresponding sums.
800
801 =cut
802
803 1 let zipWith => \&List::Gen::zipwith, 3;
804
805
806 =item zipWithAB C< {$a * $b} $gen1, $gen2 >
807
808 The zipWithAB function takes a function which uses C< $a > and C< $b >, as well
809 as two lists and returns a list analogous to zipWith.
810
811 =cut
812
813 1 let zipWithAB => \&List::Gen::zipWithAB, 3;
814
815
816 =item unzip C< GENERATOR >
817
818 unzip transforms a list into two lists of the even and odd elements.
819
820 zs = zip xs, ys
821 (xs, ys) == unzip zs
822
823 =cut
824
825 1 ▶ BEGIN {undef $_ for *unzip, *unzipn}
Subroutine BEGIN: called CC=2 SCAR=6.9
826 1 ▶ {no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
827 1 *unzip = &unzipn(2);
828 }
829
830
831 =item unzipn C< NUM GENERATOR >
832
833 The unzipn function is the n-dimentional precursor to C< unzip >
834
835 unzip xs = unzipn 2, xs
836
837 =cut
838
839 sub unzipn {
840 1 ▶ return \&unzipn unless @_;
Subroutine unzipn: called CC=3 SCAR=16.3
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
841 1 my $n = shift;
842 1 ▶ return bless sub {&unzipn($n, @_)} => 'List::Gen::Bare::Function' unless @_;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
843 0 my $xs = shift;
844 map {
845 0 my $x = $_;
846 0 ▶ gen {$$_[$x]} $xs
Subroutine __ANON__: not called CC=1 SCAR=6.9
847 } 0 .. $n - 1;
848 }
849 1 bless \&unzipn => 'List::Gen::Bare::Function';
850
851
852 =back
853
854 =head3 Functions on strings
855
856 =over 4
857
858 =item lines C< STRING >
859
860 lines breaks a string up into a list of strings at newline characters. The
861 resulting strings do not contain newlines. the newline sequence is taken from
862 the value of the input record separator C< $/ >
863
864 =cut
865
866 let lines => sub {
867 0 ▶ my $str = &lazy->join($/);
Subroutine __ANON__: not called CC=1 SCAR=5.6
868 0 my $nl = quotemeta $/;
869 iterate_multi {
870 0 ▶ if ($str =~ s{\A(.*?)(?:$nl|\Z)}{}o) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/\A(.*?)(?:$nl|\Z)//o) { } 0 0 0
871 0 ▶ length $str ? $1 : done $1
Branch: 1 branch
BranchTrueFalseTotal
CORE::length $str ? : 0 0 0
872 0 } else {done}
873 }
874 0 };
875
876
877 =item words C< STRING >
878
879 words breaks a string up into a list of words, which were delimited by white
880 space.
881
882 =cut
883
884 let words => sub {
885 0 ▶ my $str = &lazy->str;
Subroutine __ANON__: not called CC=1 SCAR=5.5
886 iterate_multi {
887 0 ▶ if ($str =~ s/\A\s*(\S*)(?:\s+|\Z)//) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/\A\s*(\S*)(?:\s+|\Z)//) { } 0 0 0
888 0 ▶ length $str ? $1 : done $1
Branch: 1 branch
BranchTrueFalseTotal
CORE::length $str ? : 0 0 0
889 0 } else {done}
890 }
891 0 };
892
893
894 =item unlines C< LIST >
895
896 unlines is an inverse operation to lines. It joins lines, after appending a
897 terminating newline to each. the newline sequence is taken from the value of the
898 input record separator C< $/ >
899
900 =cut
901
902 0 ▶ let unlines => sub {&foldl1(sub {join $/ => @_}, &lazy)};
Subroutine __ANON__: not called CC=1 SCAR=2.6
903
904
905 =item unwords C< LIST >
906
907 unwords is an inverse operation to words. It joins words with separating spaces.
908
909 =cut
910
911 0 ▶ let unwords => sub {&foldl1(sub {join ' ' => @_}, &lazy)};
Subroutine __ANON__: not called CC=1 SCAR=2.6
912
913
914 =back
915
916 =head1 ACKNOWLEGEMENTS
917
918 most of the documentation here started out at
919 L<http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.html>
920 and was subsequently edited to account for implementation differences.
921
922 =head1 AUTHOR
923
924 Eric Strom, C<< <asg at cpan.org> >>
925
926 =head1 BUGS
927
928 there are certainly bugs in code this complex. send in reports, tests, patches.
929
930 report any bugs / feature requests to C<bug-list-gen at rt.cpan.org>, or through
931 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Gen>.
932
933 =head1 COPYRIGHT & LICENSE
934
935 copyright 2009-2011 Eric Strom.
936
937 this program is free software; you can redistribute it and/or modify it under
938 the terms of either: the GNU General Public License as published by the Free
939 Software Foundation; or the Artistic License.
940
941 see http://dev.perl.org/licenses/ for more information.
942
943 =cut
944
945 1 $setup_export->();
946 1 delete $List::Gen::Haskell::{let};
947
948 1 __PACKAGE__ if 'first require';
« lib/List/Gen/Lazy.pm ↑ Summary blib/lib/List/Gen/Lazy/Builtins.pm »