/
Seq.pm
81 lines (72 loc) · 2.59 KB
/
Seq.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
augment class Seq {
multi method ACCEPTS(@topic) {
my $self_it = self.iterator();
my $topic_it = @topic.iterator();
loop {
my $cur_self_elem = $self_it.get;
if $cur_self_elem ~~ EMPTY { last }
if $cur_self_elem ~~ Whatever {
# If we just have * left, we're done. Otherwise, we have a
# "target" to look for.
loop {
$cur_self_elem = $self_it.get;
if $cur_self_elem ~~ EMPTY { return True }
unless $cur_self_elem ~~ Whatever {
last;
}
}
# Need to find our target in the topic, if possible.
loop {
my $cur_topic_elem = $topic_it.get;
if $cur_topic_elem ~~ EMPTY {
# Ran out before finding what we wanted.
return False;
}
elsif $cur_topic_elem === $cur_self_elem {
last;
}
}
}
else {
my $cur_topic_elem = $topic_it.get;
if $cur_topic_elem ~~ EMPTY || $cur_topic_elem !=== $cur_self_elem {
return False;
}
}
}
# If we've nothing left to match, we're successful.
$topic_it.get ~~ EMPTY
}
multi method ACCEPTS($topic) {
self.ACCEPTS(@($topic))
}
method elems() { pir::set__IP(self!fill); }
method Str() {
pir::join(' ', self!fill);
}
multi method Bool() {
self!fill(1) ?? Bool::True !! Bool::False;
}
multi method sort(&by = &infix:<cmp>) {
# Parrot already provides a sort method that works on
# ResizablePMCArray, so we aim to make use of that here.
# Instead of sorting the elements directly, we sort an RPA
# of indices (from 0 to $list.elems), then use that RPA
# as a slice into self.
# If &by.arity < 2, then it represents a block to be applied
# to the elements to obtain the values for sorting.
if (&by.?arity // 2) < 2 {
my $list = self.map(&by).eager;
self[(^pir::elements($list)).eager.sort(
-> $a, $b { $list[$a] cmp $list[$b] || $a <=> $b }
)];
}
else {
my $list = self.eager;
self[(^pir::elements($list)).eager.sort(
-> $a, $b { &by($list[$a],$list[$b]) || $a <=> $b }
)];
}
}
}
# vim: ft=perl6