-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Any-list.pm
278 lines (247 loc) Β· 7.34 KB
/
Any-list.pm
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
class Range { ... }
augment class Any {
our Str multi method join($separator = '') {
pir::join__SsP($separator, self.list.eager);
}
multi method elems() {
1;
}
our multi method map(&block) {
Q:PIR {
.local pmc self, block, map
self = find_lex 'self'
block = find_lex '&block'
$P0 = self.'list'()
$P0 = $P0.'iterator'()
map = new ['MapIterator']
setattribute map, '$!iter', $P0
setattribute map, '&!block', block
%r = map
};
}
multi method first($test) {
for @.list {
if $_ ~~ $test {
return $_;
}
}
fail('No values matched');
}
our multi method grep(Mu $test) {
gather {
for @.list {
take $_ if $_ ~~ $test;
}
}
}
multi method reverse() {
my @result = ();
for @.list {
@result.unshift($_);
}
return @result;
}
multi method end() { self.elems - 1; }
# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method min($by = { $^a cmp $^b}) {
my $min = +Inf;
my $first-time = Bool::True;
for @.list {
if $first-time {
$min = $_;
$first-time = Bool::False;
next;
}
if $by($_, $min) == -1 {
$min = $_;
}
}
$min;
}
# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method max($by = { $^a cmp $^b}) {
my $max = -Inf;
my $first-time = Bool::True;
for @.list {
if $first-time {
$max = $_;
$first-time = Bool::False;
next;
}
if $by($_, $max) == 1 {
$max = $_;
}
}
$max;
}
# CHEAT: this should take an ordering parameter
# And use the FIRST: phaser
multi method minmax($by = { $^a cmp $^b}) {
my $min = +Inf;
my $max = -Inf;
my $excludes_min = Bool::False;
my $excludes_max = Bool::False;
my $first-time = Bool::True;
for @.list {
when Range {
if $first-time {
$min = $_.min;
$max = $_.max;
$excludes_min = $_.excludes_min;
$excludes_max = $_.excludes_max;
$first-time = Bool::False;
next;
}
if $by($_.min, $min) == -1 {
$min = $_;
$excludes_min = $_.excludes_min;
}
if $by($_.max, $max) == 1 {
$max = $_;
$excludes_max = $_.excludes_max;
}
}
if $first-time {
$min = $_;
$max = $_;
$first-time = Bool::False;
next;
}
if $by($_, $min) == -1 {
$min = $_;
$excludes_min = Bool::False;
}
if $by($_, $max) == 1 {
$max = $_;
$excludes_max = Bool::False;
}
}
Range.new($min,
$max,
:excludes_min($excludes_min),
:excludes_max($excludes_max));
}
#CHEAT: Simplified version which we can hopefully sneak by ng.
multi method pick() {
my @l = @.list.Seq;
@l[floor(@l.elems.rand)];
}
multi method pick($num is copy = 1, :$replace) {
my @l = @.list.Seq;
if ($num == 1) {
return @l[floor(@l.elems.rand)];
}
if $replace {
gather {
while ($num > 0) {
my $idx = floor(@l.elems.rand());
take @l[$idx];
--$num;
}
}
} else {
gather {
while ($num > 0 and @l.elems > 0) {
my $idx = floor(@l.elems.rand());
take @l[$idx];
@l.splice($idx,1);
--$num;
}
}
}
}
multi method pick(Whatever, :$replace) {
self.pick(Inf, :$replace);
}
multi method classify($test) {
my %result;
for @.list {
my $k = $_ ~~ $test;
%result{$k} //= [];
%result{$k}.push: $_;
}
%result.pairs;
}
multi method reduce(Code $expression is rw) {
my $arity = $expression.?count || 2; # second half is a CHEAT
fail('Cannot reduce() using a unary or nullary function.')
if $arity < 2;
fail('Can only reduce() using a binary function for now.')
if $arity > 2;
my @args = ();
for @.list {
@args.push($_);
if (@args == $arity) {
my $res = $expression.(@args[0], @args[1]);
# my $res = $expression.(|@args);
@args = ($res);
}
}
fail('Cannot reduce() empty list') unless @args > 0;
if @args > 1 {
if @args < $expression.arity {
warn (@args -1) ~ " trailing item(s) in reduce";
} else {
return $( $expression.(@args[0], @args[1]) );
# return $( $expression.(|@args) );
}
}
return @args[0];
}
# This needs a way of taking a user-defined comparison
# specifier, but AFAIK nothing has been spec'd yet.
# CHEAT: Almost certainly should be hashed on something
# other than the stringification of the objects.
multi method uniq() {
my %seen;
gather for @.list {
unless %seen{$_} {
take $_;
%seen{$_} = 1;
}
}
}
multi method kv() {
my $i = 0;
gather for $.list -> $value {
my $key = $i++;
take $key;
take $value;
}
}
multi method keys() {
my $i = 0;
gather for $.list -> $value {
my $key = $i++;
take $key;
}
}
multi method values() {
gather for $.list -> $value {
take $value;
}
}
multi method pairs() {
self.kv.map(-> $key, $value { $key => $value; });
}
}
proto sub classify($matcher, *@values) { @values.classify($matcher) }
proto sub join (Str $separator = '', *@values) { @values.join($separator); }
proto sub reverse(@values) { @values.reverse; }
multi sub reverse(*@v) { @v.reverse; }
proto sub end(@array) { @array.end; }
proto sub grep(Mu $test, *@values) { @values.grep($test); }
proto sub first($test, @values) { @values.first($test); }
proto sub min($by, *@values) { @values.min($by); }
proto sub max($by, *@values) { @values.max($by); }
proto sub minmax($by, *@values) { @values.minmax($by); }
proto sub uniq(@values) { @values.uniq; }
proto sub pick ($num, :$replace, *@values) { @values.pick($num, :$replace); }
proto sub map(&mapper, @values) { @values.map(&mapper); }
proto sub kv(@array) { @array.kv; }
proto sub keys(@array) { @array.keys; }
proto sub values(@array) { @array.values; }
proto sub pairs(@array) { @array.pairs; }
# vim: ft=perl6