|
1 | 1 | use v6;
|
2 | 2 | use Test;
|
3 | 3 |
|
4 |
| -# L<S32::Containers/"List"/"=item last-index"> |
| 4 | +# L<S32::Containers/"List"/"=item first"> |
5 | 5 |
|
6 | 6 | plan 25;
|
7 | 7 |
|
8 | 8 | my @list = (1 ... 10);
|
9 | 9 |
|
10 | 10 | {
|
11 |
| - my $result = last-index { $^a % 2 }, |@list; |
12 |
| - ok($result ~~ Int, "last-index() returns an Int"); |
13 |
| - is($result, 8, "returned value by last-index() is correct"); |
| 11 | + my $result = first { $^a % 2 }, |@list, :end, :k; |
| 12 | + ok($result ~~ Int, "first(, :end, :k) returns an Int"); |
| 13 | + is($result, 8, "returned value by first() is correct"); |
14 | 14 | }
|
15 | 15 |
|
16 | 16 | {
|
17 |
| - my $result = last-index { $^a % 2 }, 1, 2, 3, 4, 5, 6, 7, 8; |
18 |
| - ok($result ~~ Int, "last-index() returns an Int"); |
19 |
| - is($result, 6, "returned value by last-index() is correct"); |
| 17 | + my $result = first { $^a % 2 }, 1, 2, 3, 4, 5, 6, 7, 8, :end, :k; |
| 18 | + ok($result ~~ Int, "first(, :end, :k) returns an Int"); |
| 19 | + is($result, 6, "returned value by first(, :end, :k) is correct"); |
20 | 20 | }
|
21 | 21 |
|
22 | 22 |
|
23 | 23 | {
|
24 |
| - my $result = @list.last-index( { $^a == 4} ); |
25 |
| - ok($result ~~ Int, "method form of last-index returns an Int"); |
26 |
| - is($result, 3, "method form of last-index returns the expected item"); |
| 24 | + my $result = @list.first( { $^a == 4}, :end, :k ); |
| 25 | + ok($result ~~ Int, "method form of first, :end, :k returns an Int"); |
| 26 | + is($result, 3, "method form of first, :end, :k returns the expected item"); |
27 | 27 | }
|
28 | 28 |
|
29 | 29 | #?rakudo skip "adverbial block RT #124754"
|
30 | 30 | #?niecza skip 'No value for parameter Mu $filter in CORE Any.first'
|
31 | 31 | {
|
32 |
| - my $result = @list.last-index():{ $^a == 4 }; |
33 |
| - ok($result ~~ Int, "last-index():<block> returns an Int"); |
34 |
| - is($result, 3, "last-index() returned the expected value"); |
| 32 | + my $result = @list.first():{ $^a == 4 }, :end, :k; |
| 33 | + ok($result ~~ Int, "first(, :end, :k):<block> returns an Int"); |
| 34 | + is($result, 3, "first(, :end, :k) returned the expected value"); |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | {
|
38 |
| - ok @list.last-index( { $^a == 11 } ) =:= Nil, 'last-index returns Nil on unsuccessful match'; |
| 38 | + ok @list.first( { $^a == 11 }, :end, :k ) =:= Nil, 'first, :end, :k returns Nil on unsuccessful match'; |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | {
|
42 | 42 | my $count = 0;
|
43 | 43 | my $matcher = sub (Int $x) { $count++; $x % 2 };
|
44 |
| - is @list.last-index($matcher), 8, 'last-index() search for odd elements successful'; |
45 |
| - is $count, 2, 'Matching closure in last-index() is only executed twice'; |
| 44 | + is @list.first($matcher, :end, :k), 8, 'first(, :end, :k) search for odd elements successful'; |
| 45 | + is $count, 2, 'Matching closure in first(, :end, :k) is only executed twice'; |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | {
|
49 |
| - is(@list.last-index(4..6), 5, "method form of last-index with range returns the expected item"); |
50 |
| - is(@list.last-index(4..^6), 4, "method form of last-index with range returns the expected item"); |
| 49 | + is(@list.first(4..6, :end, :k), 5, "method form of first, :end, :k with range returns the expected item"); |
| 50 | + is(@list.first(4..^6, :end, :k), 4, "method form of first, :end, :k with range returns the expected item"); |
51 | 51 | }
|
52 | 52 |
|
53 | 53 | {
|
54 | 54 | my @fancy_list = (1, 2, "Hello", 3/4, 4.Num);
|
55 |
| - is @fancy_list.last-index(Str), 2, "last-index by type Str works"; |
56 |
| - is @fancy_list.last-index(Int), 1, "last-index by type Int works"; |
57 |
| - is @fancy_list.last-index(Rat), 3, "last-index by type Rat works"; |
| 55 | + is @fancy_list.first(Str, :end, :k), 2, "first by type Str works"; |
| 56 | + is @fancy_list.first(Int, :end, :k), 1, "first by type Int works"; |
| 57 | + is @fancy_list.first(Rat, :end, :k), 3, "first by type Rat works"; |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | {
|
61 | 61 | my @fancy_list = <Philosopher Goblet Prince>;
|
62 |
| - is @fancy_list.last-index(/o/), 1, "last-index by regex /o/"; |
63 |
| - is @fancy_list.last-index(/ob/), 1, "last-index by regex /ob/"; |
64 |
| - is @fancy_list.last-index(/l.*o/), 0, "last-index by regex /l.*o/"; |
| 62 | + is @fancy_list.first(/o/, :end, :k), 1, "first by regex /o/"; |
| 63 | + is @fancy_list.first(/ob/, :end, :k), 1, "first by regex /ob/"; |
| 64 | + is @fancy_list.first(/l.*o/, :end, :k), 0, "first by regex /l.*o/"; |
65 | 65 | }
|
66 | 66 |
|
67 | 67 | {
|
68 |
| - is <a b c b a>.last-index('c' | 'b'), |
69 |
| - 3, '.last-index also takes a junction as matcher'; |
| 68 | + is <a b c b a>.first('c' | 'b', :end, :k), |
| 69 | + 3, '.first, :end, :k also takes a junction as matcher'; |
70 | 70 |
|
71 |
| - is (last-index 'c'|'b', <a b c b a>), |
72 |
| - 3, '.last-index also takes a junction as matcher (sub form)'; |
| 71 | + is (first 'c'|'b', <a b c b a>, :end, :k), |
| 72 | + 3, '.first, :end, :k also takes a junction as matcher (sub form)'; |
73 | 73 | }
|
74 | 74 |
|
75 | 75 | # Bool handling
|
76 | 76 | {
|
77 |
| - throws-like { last-index $_ == 1, 1,2,3 }, X::Match::Bool; |
78 |
| - throws-like { (1,2,3).last-index: $_== 1 }, X::Match::Bool; |
79 |
| - is last-index( Bool,True,False,Int ), 1, 'can we match on Bool as type'; |
80 |
| - is (True,False,Int).last-index(Bool), 1, 'can we match on Bool as type'; |
| 77 | + throws-like { first $_ == 1, 1,2,3, :end, :k }, X::Match::Bool; |
| 78 | + throws-like { (1,2,3).first: $_== 1, :end, :k }, X::Match::Bool; |
| 79 | + is first( Bool,True,False,Int, :end, :k ), 1, 'can we match on Bool as type'; |
| 80 | + is (True,False,Int).first(Bool, :end, :k), 1, 'can we match on Bool as type'; |
81 | 81 | }
|
82 | 82 |
|
83 | 83 | #vim: ft=perl6
|
0 commit comments