| 1 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2015-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::TransparentLazy - lazy evaluation with transparent evaluation | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use FP::TransparentLazy; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # This is the same SYNOPSIS as in FP::Lazy but with most `force` | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # calls removed, and slightly differing behaviour in places | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # (e.g. `$a + 2` will evaluate the thunk here and thus give | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # division by zero): | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | my $a = lazy { 1 / 0 }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # $a's evaluation is forced here | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | print $a | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | like $@, qr/^Illegal division by zero/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | $a + 2 | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | like $@, qr/^Illegal division by zero/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $count = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | my $b = lazy { $count++; 1 / 2 }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | is is_promise($b), 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | is $count, 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | is $b, 1/2; # increments $count | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is $count, 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # $b is still a promise at this point (although an evaluated one): | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | is is_promise($b), 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | is $b, 1/2; # does not increment $count anymore | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | is $count, 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # The following stores result of `force $b` back into $b | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | FORCE $b; | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | is is_promise($b), undef; | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | is $b, 1/2; | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | is $count, 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # Note that lazy evaluation and mutation usually doesn't mix well - | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # lazy programs better be purely functional. Here $tot depends not | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # just on the inputs, but also on how many elements were evaluated: | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | use FP::Stream qw(stream_map); # uses `lazy` internally | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | use FP::List; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | my $tot = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | my $l = stream_map sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | my ($x) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | $tot += $x; | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | $x*$x | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | }, list (5,7,8); | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | is $tot, 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | is $l->first, 25; | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | is $tot, 5; | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | is $l->length, 3; | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | is $tot, 20; | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # Also note that `local` does mutation (even if in a somewhat | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # controlled way): | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | our $foo = ""; | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub moo { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
moo: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my ($bar) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | local $foo = "Hello"; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | lazy { "$foo $bar" } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | is moo("you")->force, " you"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | is moo("you"), " you"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # runtime conditional lazyness: | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub condprom { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
condprom: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | my ($cond) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | lazy_if { 1 / 0 } $cond | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ok is_promise(condprom 1); | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # immediate division by zero exception (still pays | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # the overhead of two subroutine calls, though) | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | condprom 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | like $@, qr/^Illegal division by zero/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # A `lazyLight` promise is re-evaluated on every access: | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | my $z = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | my $v = lazyLight { $z++; 3*4 }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | is $v, 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | is $z, 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | is $v, 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | is $z, 2; | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | is force($v), 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | is $z, 3; | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | is $v, 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | is $z, 4; | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # There are 3 possible motivations for lazyLight: (1) lower | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # allocation cost (save the wrapper data structure); (2) no risk | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # for circular references (due to storing the result back into the | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # wrapper (mutation) that can be used recursively); (3) to get | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # fresh re-evaluation on every access and thus picking up any | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # potential side effect. | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Arguably (3) is against the functional programming idea, and is | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # a bit of a mis-use of lazyLight. But, at least for now, | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # FP::TransparentLazy helps this case by not using `FORCE` | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # transparently; it shouldn't since that would break automatic | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # stream_ detection on subsequent calls (to things like `->map` | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # instead of `->stream_map`). | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Note that manual use of `FORCE` still stops the re-evalution: | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | ok ref $v; | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | is FORCE($v), 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | is $z, 5; | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | is $v, 12; | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | is $z, 5; # you can see that re-evaluation has stopped | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | ok not ref $v; | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # WARNING: such impure lazyLight promises from TransparentLazy are | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # dangerous in that if you never explicitly `force` them and use | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # the result (or `FORCE` them) then the exposure to side effects | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # will remain active. Use FP::Lazy's lazyLight instead, which | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # requires forcing thus requires this boundary to be explicit. | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | This implements a variant of FP::Lazy that forces promises | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | automatically upon access (and writes their result back to the place | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | they are forced from, like FP::Lazy's `FORCE` does, except in the | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | lazyLight case where `FORCE` is consciously not used automatically to | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | keep more consistent re-evaluation behaviour). Otherwise the two are | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | fully interchangeable. | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | NOTE: this is EXPERIMENTAL. Also, should this be merged with | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | L<Data::Thunk>? OTOH, should remain interchangeable with L<FP::Lazy>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | and maybe merged with that one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | The drawback of transparency might be more confusion, as it's not | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | directly visible anymore (neither in the debugger nor the source code) | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | what's lazy. Also, transparent forcing will be a bit more expensive | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | CPU wise. Please give feedback about your experiences! | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | L<FP::Lazy> | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =head1 NOTE | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | package FP::TransparentLazy; | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 45 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 45 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 45 | ▶ | use Exporter "import"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 45 | our @EXPORT = qw(lazy lazy_if lazyLight force FORCE is_promise); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 45 | our @EXPORT_OK = qw(delay lazy_backtrace); | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 45 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 45 | ▶ | use FP::Lazy qw(force FORCE is_promise lazy_backtrace); # for re-export | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 45 | ▶ | 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 45 | ▶ | our $debug = $ENV{DEBUG_FP_LAZY} ? (not $eager) : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | ▶ | sub lazy (&) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lazy: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 8 | ▶ | $eager | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$FP::TransparentLazy::debug && 'FP::Repl::Stack'->get(1)->backtrace
MC/DC0%0/2
$FP::TransparentLazy::debug && 'FP::Repl::Stack'->get(1)->backtrace
$FP::TransparentLazy::debug
'FP::Repl::Stack'->get(1)->backtrace
Truth table33%1/3
A && B
A= $FP::TransparentLazy::debugB= 'FP::Repl::Stack'->get(1)->backtrace
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | ? goto $_[0] | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | : bless [$_[0], undef, $debug && FP::Repl::Stack->get(1)->backtrace], | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | "FP::TransparentLazy::Promise" | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | sub lazy_if (&$) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lazy_if: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | ($_[1] and not $eager) | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | ? bless([$_[0], undef, $debug && FP::Repl::Stack->get(1)->backtrace], | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | "FP::TransparentLazy::Promise") | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | : do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$_[1] and not $FP::TransparentLazy::eager
$FP::TransparentLazy::debug && 'FP::Repl::Stack'->get(1)->backtrace
MC/DC50%1/2
$_[1] and not $FP::TransparentLazy::eager
$_[1]
not $FP::TransparentLazy::eager
MC/DC0%0/2
$FP::TransparentLazy::debug && 'FP::Repl::Stack'->get(1)->backtrace
$FP::TransparentLazy::debug
'FP::Repl::Stack'->get(1)->backtrace
Truth table66%2/3
A and not(B)
A= $_[1]B= not $FP::TransparentLazy::eager
Truth table33%1/3
A && B
A= $FP::TransparentLazy::debugB= 'FP::Repl::Stack'->get(1)->backtrace
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | my ($thunk) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | @_ = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | goto $thunk; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # not providing for caching (1-time-only evaluation) | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | ▶ | sub lazyLight (&) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lazyLight: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | ▶ | $eager ? goto $_[0] : bless $_[0], "FP::TransparentLazy::PromiseLight" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | sub delay (&); | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 45 | *delay = \&lazy; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | sub delayLight (&); | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 45 | *delayLight = \&lazyLight; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # XX to make it truly transparent, should always overload '&{}'; but | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # then how to force it without getting into an infinite loop? No way | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # to turn off the overload (except reblessing)? | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # XX hm, can't overload '@{}', why? | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 90 | ▶ | sub overloads { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
overloads: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 90 | my ($with_application_overload) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 90 | ▶ | ($with_application_overload ? ('&{}') : ()), qw'"" 0+ bool qr ${} %{} *{}'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # COPY-PASTE from FP::Lazy | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub die_type_error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
die_type_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my ($expected, $gotstr, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | die "promise expected to evaluate to an object " | |||||||||||||||||||||||||||||||||||||||||||||
| 225 | . "of class '$expected' but got $gotstr: " | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | . show($v) | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Only for the overload, you shouldn't use this manually (for one, | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # because it doesn't check the number of arguments, which is because | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # overload passes 3 of them, and then because this can't be used for | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # other promises and that will be dangerous): | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 1609 | ▶ | sub forceTransparentLazy { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
forceTransparentLazy: called CC=6 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1609 | my ($perhaps_promise) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1609 | my $nocache = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # COPY-PASTE from part of FP::Lazy::force: | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1609 | ▶ | if (defined(my $thunk = $$perhaps_promise[0])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | my $v = force(&$thunk(), $nocache); | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 6 | ▶ | if ($$perhaps_promise[2]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | if (defined(my $got = blessed($v))) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | $v->isa($$perhaps_promise[2]) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 245 | or die_type_error($$perhaps_promise[2], "a '$got'", $v); | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | die_type_error($$perhaps_promise[2], "a non-object", $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 6 | ▶ | unless ($nocache) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 6 | $$perhaps_promise[1] = $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | 6 | $$perhaps_promise[0] = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 254 | $v | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 6 | } else { | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1601 | $$perhaps_promise[1] | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 45 | package FP::TransparentLazy::Promise { | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 45 | our @ISA = qw(FP::Lazy::Promise); | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # Use of the FORCE method would be bad since it will make stream_ | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # detection fail on subsequent calls! (OK, would need to use | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # `Keep` anyway usually, but apparently not always?) Also, can't | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # use "force" method as that expects 1-2 arguments, overload | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # passes 3 (different ones)! | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 45 | ▶ | use overload( | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 45 | map { $_ => \&FP::TransparentLazy::forceTransparentLazy } | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | FP::TransparentLazy::overloads(1) | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | fallback => 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 45 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # Do *not* call "FORCE" method for PromiseLight if the aim is to | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # re-evaluate it every time. | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 3 | ▶ | sub forceLight { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
forceLight: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 3 | &{ $_[0] } | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 45 | package FP::TransparentLazy::PromiseLight { | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | 45 | our @ISA = qw(FP::Lazy::PromiseLightBase); | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 45 | ▶ | use overload( | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 45 | map { $_ => \&FP::TransparentLazy::forceLight } | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | FP::TransparentLazy::overloads(0) | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 290 | fallback => 1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 45 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 45 | ▶ | use Chj::TEST; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 45 | our $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | ▶ | TEST { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | $c = lazy { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | ▶ | sub {"foo"} | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 301 | 1 | ref $c | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 45 | 'FP::TransparentLazy::Promise'; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | TEST { &$c() } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 45 | "foo"; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | ▶ | TEST { ref $c } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 45 | "FP::TransparentLazy::Promise"; # was CODE before removing transparent FORCE | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 45 | 1 | |||||||||||||||||||||||||||||||||||||||||||||