-
Notifications
You must be signed in to change notification settings - Fork 3
/
Agnostic.pm6
299 lines (227 loc) · 9.12 KB
/
Agnostic.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use v6.c;
sub is-container(\it) is export { it.VAR.^name ne it.^name }
role Array::Agnostic:ver<0.0.6>:auth<cpan:ELIZABETH>
does Positional # .AT-POS and friends
does Iterable # .iterator, basically
{
#--- These methods *MUST* be implemented by the consumer -----------------------
method AT-POS($) is raw { ... }
method BIND-POS($,$) is raw { ... }
method EXISTS-POS($) { ... }
method DELETE-POS($) { ... }
method elems() { ... }
#--- Internal Iterator classes that need to be specified here ------------------
my class Iterate does Iterator {
has $.backend;
has $.end;
has $.index = -1;
method pull-one() is raw {
$!index < $!end
?? $!backend.AT-POS(++$!index)
!! IterationEnd
}
}
my class KV does Iterator {
has $.backend;
has $.end;
has $.index = -1;
has int $on;
method pull-one() is raw {
$on++ %% 2
?? $!index < $!end # on the key now
?? ++$!index
!! IterationEnd
!! $!backend.AT-POS($!index) # on the value now
}
}
#--- Positional methods that *MAY* be implemented by the consumer --------------
method CLEAR() {
self.DELETE-POS($_) for (^$.elems).reverse;
}
method ASSIGN-POS($pos, \value) is raw {
self.AT-POS($pos) = value;
}
method STORE(*@values, :$initialize) {
self.CLEAR;
self.ASSIGN-POS($_,@values.AT-POS($_)) for ^@values;
self
}
#--- Array methods that *MAY* be implemented by the consumer -------------------
method new(::?CLASS:U: **@values is raw) {
self.CREATE.STORE(@values)
}
method iterator() { Iterate.new( :backend(self), :$.end ) }
method end() { $.elems - 1 }
method keys() { Seq.new( (^$.elems).iterator ) }
method values() { Seq.new( self.iterator ) }
method pairs() { (^$.elems).map: { $_ => self.AT-POS($_) } }
method shape() { (*,) }
method kv() { Seq.new( KV.new( :backend(self), :$.end ) ) }
method list() { List .from-iterator(self.iterator) }
method Slip() { Slip .from-iterator(self.iterator) }
method List() { List .from-iterator(self.iterator) }
method Array() { Array.from-iterator(self.iterator) }
method !append(@values) {
self.ASSIGN-POS(self.elems,$_) for @values;
self
}
method append(+@values is raw) { self!append(@values) }
method push( **@values is raw) { self!append(@values) }
method pop() {
if self.elems -> \elems {
self.DELETE-POS(elems - 1)
}
else {
[].pop # standard behaviour on empty arrays
}
}
method !prepend(@values) {
self.move-indexes-up(+@values);
self.ASSIGN-POS($_,@values.AT-POS($_)) for ^@values;
self
}
method prepend( +@values is raw) { self!prepend(@values) }
method unshift(**@values is raw) { self!prepend(@values) }
method shift() {
if self.elems -> \elems {
my \value = self.AT-POS(0)<>;
self.move-indexes-down(1);
value
}
else {
[].shift # standard behaviour on empty arrays
}
}
method gist() { '[' ~ self.Str ~ ']' }
method Str() { self.values.map( *.Str ).join(" ") }
method perl() {
self.perlseen(self.^name, {
~ self.^name
~ '.new('
~ self.map({$_<>.perl}).join(',')
~ ',' x (self.elems == 1 && self.AT-POS(0) ~~ Iterable)
~ ')'
})
}
method splice() { X::NYI.new( :feature<splice> ).throw }
method grab() { X::NYI.new( :feature<grab> ).throw }
# -- Internal subroutines and methods that *MAY* be implemented ----------------
# Move indexes up for the number of positions given, optionally from the
# given given position (defaults to start). Removes the original positions.
method move-indexes-up($up, $start = 0 --> Nil) {
for ($start ..^ $.elems).reverse {
if self.EXISTS-POS($_) {
is-container(my \value = self.DELETE-POS($_))
?? self.ASSIGN-POS($_ + $up, value)
!! self.BIND-POS( $_ + $up, value);
}
}
}
# Move indexes down for the number of positions given, optionally from the
# given position (which defaults to the number of positions to move down).
# Removes original positions.
method move-indexes-down($down, $start = $down --> Nil) {
for ($start ..^ $.elems).list -> $from {
my $to = $from - $down;
if self.EXISTS-POS($from) {
my \value = self.DELETE-POS($from); # something to move
if is-container(value) {
self.DELETE-POS($to); # could have been bound
self.ASSIGN-POS($to, value);
}
else {
self.BIND-POS($to, value); # don't care what it was
}
}
else {
self.DELETE-POS($to); # nothing to move
}
}
}
}
=begin pod
=head1 NAME
Array::Agnostic - be an array without knowing how
=head1 SYNOPSIS
use Array::Agnostic;
class MyArray does Array::Agnostic {
method AT-POS() { ... }
method BIND-POS() { ... }
method DELETE-POS() { ... }
method EXISTS-POS() { ... }
method elems() { ... }
}
my @a is MyArray = 1,2,3;
=head1 DESCRIPTION
This module makes an C<Array::Agnostic> role available for those classes that
wish to implement the C<Positional> role as an C<Array>. It provides all of
the C<Array> functionality while only needing to implement 5 methods:
=head2 Required Methods
=head3 method AT-POS
method AT-POS($position) { ... } # simple case
method AT-POS($position) { Proxy.new( FETCH => { ... }, STORE => { ... } }
Return the value at the given position in the array. Must return a C<Proxy>
that will assign to that position if you wish to allow for auto-vivification
of elements in your array.
=head3 method BIND-POS
method BIND-POS($position, $value) { ... }
Bind the given value to the given position in the array, and return the value.
=head3 method DELETE-POS
method DELETE-POS($position) { ... }
Mark the element at the given position in the array as absent (make
C<EXISTS-POS> return C<False> for this position).
=head3 method EXISTS-POS
method EXISTS-POS($position) { ... }
Return C<Bool> indicating whether the element at the given position exists
(aka, is B<not> marked as absent).
=head3 method elems
method elems(--> Int:D) { ... }
Return the number of elements in the array (defined as the index of the
highest element + 1).
=head2 Optional Methods (provided by role)
You may implement these methods out of performance reasons yourself, but you
don't have to as an implementation is provided by this role. They follow the
same semantics as the methods on the
L<Array object|https://docs.perl6.org/type/Array>.
In alphabetical order:
C<append>, C<Array>, C<ASSIGN-POS>, C<end>, C<gist>, C<grab>, C<iterator>,
C<keys>, C<kv>, C<list>, C<List>, C<new>, C<pairs>, C<perl>, C<pop>,
C<prepend>, C<push>, C<shape>, C<shift>, C<Slip>, C<STORE>, C<Str>, C<splice>,
C<unshift>, C<values>
=head2 Optional Internal Methods (provided by role)
These methods may be implemented by the consumer for performance reasons.
=head3 method CLEAR
method CLEAR(--> Nil) { ... }
Reset the array to have no elements at all. By default implemented by
repeatedly calling C<DELETE-POS>, which will by all means, be very slow.
So it is a good idea to implement this method yourself.
=head3 method move-indexes-up
method move-indexes-up($up, $start = 0) { ... }
Add the given value to the B<indexes> of the elements in the array, optionally
starting from a given start index value (by default 0, so all elements of the
array will be affected). This functionality is needed if you want to be able
to use C<shift>, C<unshift> and related functions.
=head3 method move-indexes-down
method move-indexes-down($down, $start = $down) { ... }
Subtract the given value to the B<indexes> of the elements in the array,
optionally starting from a given start index value (by default the same as
the number to subtract, so that all elements of the array will be affected.
This functionality is needed if you want to be able to use C<shift>,
C<unshift> and related functions.
=head2 Exported subroutines
=head3 sub is-container
my $a = 42;
say is-container($a); # True
say is-container(42); # False
Returns whether the given argument is a container or not. This can be handy
for situations where you want to also support binding, B<and> allow for
methods such as C<shift>, C<unshift> and related functions.
=head1 AUTHOR
Elizabeth Mattijsen <liz@wenzperl.nl>
Source can be located at: https://github.com/lizmat/Array-Agnostic .
Comments and Pull Requests are welcome.
=head1 COPYRIGHT AND LICENSE
Copyright 2018 Elizabeth Mattijsen
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod
# vim: ft=perl6 expandtab sw=4