/
List.pm
47 lines (32 loc) · 893 Bytes
/
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
class List is also {
=begin item fmt
our Str multi List::fmt ( Str $format = '%s', $separator = ' ' )
Returns the invocant list formatted by an implicit call to C<sprintf> on each
of the elements, then joined with spaces or an explicitly given separator.
=end item
multi method fmt(Str $format = '%s', $sep = ' ') is export {
return join($sep, self.map({ sprintf($format, $^elem) }));
}
=begin item iterator()
Returns an iterator for the list.
=end item
method iterator() {
return Q:PIR {
self.'!flatten'()
%r = iter self
};
}
=begin item list
A List in list context returns itself.
=end item
method list() {
return self;
}
=begin item perl()
Returns a Perl representation of a List.
=end item
method perl() {
return '[' ~ self.map({ .perl }).join(", ") ~ ']';
}
}
# vim: ft=perl6