/
Seq.pm6
182 lines (166 loc) Β· 5.49 KB
/
Seq.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
my class X::Seq::Consumed { ... }
my class X::Seq::NotIndexable { ... }
my class Seq is Cool does Iterable does Sequence {
# The underlying iterator that iterating this sequence will work its
# way through. Can only be obtained once.
has Iterator $!iter;
# The only valid way to create a Seq directly is by giving it the
# iterator it will consume and maybe memoize.
multi method new(Seq: Iterator:D $iter) {
nqp::p6bindattrinvres(nqp::create(self),Seq,'$!iter',nqp::decont($iter))
}
# This candidate exists purely for being able to EVAL a .raku
# representation of a Seq of which the iterator has already been taken,
multi method new(Seq:) { nqp::create(self) }
method iterator(Seq:D:) {
nqp::if(
nqp::isconcrete(my \iter = $!iter),
nqp::stmts(
($!iter := Iterator),
iter
),
nqp::if(
nqp::isconcrete($!list),
$!list.iterator,
X::Seq::Consumed.new.throw
)
)
}
multi method is-lazy(Seq:D:) {
nqp::if(
nqp::isconcrete($!iter),
$!iter.is-lazy,
nqp::if(
nqp::isconcrete($!list),
$!list.is-lazy,
X::Seq::Consumed.new.throw
)
)
}
multi method Seq(Seq:D:) { self }
method Capture() {
self.List.Capture
}
method elems() {
nqp::if(
self.is-lazy,
Failure.new(X::Cannot::Lazy.new(action => '.elems')),
nqp::if(
nqp::isconcrete($!iter) && nqp::istype($!iter,PredictiveIterator),
$!iter.count-only,
self.cache.elems
)
)
}
method Numeric() { self.elems }
method Int() { self.elems }
method Bool(Seq:D:) {
nqp::if(
nqp::isconcrete($!iter) && nqp::istype($!iter,PredictiveIterator),
$!iter.bool-only,
self.cache.Bool
)
}
multi method raku(Seq:D \SELF:) {
# If we don't have an iterator, someone grabbed it already;
# Check for cached $!list; if that's missing too, we're consumed
my $perl;
if not $!iter.DEFINITE and not $!list.DEFINITE {
# cannot call .cache on a Seq that's already been iterated,
# so we need to produce a string that, when EVAL'd, reproduces
# an already iterated Seq.
# compare https://github.com/Raku/old-issue-tracker/issues/5124
$perl = self.^name ~ '.new()';
}
else { $perl = self.cache.raku ~ '.Seq' }
nqp::iscont(SELF) ?? '$(' ~ $perl ~ ')' !! $perl
}
method join(Seq:D: $separator = '' --> Str:D) {
nqp::if(
(my $iterator := self.iterator).is-lazy,
'...',
nqp::stmts(
(my $strings := nqp::list_s),
nqp::until(
nqp::eqaddr((my $pulled := $iterator.pull-one),IterationEnd),
nqp::push_s($strings,nqp::unbox_s(
nqp::if(
nqp::isconcrete($pulled) && nqp::istype($pulled,Str),
$pulled,
nqp::if(
nqp::can($pulled,'Str'),
$pulled.Str,
nqp::box_s($pulled,Str)
)
)
))
),
nqp::box_s(nqp::join(nqp::unbox_s($separator.Str),$strings),Str)
)
)
}
method reverse(--> Seq:D) {
nqp::if(
(my $iterator := self.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<reverse>)),
nqp::stmts(
$iterator.push-all(my \buffer := nqp::create(IterationBuffer)),
Seq.new: Rakudo::Iterator.ReifiedReverse(buffer, Mu)
)
)
}
method sink(--> Nil) {
nqp::if(
nqp::isconcrete($!iter),
nqp::stmts(
$!iter.sink-all,
($!iter := Iterator)
),
nqp::if(
nqp::isconcrete($!list),
$!list.sink
)
)
}
# This method is mainly called from Actions.nqp
proto method from-loop(|) {*}
multi method from-loop(&body, :$label) {
Seq.new: Rakudo::Iterator.Loop(&body, $label)
}
multi method from-loop(&body, &cond, :$repeat!, :$label) {
Seq.new: $repeat
?? Rakudo::Iterator.RepeatLoop(&body, &cond, $label)
!! Rakudo::Iterator.WhileLoop(&body, &cond, $label)
}
multi method from-loop(&body, &cond, :$label) {
Seq.new: Rakudo::Iterator.WhileLoop(&body, &cond, $label)
}
multi method from-loop(&body, &cond, &afterwards, :$label) {
Seq.new: Rakudo::Iterator.CStyleLoop(&body, &cond, &afterwards, $label)
}
multi method ACCEPTS(Seq:D: Iterable:D \iterable --> Bool:D) {
nqp::if(
(my \liter := self.iterator).is-lazy,
False,
nqp::if(
(my \riter := iterable.iterator).is-lazy,
False,
nqp::stmts(
nqp::until(
nqp::eqaddr((my \left := liter.pull-one),IterationEnd),
nqp::if(
nqp::eqaddr((my \right := riter.pull-one),IterationEnd)
|| nqp::not_i(right.ACCEPTS(left)),
(return False)
)
),
nqp::hllbool(nqp::eqaddr(riter.pull-one,IterationEnd))
)
)
)
}
}
sub GATHER(&block) is implementation-detail {
Seq.new(Rakudo::Iterator.Gather(&block))
}
# vim: ft=perl6 expandtab sw=4