lib/FP/Lazy.pm

filter: Statementstmt 85.3% 175 / 205 Branchbran 67.3% 62 / 92 Conditioncond 48.5% 17 / 35 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 76.9% 40 / 52 Podpod 0.0% 0 / 16 totaltotal 71.1% 299 / 420 CC 58 SCAR 17.1
CC
58
Cov
77%
lazy_backtrace
20.0
AUTOLOAD
13.2
force
11.3
CRAP 5.5
« lib/Chj/TEST.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Mediawiki.pm »
1 #
2 # Copyright (c) 2013-2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Lazy - lazy evaluation (delayed evaluation, promises)
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 my $a = lazy { 1 / 0 };
19 eval {
20 print force $a
21 };
22 like $@, qr/^Illegal division by zero/;
23  
24 eval {
25 $a + 2
26 };
27 like $@, qr/^non-auto-forcing promise accessed via 0\+ operation/;
28  
29 my $count = 0;
30 my $b = lazy { $count++; 1 / 2 };
31 is is_promise($b), 1;
32 is $count, 0;
33 is force($b), 1/2; # increments $count
34 is $count, 1;
35 # $b is still a promise at this point (although an evaluated one):
36 is is_promise($b), 1;
37 is force($b), 1/2; # does not increment $count anymore
38 is $count, 1;
39  
40 # The following stores result of `force $b` back into $b
41 FORCE $b;
42 is is_promise($b), undef;
43 is $b, 1/2;
44 is $count, 1;
45  
46 # Note that lazy evaluation and mutation usually doesn't mix well -
47 # lazy programs better be purely functional. Here $tot depends not
48 # just on the inputs, but also on how many elements were evaluated:
49 1 ▶ use FP::Stream qw(stream_map); # uses `lazy` internally
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 {
52 my $tot = 0;
53 my $l = stream_map sub {
54 my ($x) = @_;
55 $tot += $x;
56 $x*$x
57 }, list (5,7,8);
58 is $tot, 0;
59 is $l->first, 25;
60 is $tot, 5;
61 is $l->length, 3;
62 is $tot, 20;
63 }
64  
65 # Also note that `local` does mutation (even if in a somewhat
66 # controlled way):
67 our $foo = "";
68 1 ▶ sub moo {
Subroutine moo: called CC=1 SCAR=0.0
69 1 my ($bar) = @_;
70 1 local $foo = "Hello";
71 1 ▶ lazy { "$foo $bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 }
73 is moo("you")->force, " you";
74  
75 # runtime conditional lazyness:
76  
77 2 ▶ sub condprom {
Subroutine condprom: called CC=1 SCAR=0.0
78 2 my ($cond) = @_;
79 2 ▶ lazy_if { 1 / 0 } $cond
Subroutine __ANON__: called CC=1 SCAR=0.0
80 }
81  
82 ok is_promise(condprom 1);
83  
84 eval {
85 # immediate division by zero exception (still pays
86 # the overhead of two subroutine calls, though)
87 condprom 0
88 };
89 like $@, qr/^Illegal division by zero/;
90  
91 # Calling methods on those promises will automatically force them,
92 # which is normally necessary since there's no way to know the
93 # class of the object otherwise:
94 1 ▶ use FP::Lazy qw(is_forced);
Subroutine BEGIN: called CC=1 SCAR=0.0
95 {
96 my $l = lazy { cons(1, null) };
97 ok !is_forced($l);
98 my $l2 = $l->cons(10);
99 # $l was forced even though the only reason is to know which
100 # class to call `cons` on:
101 ok is_forced($l);
102 }
103  
104 # There's `lazyT` which specifies the (or a base) class of the
105 # object statically, hence there's no need to evaluate a promise
106 # just to call a method. In this case the called method receives
107 # the unevaluated promise as its argument! (This might change in
108 # that either some flag in the the interface definition, or simply
109 # the stream_ prefix of a method could be required, otherwise it
110 # would still be forced. That would make it safe(r) but *maybe*
111 # (given a good test suite) it's not necessary?)
112 {
113 my $l = lazyT { cons(1, null) } "FP::List::Pair";
114 ok !is_forced($l);
115 my $l2 = $l->cons(10);
116 # $l has *not* been forced now.
117 ok !is_forced($l);
118 }
119  
120 # And `lazyT_if` which is the conditional variant:
121 2 ▶ sub typed_condprom {
Subroutine typed_condprom: called CC=1 SCAR=0.0
122 2 my ($cond) = @_;
123 2 ▶ lazyT_if { list(1 / 0) } "FP::List::List", $cond
Subroutine __ANON__: called CC=1 SCAR=0.0
124 }
125 ok is_promise(typed_condprom 1);
126 eval {
127 # immediate division by zero exception (still pays
128 # the overhead of two subroutine calls, though)
129 typed_condprom 0
130 };
131 like $@, qr/^Illegal division by zero/;
132  
133 # A `lazyLight` promise is re-evaluated on every access:
134 my $z = 0;
135 my $v = lazyLight { $z++; 3*4 };
136 is force($v), 12;
137 is $z, 1;
138 is force($v), 12;
139 is $z, 2;
140  
141 # There are 3 possible motivations for lazyLight: (1) lower
142 # allocation cost (save the wrapper data structure); (2) no risk
143 # for circular references (due to storing the result back into the
144 # wrapper (mutation) that can be used recursively); (3) to get
145 # fresh re-evaluation on every access and thus picking up any
146 # potential side effect.
147  
148 # Arguably (3) is against the functional programming idea, and is
149 # a bit of a mis-use of lazyLight. For now, FP::TransparentLazy
150 # still helps this case by not using `FORCE` automatically. (TODO:
151 # provide another type that provides this with a guarantee?)
152  
153 # Note that manual use of `FORCE` still stops the re-evalution:
154  
155 ok ref $v;
156 is FORCE($v), 12;
157 is $z, 3;
158 is force($v), 12;
159 is $z, 3; # you can see that re-evaluation has stopped
160 ok not ref $v;
161  
162 =head1 DESCRIPTION
163  
164 This implements promises, a data type that represents an unevaluated
165 or evaluated computation. The computation represented by a promise is
166 only ever evaluated once, after which point its result is saved in the
167 promise, and subsequent requests for evaluation are simply returning
168 the saved value.
169  
170 my $p = lazy { "......" }; # returns a promise that represents the computation
171 # given in the block of code
172  
173 force $p; # runs the block of code and stores the result within the
174 # promise and also returns it
175  
176 FORCE $p; # or FORCE $p,$q,$r;
177 # in addition to running force, stores back the resulting
178 # value into the variable given as argument ($p, $q, and $r
179 # respectively (the commented example forces 3 (possibly)
180 # separate values))
181  
182 is is_promise($p), undef; # returns true iff $x holds a promise
183  
184  
185 =head1 NOTE
186  
187 The thunk (code body) of a promise is always evaluated in scalar
188 context, even if it is being forced in list or void context.
189  
190 =head1 NAMING
191  
192 The name `lazy` for the delaying form was chosen because it seems what
193 most frameworks for functional programming on non-functional
194 programming languages are using, as well as Ocaml. We don't want to
195 stand in the way of what people expect, after all.
196  
197 Scheme calls the lazy evaluation form `delay`. This seems to make
198 sense, as that's a verb, unlike `lazy`. There's a conceptually
199 different way to introduce lazyness, which is to change the language
200 to be lazy by default, and `lazy` could be misunderstood to be a form
201 that changes the language in its scope to be that. Both for this
202 current (slight?) risk for misinterpretation, and to reserve it for
203 possible future implementation of this latter feature, it seems to be
204 wise to use `delay` and not `lazy` for what this module offers.
205  
206 What should we do?
207  
208 (To experiment with the style, or in case you're stubborn, you can
209 explicitely import `delay` or import the `:all` export tag to get it.)
210  
211 =head1 TODO
212  
213 If the thunk of a promise throws an exception, the promise will remain
214 unevaluated. This is the easiest (and most efficient) thing to do, but
215 there remains a question about the safety: if the data source is
216 read-once (like reading lines from files), and the exception happens
217 after the read, then forcing the promise again will fetch and store
218 the next line, hence a line will be lost. Since exceptions can happen
219 because of out of memory conditions or from signal handlers, this will
220 be of real concern in some situations.
221  
222 Provide safe promises for these situations? (But that would mean that
223 they need to be implemented in C as Perl does not offer the features
224 to implement them safely, correct?)
225  
226 =head1 DEBUGGING
227  
228 Lazy code can be difficult to debug because the context in which the
229 code that evaluates a promise runs is not the same context in which
230 the promise was captured. There are two approaches to make this
231 easier:
232  
233 C<$ENV{DEBUG_FP_LAZY} = "1"> or C<local $FP::Lazy::debug=1> --
234 captures a backtrace in every promise (slow, of course!). Use the
235 optionally exported C<lazy_backtrace> function to get the backtrace
236 (you can also see it in the C<FP::Repl>).
237  
238 C<$ENV{DEBUG_FP_LAZY} = "eager"> or C<local $FP::Lazy::eager=1> --
239 completely turns of any lazyness (except for lazyLight, currently);
240 easy stack traces and flow logic but of course the program behaves
241 differently; beware of infinite lists, they will be created
242 immediately now "in full length" and thus make the program use up all
243 the memory instead of finishing!
244  
245 C<use>ing L<FP::noLazy> instead of L<FP::Lazy> turns off laziness for
246 the current module, only. See the L<FP::noLazy> docs for more
247 information.
248  
249 =head1 SEE ALSO
250  
251 https://en.wikipedia.org/wiki/Futures_and_promises
252  
253 Alternative Data::Thunk, but see note in TODO file about problems.
254  
255 Alternative Scalar::Defer?
256  
257 L<FP::TransparentLazy>
258  
259 L<FP::noLazy>
260  
261 L<FP::Mixin::Utils> -- Lazy implements this as a fallback (lower
262 priority than forcing the promise and finding the method on the
263 result)
264  
265 =head1 NOTE
266  
267 This is alpha software! Read the status section in the package README
268 or on the L<website|http://functional-perl.org/>.
269  
270 =cut
271  
272 package FP::Lazy;
273 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
274 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 86 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
276 86 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
277  
278 86 our @EXPORT = qw(lazy lazyT lazy_if lazyT_if lazyLight force FORCE is_promise);
279 86 our @EXPORT_OK = qw(delay force_noeval lazy_backtrace is_forced);
280 86 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
281  
282 86 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
283 86 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 86 ▶use FP::Mixin::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
285 86 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 86 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
287 86 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
288  
289 86 ▶our $eager = ($ENV{DEBUG_FP_LAZY} and $ENV{DEBUG_FP_LAZY} =~ /^eager$/i);
Condition33%1/3
$ENV{'DEBUG_FP_LAZY'} && $ENV{'DEBUG_FP_LAZY'} =~ /^eager$/i
!ll&&!rl&&r
8600
MC/DC0%0/2
$ENV{'DEBUG_FP_LAZY'} && $ENV{'DEBUG_FP_LAZY'} =~ /^eager$/i
$ENV{'DEBUG_FP_LAZY'} $ENV{'DEBUG_FP_LAZY'} =~ /^eager$/i
Truth table33%1/3
A && B
A= $ENV{'DEBUG_FP_LAZY'}B= $ENV{'DEBUG_FP_LAZY'} =~ /^eager$/i
ABresult
0X0
100
111
290 86 ▶our $debug = $ENV{DEBUG_FP_LAZY} ? (not $eager) : '';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DEBUG_FP_LAZY'} ? : 0 86 86
291  
292 0 ▶sub die_not_a_Lazy_Promise {
Subroutine die_not_a_Lazy_Promise: not called CC=1 SCAR=6.9
Pod undocumented
293 0 my ($v) = @_;
294 0 die "not a FP::Lazy::Promise: " . show($v);
295 }
296  
297 # A promise is an array with two fields:
298 # index 0: thunk when unevaluated, undef once evaluated
299 # index 1: value once evaluated
300 # index 2: maybe blessed(force($promise))
301 # index 3: backtrace if $debug is true
302  
303 4 ▶sub is_forced {
Subroutine is_forced: called CC=4 SCAR=14.2
Pod undocumented
304 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
305 4 my ($v) = @_;
306  
307 # Note that $v might not even be a promise (anymore), given FORCE
308 # and AUTOLOAD.
309  
310 4 my $m;
311 4 ▶ blessed($v) ? (($m = $v->can("FP_Lazy_is_forced")) ? &$m($v) : 1) : 1
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($v) ? : 4 0 4
($m = $v->can('FP_Lazy_is_forced')) ? : 3 1 4
312 }
313  
314 0 ▶sub lazy_backtrace { # not a method to avoid shadowing any
Subroutine lazy_backtrace: not called CC=4 SCAR=30.0
Pod undocumented
315 # 'contained' method
316 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
317 0 my ($v) = @_;
318 0 ▶ blessed($v) // die_not_a_Lazy_Promise($v);
Condition0%0/3
builtin::blessed($v) // die_not_a_Lazy_Promise($v)
l!l&&r!l&&!r
000
MC/DC0%0/2
builtin::blessed($v) // die_not_a_Lazy_Promise($v)
builtin::blessed($v) die_not_a_Lazy_Promise($v)
Truth table0%0/3
A // B
A= builtin::blessed($v)B= die_not_a_Lazy_Promise($v)
ABresult
1X1
011
000
319  
320 # Consciously not working for Light ones!
321 0 ▶ if ($v->isa("FP::Lazy::AnyPromise")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('FP::Lazy::AnyPromise')) { } 0 0 0
322 0 $$v[3] # really assume such an access works, no fallback to a
323 # method like in FP::List
324 } else {
325 0 die_not_a_Lazy_Promise($v);
326 }
327 }
328  
329 1848 ▶sub lazy (&) {
Subroutine lazy: called CC=3 SCAR=12.0
Pod undocumented
330 1848 __ '`lazy { expr }`: evaluate expr only when forced via `force`';
331 1848 ▶ $eager ? goto $_[0]
Branch: 2 branches
BranchTrueFalseTotal
$FP::Lazy::eager ? : 0 1848 1848
$FP::Lazy::debug ? : 0 1848 1848
332 : $debug
333 ? bless([$_[0], undef, undef, FP::Repl::Stack->get(1)->backtrace],
334 "FP::Lazy::Promise")
335 : bless([$_[0], undef], "FP::Lazy::Promise")
336 }
337  
338 6254 ▶sub lazyT (&$) {
Subroutine lazyT: called CC=3 SCAR=12.0
Pod undocumented
339 6254 __ '`lazyT { expr } $classname`: expr must return an object that
340 satisfies ->isa($classname)';
341 6254 ▶ $eager ? goto $_[0] : bless [
Branch: 2 branches
BranchTrueFalseTotal
$FP::Lazy::eager ? : 0 6254 6254
$FP::Lazy::debug ? : 0 6254 6254
342 $_[0], undef,
343 $_[1], $debug ? FP::Repl::Stack->get(1)->backtrace : ()
344 ],
345 "FP::Lazy::Promise"
346 }
347  
348 742 ▶sub lazy_if (&$) {
Subroutine lazy_if: called CC=4 SCAR=14.0
Pod undocumented
349 742 __ '`lazy_if { expr } $boolean`: evaluate expr immediately if
350 $boolean is false, lazily otherwise';
351 (
352 ($_[1] and not $eager)
353 ? (
354 $debug
355 ? bless([$_[0], undef, undef, FP::Repl::Stack->get(1)->backtrace],
356 "FP::Lazy::Promise")
357 : bless([$_[0], undef], "FP::Lazy::Promise")
358 )
359 742 ▶ : do {
Branch: 2 branches
BranchTrueFalseTotal
$_[1] && !$FP::Lazy::eager ? : 305 437 742
$FP::Lazy::debug ? : 0 305 305
Condition66%2/3
$_[1] and not $FP::Lazy::eager
!ll&&!rl&&r
4370305
MC/DC50%1/2
$_[1] and not $FP::Lazy::eager
$_[1] not $FP::Lazy::eager
Truth table66%2/3
A and not(B)
A= $_[1]B= not $FP::Lazy::eager
ABresult
0X0
100
111
360 437 my ($thunk) = @_;
361 437 @_ = ();
362 437 goto $thunk;
363 }
364 )
365 }
366  
367 2 ▶sub lazyT_if (&$$) {
Subroutine lazyT_if: called CC=4 SCAR=14.3
Pod undocumented
368 2 __ '`lazyT_if { expr } $classname, $boolean`: expr must return an
369 object that satisfies ->isa($classname); eager unless $boolean
370 is true.';
371 (
372 2 ▶ ($_[2] and not $eager)
Branch: 2 branches
BranchTrueFalseTotal
$_[2] && !$FP::Lazy::eager ? : 1 1 2
$FP::Lazy::debug ? : 0 1 1
Condition66%2/3
$_[2] and not $FP::Lazy::eager
!ll&&!rl&&r
101
MC/DC50%1/2
$_[2] and not $FP::Lazy::eager
$_[2] not $FP::Lazy::eager
Truth table66%2/3
A and not(B)
A= $_[2]B= not $FP::Lazy::eager
ABresult
0X0
100
111
373 ? (
374 bless [
375 $_[0], undef,
376 $_[1], $debug ? FP::Repl::Stack->get(1)->backtrace : ()
377 ],
378 "FP::Lazy::Promise"
379 )
380 : goto $_[0]
381 )
382 }
383  
384 # not providing for caching (1-time-only evaluation)
385 111 ▶sub lazyLight (&) {
Subroutine lazyLight: called CC=2 SCAR=7.6
Pod undocumented
386 111 ▶ $eager ? goto $_[0] : bless $_[0], "FP::Lazy::PromiseLight"
Branch: 1 branch
BranchTrueFalseTotal
$FP::Lazy::eager ? : 0 111 111
387 }
388  
389 3964 ▶sub is_promise {
Subroutine is_promise: called CC=3 SCAR=11.1
Pod undocumented
390 3964 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3964 0 3964
391 3964 ▶ blessed($_[0]) // return;
Condition100%2/2
builtin::blessed($_[0]) // (return)
l!l
26451319
MC/DC100%1/1
builtin::blessed($_[0]) // (return)
builtin::blessed($_[0])
Truth table100%2/2
A
A= builtin::blessed($_[0])
Aresult
11
00
392 2645 $_[0]->isa("FP::Lazy::AnyPromise")
393 }
394  
395 sub delay (&);
396 86 *delay = \&lazy;
397 sub delayLight (&);
398 86 *delayLight = \&lazyLight;
399  
400 # (NOTE: there is a COPY-PASTE of this in TransparentLazy)
401 0 ▶sub die_type_error {
Subroutine die_type_error: not called CC=1 SCAR=6.9
Pod undocumented
402 0 my ($expected, $gotstr, $v) = @_;
403 0 die "promise expected to evaluate to an object "
404 . "of class '$expected' but got $gotstr: "
405 . show($v)
406 }
407  
408 25415 ▶sub force {
Subroutine force: called CC=11 SCAR=24.3
Pod undocumented
409 25415 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 25415 0 25415
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0025415
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
410 25415 my ($perhaps_promise, $nocache) = @_;
411 LP: {
412 25415 ▶ if (defined blessed($perhaps_promise)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($perhaps_promise)) { } 25306 211 25517
413 25306 ▶ if ($perhaps_promise->isa("FP::Lazy::PromiseLightBase")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($perhaps_promise->isa('FP::Lazy::PromiseLightBase')) { } 102 25204 25306
elsif ($perhaps_promise->isa('FP::Lazy::Promise')) { } 15042 10162 25204
414 102 $perhaps_promise = &$perhaps_promise;
415 102 redo LP;
416 } elsif ($perhaps_promise->isa("FP::Lazy::Promise")) {
417  
418 # NOTE: there is a COPY-PASTE of this part in
419 # TransparentLazy!
420 15042 ▶ if (defined(my $thunk = $$perhaps_promise[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $thunk = $perhaps_promise->[0])) { } 8052 6990 15042
421 no warnings
422 86 ▶ 'recursion'; # XXX leave something for debugging?
Subroutine BEGIN: called CC=1 SCAR=0.0
423 8052 my $v = force(&$thunk(), $nocache);
424 8045 ▶ if ($$perhaps_promise[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perhaps_promise->[2]) 6084 1961 8045
425  
426 6084 ▶ if (defined(my $got = blessed($v))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $got = builtin::blessed($v))) { } 6084 0 6084
427  
428 6084 ▶ $v->isa($$perhaps_promise[2])
Branch: 1 branch
BranchTrueFalseTotal
$v->isa($perhaps_promise->[2]) or die_type_error($perhaps_promise->[2], "a '${got}'", $v) 6084 0 6084
429 or die_type_error($$perhaps_promise[2],
430 "a '$got'", $v);
431 } else {
432 0 die_type_error($$perhaps_promise[2],
433 "a non-object", $v);
434 }
435 }
436 8045 ▶ unless ($nocache) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nocache) 7916 129 8045
437 7916 $$perhaps_promise[1] = $v;
438 7916 $$perhaps_promise[0] = undef;
439 }
440 $v
441 8045 } else {
442 6990 $$perhaps_promise[1]
443 }
444 } else {
445 10162 $perhaps_promise
446 }
447 } else {
448 211 $perhaps_promise
449 }
450 }
451 }
452  
453 # just remove promise wrapper, don't actually force its evaluation. XX
454 # does this need updating for the new type feature?
455 41 ▶sub force_noeval {
Subroutine force_noeval: called CC=5 SCAR=16.1
Pod undocumented
456 41 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 41 0 41
457 41 my ($s) = @_;
458 41 ▶ if (defined blessed($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($s)) { } 37 4 41
459 37 ▶ if ($s->isa("FP::Lazy::Promise")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->isa('FP::Lazy::Promise')) { } 10 27 37
460 10 ▶ if ($$s[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->[0]) { } 1 9 10
461 1 $s
462 } else {
463 9 $$s[1]
464 }
465 } else {
466 27 $s
467 }
468 } else {
469 4 $s
470 }
471 }
472  
473 349 ▶sub FORCE {
Subroutine FORCE: called CC=3 SCAR=11.2
Pod undocumented
474 349 for (@_) {
475 463 $_ = force $_
476 }
477 349 ▶ wantarray ? @_ : $_[-1] ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 349 349
478 }
479  
480 # XX because show did lead to endless loop, (why?) sgh
481 0 ▶sub strshow {
Subroutine strshow: not called CC=2 SCAR=17.9
Pod undocumented
482 0 my ($v) = @_;
483 0 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 0 0 0
484 0 require overload;
485 0 overload::StrVal($v)
486 } else {
487 0 "undef"
488 }
489 }
490  
491 # `use overload` arguments, to prevent from accidental use as if it
492 # were FP::TransparentLazy
493  
494 86 our $allow_access = 0; # true 'turns off' the overload
495  
496 172 ▶sub overloads {
Subroutine overloads: called CC=2 SCAR=7.0
Pod undocumented
497 172 my ($with_application_overload) = @_;
498 (
499 (
500 map {
501 172 my $ctx = $_;
502 1 ▶ $ctx => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
503 1 ▶ $allow_access
Branch: 1 branch
BranchTrueFalseTotal
$FP::Lazy::allow_access ? : 0 1 1
504 ? $_[0]
505 : Carp::croak
506 "non-auto-forcing promise accessed via $ctx operation"
507 }
508 1290 ▶ } ($with_application_overload ? ("&{}") : ()),
Branch: 1 branch
BranchTrueFalseTotal
$with_application_overload ? : 86 86 172
509  
510 # (XX can't overload '@{}'?)
511 qw(0+ "" bool qr ${} %{} *{})
512 ),
513 fallback => 1
514 )
515 }
516  
517 86 package FP::Lazy::AnyPromise {
518  
519 86 *force = \&FP::Lazy::force;
520  
521 0 ▶ sub FORCE {
Subroutine FORCE: not called CC=1 SCAR=6.9
522 0 $_[0] = FP::Lazy::force($_[0]);
523 }
524  
525 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
526  
527 # nothing, catch this to prevent it from entering AUTOLOAD
528 }
529  
530 # XXX TODO: provide a `can` method, right?
531  
532 }
533  
534 0 ▶sub dummy_modifier_for {
Subroutine dummy_modifier_for: not called CC=1 SCAR=6.9
Pod undocumented
535 0 my ($s) = @_;
536 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
537 0 my ($dummystr) = @_;
538 0 $dummystr . "\nContext:\n" . $$s[3]
539 }
540 0 }
541  
542 86 ▶use FP::Show qw(parameterized_show_coderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
543  
544 86 my $lazy_thunk_show = parameterized_show_coderef("lazy ");
545 86 my $lazyT_thunk_show = parameterized_show_coderef("lazyT ");
546  
547 86 package FP::Lazy::Promise {
548 86 our @ISA = 'FP::Lazy::AnyPromise';
549  
550 86 ▶ use overload FP::Lazy::overloads(1);
Subroutine BEGIN: called CC=1 SCAR=0.0
551  
552 3 ▶ sub FP_Lazy_is_forced {
Subroutine FP_Lazy_is_forced: called CC=1 SCAR=0.0
553 3 not defined $_[0][0]
554 }
555  
556 5 ▶ sub FP_Show_show {
Subroutine FP_Show_show: called CC=5 SCAR=16.2
557 5 my ($s, $show) = @_;
558  
559 # do not force unforced promises
560 5 ▶ if (defined $$s[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->[0]) { } 4 1 5
561 4 ▶ if (defined(my $cl = $$s[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cl = $s->[2])) { } 3 1 4
562 3 ▶ my $thunk_show
Branch: 1 branch
BranchTrueFalseTotal
$s->[3] ? : 0 3 3
563 = $$s[3]
564 ? FP::Lazy::parameterized_show_coderef("lazyT ",
565 FP::Lazy::dummy_modifier_for($s))
566 : $lazyT_thunk_show;
567 3 $thunk_show->($$s[0]) . " " . $show->($cl)
568 } else {
569 1 ▶ my $thunk_show
Branch: 1 branch
BranchTrueFalseTotal
$s->[3] ? : 0 1 1
570 = $$s[3]
571 ? FP::Lazy::parameterized_show_coderef("lazy ",
572 FP::Lazy::dummy_modifier_for($s))
573 : $lazy_thunk_show;
574 1 $thunk_show->($$s[0])
575 }
576 } else {
577 1 &$show($$s[1])
578 }
579 }
580  
581 86 our $AUTOLOAD; # needs to be declared even though magical
582  
583 233 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=12 SCAR=25.8
584 233 my $methodname = $AUTOLOAD;
585 233 $methodname =~ s/.*:://;
586 233 my $maybe_expected_ref = $_[0][2];
587 233 my ($v, $ref);
588 233 ▶ if (defined $maybe_expected_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_expected_ref) { } 73 160 233
589 73 $ref = $maybe_expected_ref;
590 } else {
591 160 $v = FP::Lazy::force($_[0]);
592 158 $ref = ref $v;
593 }
594  
595 # To be able to select special implementations for lazy
596 # inputs, select a method with `stream_` prefix if present.
597  
598 # This will give "Can't call method "can" without a package or
599 # object reference" exception for the empty string given as
600 # type, which is happening in a weird place but actually is
601 # appropriate enough, right? Leaving at that is cheaper than
602 # special-casing it.
603  
604 # XX: a possibility would be to force the value even if it's a
605 # lazyT, if the method isn't a lazy one. How to know if it's a
606 # lazy one? stream_ prefix could double up for that,
607 # possibly. Will have to provide cons as stream_cons alias,
608 # then, though, for example.
609  
610 231 ▶ my $method
Branch: 1 branch
BranchTrueFalseTotal
$methodname =~ /^stream_/ ? : 3 228 231
Condition83%5/6
($ref->can("stream_$methodname") // $ref->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
l!l&&r!l&&!r
212016
$ref->can("stream_$methodname") // $ref->can($methodname)
l!l&&r!l&&!r
1625016
MC/DC66%2/3
($ref->can("stream_$methodname") // $ref->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
$ref->can("stream_$methodname") $ref->can($methodname) "FP::Mixin::Utils"->can($methodname)
Truth table75%3/4
A // B // C
A= $ref->can("stream_$methodname")B= $ref->can($methodname)C= "FP::Mixin::Utils"->can($methodname)
ABCresult
1XX1
01X1
0011
0000
611 = ($methodname =~ /^stream_/
612 ? $ref->can($methodname)
613 : $ref->can("stream_$methodname") // $ref->can($methodname)
614 // "FP::Mixin::Utils"->can($methodname));
615 231 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 215 16 231
616  
617 # If we forced evaluation, pass on the evaluated value.
618 # Can't rebuild @_ or it would break 'env clearing' ability
619 # of the method. Thus assign to $_[0], which will effect
620 # our env, too, but so what? XX still somewhat bad. (Is
621 # like `FORCE`.)
622 215 ▶ $_[0] = $v unless defined $maybe_expected_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maybe_expected_ref 158 57 215
623 215 goto &$method;
624 } else {
625  
626 16 ▶ if (defined $maybe_expected_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_expected_ref) 16 0 16
627  
628 # If the library is declaring a base class as the type
629 # of the lazyT (in the case of basically a sum type),
630 # then if the user calls a method on the promise that
631 # does not exist in that base class, then still force
632 # it: this is also correct since in that case, the
633 # method will surely *need* the forced value, since,
634 # if it doesn't, it would be independent of the
635 # subclass hence be in the base class. Thus: force
636 # (equivalent to downgrading the lazyT to a lazy), and
637 # try again.
638  
639 # $v = FP::Lazy::force($_[0]);
640 # $ref = ref $v;
641 # $maybe_expected_ref = undef; # so that the logic works
642 # redo ...
643  
644 # or simply adapted copy-paste:
645  
646 16 $v = FP::Lazy::force($_[0]);
647 16 ▶ my $method
Branch: 1 branch
BranchTrueFalseTotal
$methodname =~ /^stream_/ ? : 0 16 16
Condition33%2/6
($v->can("stream_$methodname") // $v->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
l!l&&r!l&&!r
1600
$v->can("stream_$methodname") // $v->can($methodname)
l!l&&r!l&&!r
0160
MC/DC0%0/3
($v->can("stream_$methodname") // $v->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
$v->can("stream_$methodname") $v->can($methodname) "FP::Mixin::Utils"->can($methodname)
Truth table25%1/4
A // B // C
A= $v->can("stream_$methodname")B= $v->can($methodname)C= "FP::Mixin::Utils"->can($methodname)
ABCresult
1XX1
01X1
0011
0000
648 = ($methodname =~ /^stream_/
649 ? $v->can($methodname)
650 : $v->can("stream_$methodname") // $v->can($methodname)
651 // "FP::Mixin::Utils"->can($methodname));
652 16 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 16 0 16
653 16 $_[0] = $v;
654 16 goto &$method;
655 }
656 }
657  
658 # XX imitate perl's ~exact error message?
659 0 Carp::croak "no method '$methodname' found for object: "
660 . FP::Lazy::strshow($v);
661 }
662 }
663  
664 # should really have a maybe_ prefix, but since it's for debugging
665 # purposes only (and in that case also likely always returns a
666 # value) and we like short names for that, leave it at this, ok?
667 # XXX avoid spamming with such a short name; use FP_Lazy_bt [or
668 # de-priorize it like the FP::Mixin::Utils stuff?]
669 0 ▶ sub bt {
Subroutine bt: not called CC=1 SCAR=6.9
670 0 my $s = shift;
671 0 $$s[2]
672 }
673 }
674  
675 86 my $lazyLight_thunk_show = parameterized_show_coderef("lazyLight ");
676  
677 86 package FP::Lazy::PromiseLightBase {
678  
679 # Things shared with FP::TransparentLazy::PromiseLight
680  
681 86 our @ISA = qw(FP::Lazy::AnyPromise);
682  
683 0 ▶ sub FP_Lazy_is_forced {
Subroutine FP_Lazy_is_forced: not called CC=1 SCAR=6.9
684 0 0
685 }
686  
687 0 ▶ sub FP_Show_show {
Subroutine FP_Show_show: not called CC=1 SCAR=6.9
688 0 my ($s, $show) = @_;
689  
690 # do not force unforced promises
691 0 &$lazyLight_thunk_show($s)
692 }
693  
694 86 our $AUTOLOAD; # needs to be declared even though magical
695  
696 4 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=18.7
697 4 my $methodname = $AUTOLOAD;
698  
699 4 my $v = $_[0]->();
700 4 $methodname =~ s/.*:://;
701  
702 # To be able to select special implementations for lazy
703 # inputs, select a method with `stream_` prefix if present.
704  
705 4 ▶ my $method
Branch: 1 branch
BranchTrueFalseTotal
$methodname =~ /^stream_/ ? : 0 4 4
Condition33%2/6
($v->can("stream_$methodname") // $v->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
l!l&&r!l&&!r
400
$v->can("stream_$methodname") // $v->can($methodname)
l!l&&r!l&&!r
040
MC/DC0%0/3
($v->can("stream_$methodname") // $v->can($methodname)) // "FP::Mixin::Utils"->can($methodname)
$v->can("stream_$methodname") $v->can($methodname) "FP::Mixin::Utils"->can($methodname)
Truth table25%1/4
A // B // C
A= $v->can("stream_$methodname")B= $v->can($methodname)C= "FP::Mixin::Utils"->can($methodname)
ABCresult
1XX1
01X1
0011
0000
706 = ($methodname =~ /^stream_/
707 ? $v->can($methodname)
708 : $v->can("stream_$methodname") // $v->can($methodname)
709 // "FP::Mixin::Utils"->can($methodname));
710 4 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 4 0 4
711  
712 # Can't rebuild @_ or it would break 'env clearing' ability
713 # of the method. Thus assign to $_[0], which will effect
714 # our env, too, but so what? XX still somewhat bad. (Is
715 # like `FORCE`.)
716 4 $_[0] = $v;
717 4 goto &$method;
718 } else {
719  
720 # XX imitate perl's ~exact error message?
721 0 Carp::croak "no method '$methodname' found for object: "
722 . FP::Lazy::strshow($v);
723 }
724 }
725  
726 0 ▶ sub bt {
Subroutine bt: not called CC=1 SCAR=6.9
727 0 my $s = shift;
728 0 Carp::croak "lazyLight cannot store a backtrace"
729  
730 # except if using a different implementation when $debug
731 # is on
732 }
733 }
734  
735 86 package FP::Lazy::PromiseLight {
736 86 our @ISA = qw(FP::Lazy::PromiseLightBase);
737 86 ▶ use overload FP::Lazy::overloads(0);
Subroutine BEGIN: called CC=1 SCAR=0.0
738 }
739  
740 86 1
« lib/Chj/TEST.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Mediawiki.pm »