| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Monad::Base::Sugar; |
|
2
|
17
|
|
|
17
|
|
85
|
use strict; |
|
|
17
|
|
|
|
|
31
|
|
|
|
17
|
|
|
|
|
419
|
|
|
3
|
17
|
|
|
17
|
|
83
|
use warnings; |
|
|
17
|
|
|
|
|
28
|
|
|
|
17
|
|
|
|
|
468
|
|
|
4
|
17
|
|
|
17
|
|
79
|
use Scalar::Util qw/blessed weaken/; |
|
|
17
|
|
|
|
|
31
|
|
|
|
17
|
|
|
|
|
1992
|
|
|
5
|
17
|
|
|
17
|
|
82
|
use Exporter qw/import/; |
|
|
17
|
|
|
|
|
36
|
|
|
|
17
|
|
|
|
|
18486
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @EXPORT = qw/pick satisfy yield let/; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $_PICK = our $_SATISFY = |
|
10
|
|
|
|
|
|
|
our $_YIELD = our $_LET = sub { die "called outside for()." }; |
|
11
|
32
|
|
|
32
|
1
|
144
|
sub pick($;$) { $_PICK->(@_) } |
|
12
|
5
|
|
|
5
|
1
|
34
|
sub satisfy(&) { $_SATISFY->(@_) } |
|
13
|
7
|
|
|
7
|
1
|
46
|
sub yield(&) { $_YIELD->(@_) } |
|
14
|
5
|
|
|
5
|
1
|
28
|
sub let($$) { $_LET->(@_) } |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _capture { |
|
17
|
181
|
|
|
181
|
|
229
|
my $ref = pop; |
|
18
|
|
|
|
|
|
|
|
|
19
|
181
|
100
|
66
|
|
|
699
|
return $ref->capture(@_) if blessed $ref && $ref->can('capture'); |
|
20
|
|
|
|
|
|
|
|
|
21
|
151
|
100
|
|
|
|
490
|
ref $ref eq 'ARRAY' ? (@$ref = @_) : ($$ref = $_[0]); |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
18
|
|
|
18
|
|
71
|
sub _tuple { bless [@_], 'Data::Monad::Base::Sugar::Tuple' } |
|
25
|
|
|
|
|
|
|
sub Data::Monad::Base::Sugar::Tuple::capture { |
|
26
|
30
|
|
|
30
|
|
44
|
my ($self, $result) = @_; |
|
27
|
30
|
50
|
33
|
|
|
191
|
blessed $result && $result->isa(ref $self) |
|
28
|
|
|
|
|
|
|
or die "[BUG]result is not tuple"; |
|
29
|
|
|
|
|
|
|
|
|
30
|
30
|
|
|
|
|
76
|
_capture @{$result->[$_]} => $self->[$_] for 0 .. $#$self; |
|
|
60
|
|
|
|
|
132
|
|
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub for(&) { |
|
34
|
12
|
|
|
12
|
1
|
4012
|
my $code = shift; |
|
35
|
|
|
|
|
|
|
|
|
36
|
12
|
|
|
|
|
20
|
my @blocks; |
|
37
|
|
|
|
|
|
|
{ |
|
38
|
12
|
|
|
|
|
19
|
local $_PICK = sub { |
|
39
|
32
|
|
|
32
|
|
66
|
my ($ref, $block) = @_; |
|
40
|
32
|
100
|
|
|
|
91
|
$block = $ref, $ref = undef unless defined $block; |
|
41
|
|
|
|
|
|
|
|
|
42
|
32
|
|
|
|
|
187
|
push @blocks, {ref => $ref, block => $block}; |
|
43
|
12
|
|
|
|
|
55
|
}; |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
local $_YIELD = sub { |
|
46
|
7
|
|
|
7
|
|
10
|
my $block = shift; |
|
47
|
|
|
|
|
|
|
|
|
48
|
7
|
|
|
|
|
77
|
$blocks[$#blocks]->{yield} = $block; |
|
49
|
12
|
|
|
|
|
40
|
}; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
local $_SATISFY = sub { |
|
52
|
5
|
|
|
5
|
|
7
|
my $predicate = shift; |
|
53
|
5
|
50
|
|
|
|
15
|
die "satisfy() should be called after pick()." |
|
54
|
|
|
|
|
|
|
unless @blocks; |
|
55
|
|
|
|
|
|
|
|
|
56
|
5
|
|
|
|
|
8
|
my $slot = $#blocks; |
|
57
|
5
|
|
|
|
|
11
|
my $orig_block = $blocks[$slot]->{block}; |
|
58
|
5
|
|
|
|
|
7
|
my $orig_ref = $blocks[$slot]->{ref}; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$blocks[$slot]->{block} = sub { |
|
61
|
|
|
|
|
|
|
$orig_block->()->filter(sub { |
|
62
|
18
|
|
|
|
|
35
|
_capture @_ => $orig_ref; |
|
63
|
18
|
|
|
|
|
33
|
delete $blocks[$slot]; # destroy the cyclic ref |
|
64
|
18
|
|
|
|
|
42
|
$predicate->(@_); |
|
65
|
8
|
|
|
|
|
18
|
}); |
|
66
|
5
|
|
|
|
|
23
|
}; |
|
67
|
12
|
|
|
|
|
52
|
}; |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
local $_LET = sub { |
|
70
|
5
|
|
|
5
|
|
9
|
my ($ref, $block) = @_; |
|
71
|
|
|
|
|
|
|
|
|
72
|
5
|
100
|
|
|
|
12
|
unless (@blocks) { |
|
73
|
|
|
|
|
|
|
# eval immediately because we aren't in any lambdas. |
|
74
|
1
|
|
|
|
|
3
|
_capture $block->() => $ref; |
|
75
|
1
|
|
|
|
|
3
|
return; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
6
|
my $slot = $#blocks; |
|
79
|
4
|
|
|
|
|
8
|
my $orig_block = $blocks[$slot]->{block}; |
|
80
|
4
|
|
|
|
|
4
|
my $orig_ref = $blocks[$slot]->{ref}; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Capture multiple values. |
|
83
|
|
|
|
|
|
|
# A tupple is used in "p <- e; p' = e'" pattern. |
|
84
|
|
|
|
|
|
|
# See: http://www.scala-lang.org/docu/files/ScalaReference.pdf |
|
85
|
4
|
|
|
|
|
8
|
$blocks[$slot]->{ref} = _tuple $orig_ref, $ref; |
|
86
|
|
|
|
|
|
|
$blocks[$slot]->{block} = sub { |
|
87
|
|
|
|
|
|
|
$orig_block->()->map(sub { |
|
88
|
14
|
|
|
|
|
31
|
_capture @_ => $orig_ref; |
|
89
|
14
|
|
|
|
|
23
|
delete $blocks[$slot]; # destroy the cyclic ref |
|
90
|
14
|
|
|
|
|
36
|
return _tuple [@_], [$block->()]; |
|
91
|
6
|
|
|
|
|
11
|
}); |
|
92
|
4
|
|
|
|
|
18
|
}; |
|
93
|
12
|
|
|
|
|
50
|
}; |
|
94
|
12
|
|
|
|
|
37
|
$code->(); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
12
|
|
|
|
|
25
|
my $weak_loop; |
|
98
|
|
|
|
|
|
|
my $loop = sub { |
|
99
|
85
|
|
|
85
|
|
143
|
my @blocks = @_; |
|
100
|
|
|
|
|
|
|
|
|
101
|
85
|
|
|
|
|
120
|
my $info = shift @blocks; |
|
102
|
85
|
|
|
|
|
228
|
my $m = $info->{block}->(); |
|
103
|
85
|
|
|
|
|
210
|
my $ref = $info->{ref}; |
|
104
|
|
|
|
|
|
|
|
|
105
|
85
|
100
|
|
|
|
226
|
if ($info->{yield}) { |
|
|
|
100
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return $m->map(sub { |
|
107
|
15
|
|
|
|
|
33
|
_capture @_ => $ref; |
|
108
|
15
|
|
|
|
|
36
|
$info->{yield}->(); |
|
109
|
11
|
|
|
|
|
59
|
}); |
|
110
|
|
|
|
|
|
|
} elsif (@blocks) { |
|
111
|
38
|
|
|
|
|
47
|
my $retained_loop = $weak_loop; |
|
112
|
|
|
|
|
|
|
return $m->flat_map(sub { |
|
113
|
73
|
|
|
|
|
149
|
_capture @_ => $ref; |
|
114
|
73
|
|
|
|
|
221
|
$retained_loop->(@blocks); |
|
115
|
38
|
|
|
|
|
212
|
}); |
|
116
|
|
|
|
|
|
|
} else { |
|
117
|
36
|
|
|
|
|
201
|
return $m; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
12
|
|
|
|
|
50
|
}; |
|
120
|
12
|
|
|
|
|
89
|
weaken($weak_loop = $loop); |
|
121
|
|
|
|
|
|
|
|
|
122
|
12
|
|
|
|
|
50
|
return $loop->(@blocks); |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__END__ |