-
Notifications
You must be signed in to change notification settings - Fork 292
/
Any.pod6
292 lines (194 loc) · 7.02 KB
/
Any.pod6
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
=begin pod
=TITLE class Any
=SUBTITLE Thing/object
class Any is Mu {}
While L<Mu|/type/Mu> is the root of the Perl 6 class hierarchy, C<Any> is the class
that serves as a default base class for new classes, and as the base class for
most built-in classes.
Since Perl 6 intentionally confuses items and single-element lists, most
methods in C<Any> are also present on class L<List|/type/List>, and coerce to
List or a list-like type.
=head1 Methods
=head2 method ACCEPTS
Defined as:
=begin code :signature
multi method ACCEPTS(Any:D: Mu $other)
=end code
Usage:
=begin code :skip-test
EXPR.ACCEPTS(EXPR);
=end code
Returns C<True> if C<$other === self> (i.e. it checks object identity).
Many built-in types override this for more specific comparisons
=head2 method any
Defined as:
=begin code :signature
method any() returns Junction:D
=end code
Usage:
=begin code :skip-test
LIST.any
=end code
Interprets the invocant as a list and creates an
C<any>-L<Junction|/type/Junction> from it.
say so 2 == <1 2 3>.any; # True
say so 5 == <1 2 3>.any; # False
=head2 method all
Defined as:
=begin code :signature
method all() returns Junction:D
=end code
Usage:
=begin code :skip-test
LIST.all
=end code
Interprets the invocant as a list and creates an
C<all>-L<Junction|/type/Junction> from it.
say so 1 < <2 3 4>.all; # True
say so 3 < <2 3 4>.all; # False
=head2 method one
Defined as:
=begin code :signature
method one() returns Junction:D
=end code
Usage:
=begin code :skip-test
LIST.one
=end code
Interprets the invocant as a list and creates a
C<one>-L<Junction|/type/Junction> from it.
say so 1 == (1, 2, 3).one; # True
say so 1 == (1, 2, 1).one; # False
=head2 method none
Defined as:
=begin code :signature
method none() returns Junction:D
=end code
Usage:
=begin code :skip-test
LIST.none
=end code
Interprets the invocant as a list and creates a
C<none>-L<Junction|/type/Junction> from it.
say so 1 == (1, 2, 3).none; # False
say so 4 == (1, 2, 3).none; # True
=head2 method list
Interprets the invocant as a list, and returns that L<List|/type/List>.
say 42.list.^name; # List
say 42.list.elems; # 1
=head2 method push
The method push is defined for undefined invocants and allows for
autovivifying undefined to an empty C<Array>, unless the undefined value
implements C<Positional> already. The argument provided will then be pushed
into the newly created Array.
my %h;
dd %h<a>; # Any (and therefor undefined)
%h<a>.push(1); # .push on Any
dd %h; # «Hash %h = {:a($[1])}» # please note the Array
=head2 routine reverse
Defined as:
=begin code :signature
multi sub reverse(*@list ) returns List:D
multi method reverse(List:D:) returns List:D
=end code
Usage:
=begin code :skip-test
reverse(LIST);
LIST.reverse;
=end code
Returns a list with the same elements in reverse order.
Note that C<reverse> always refers to reversing elements of a list;
to reverse the characters in a string, use L<flip>.
Examples:
say <hello world!>.reverse; # (world! hello)
say reverse ^10; # (9 8 7 6 5 4 3 2 1 0)
=head2 method sort
Sorts iterables with C<infix:<cmp>> or given code object and returns a new C<List>.
Examples:
say <b c a>.sort; # (a b c)
say 'bca'.comb.sort.join; # abc
say 'bca'.comb.sort({$^b cmp $^a}).join; # cba
say '231'.comb.sort(&infix:«<=>»).join; # 123
=head2 method map
Defined as:
proto method map(|) is nodal { * }
=begin code :signature
multi method map(\SELF: █; :$label, :$item)
multi method map(HyperIterable:D: █; :$label)
=end code
C<map> will iterate over the invocant and apply the number of positional
parameters of the code object from the invocant per call. The returned values
of the code object will become elements of the returned C<Seq>.
=head2 method deepmap
Defined as:
=begin code :signature
method deepmap(&block -->List) is nodal
=end code
C<deepmap> will apply C<&block> to each element and return a new C<List> with
the return values of C<&block>, unless the element does the C<Iterable> role.
For those elements C<deepmap> will descend recursively into the sublist.
dd [[1,2,3],[[4,5],6,7]].deepmap(*+1);
# OUTPUT«[[2, 3, 4], [[5, 6], 7, 8]]»
=head2 method duckmap
Defined as:
=begin code :signature
method duckmap(&block) is rw is nodal
=end code
C<duckmap> will apply C<&block> on each element and return a new list with
defined return values of the block. For undefined return values, C<duckmap>
will try to descend into the element if that element implements C<Iterable>.
my @a = [1,[2,3],4];
dd @a.duckmap({ $_ ~~ Int ?? $_++ !! Any });
# OUTPUT«(1, (2, 3), 4)»
=head2 method flat
Interprets the invocant as a list, flattens it, and returns that list. Please
note that C<.flat> will not solve the halting problem for you. If you flat
an infinite list C<.flat> may return that infinite list, eating all your RAM
in the process.
say ((1, 2), (3)).elems; # 2
say ((1, 2), (3)).flat.elems; # 3
Please not that C<flat> is not recursing into sub lists. You have to recurse by
hand or reconsider your data structures. A single level of nesting can often be
handled with L<destructuring|/type/Signature#Destructuring_Parameters> in
signatures. For deeper structures you may consider
L<gather/take|/syntax/gather take> to create a lazy list.
my @a = [[1,2,3],[[4,5],6,7]];
sub deepflat(@a){
gather for @a {
take ($_ ~~ Iterable ?? deepflat($_).Slip !! $_)
}
};
dd deepflat(@a);
# OUTPUT«(1, 2, 3, 4, 5, 6, 7).Seq»
=head2 method eager
Interprets the invocant as a list, evaluates it eagerly, and returns that
list.
say (1..10).eager; # (1 2 3 4 5 6 7 8 9 10)
=head2 method elems
Interprets the invocant as a list, and returns the number of elements in the
list.
say 42.elems; # 1
say <a b c>.elems; # 3
=head2 method end
Interprets the invocant as a list, and returns the last index of that list.
say 6.end; # 0
say <a b c>.end; # 2
=head2 method pairup
=begin code :signature
method pairup() returns List
=end code
Interprets the invocant as a list, and constructs a list of
L<pairs|/type/Pair> from it, in the same way that assignment to a
L<Hash|/type/Hash> does. That is, it takes two consecutive elements and
constructs a pair from them, unless the item in the key position already is a
pair (in which case the pair is passed is passed through, and the next
list item, if any, is considered to be a key again).
say (a => 1, 'b', 'c').pairup.perl; # (:a(1), :b("c")).Seq
=head2 sub exit
=begin code :signature
sub exit(Int() $status = 0)
=end code
Exits the current process with return code C<$status>.
=comment TODO maybe find a better place to document &exit
=end pod
# vim: expandtab shiftwidth=4 ft=perl6