Permalink
Browse files

Implement RPA.grep and rebootstrap setting.

  • Loading branch information...
1 parent feb29e6 commit 0bf82c932503c30fcdb1a01a67e854765f3e7b11 @bacek bacek committed Jan 27, 2011
Showing with 60 additions and 26 deletions.
  1. +11 −0 src/setting/ResizablePMCArray.pm
  2. +43 −25 src/stage0/nqp-setting.nqp
  3. +6 −1 t/setting/01-resizablepmcarray.t
@@ -48,6 +48,16 @@ module ResizablePMCArray {
@mapped;
}
+ =begin item grep
+ Return an array with elements matching code.
+ =end item
+
+ method grep (&code) {
+ my @grepped;
+ for self { @grepped.push($_) if &code($_) };
+ @grepped;
+ }
+
=begin item reverse
Return a reversed copy of the invocant.
=end item
@@ -62,6 +72,7 @@ module ResizablePMCArray {
our sub join ($separator, *@values) { @values.join($separator); }
our sub map (&code, *@values) { @values.map(&code); }
+our sub grep (&code, *@values) { @values.grep(&code); }
our sub list (*@values) { @values; }
# vim: ft=perl6
@@ -37,7 +37,7 @@ module ResizablePMCArray {
Return all elements joined by $sep.
=end item
- method join ($separator) {
+ method join ($separator = '') {
pir::join($separator, self);
}
@@ -54,6 +54,16 @@ module ResizablePMCArray {
@mapped;
}
+ =begin item grep
+ Return an array with elements matching code.
+ =end item
+
+ method grep (&code) {
+ my @grepped;
+ for self { @grepped.push($_) if &code($_) };
+ @grepped;
+ }
+
=begin item reverse
Return a reversed copy of the invocant.
=end item
@@ -68,6 +78,7 @@ module ResizablePMCArray {
our sub join ($separator, *@values) { @values.join($separator); }
our sub map (&code, *@values) { @values.map(&code); }
+our sub grep (&code, *@values) { @values.grep(&code); }
our sub list (*@values) { @values; }
# vim: ft=perl6
@@ -217,31 +228,38 @@ our sub subst ($text, $regex, $repl, :$global?) {
Splits C<$text> on occurences of C<$regex>
=end item
-# our sub split ($regex, $text) {
-# my $pos := 0;
-# my @result;
-# my $looking := 1;
-# while $looking {
-# my $match :=
-# Regex::Cursor.parse($text, :rule($regex), :c($pos)) ;
-#
-# if ?$match {
-# my $from := $match.from();
-# my $to := $match.to();
-# my $prefix := pir::substr__sPii($text, $pos, $from-$pos);
-# @result.push($prefix);
-# $pos := $match.to();
-# } else {
-# my $len := pir::length($text);
-# if $pos < $len {
-# @result.push(pir::substr__ssi($text, $pos) );
-# }
-# $looking := 0;
-# }
-# }
-# return @result;
-# }
+our multi sub split (Regex::Regex $regex, $text) {
+ my $pos := 0;
+ my @result;
+ my $looking := 1;
+ while $looking {
+ my $match :=
+ Regex::Cursor.parse($text, :rule($regex), :c($pos)) ;
+
+ if ?$match {
+ my $from := $match.from();
+ my $to := $match.to();
+ my $prefix := pir::substr__sPii($text, $pos, $from-$pos);
+ @result.push($prefix);
+ $pos := $match.to();
+ } else {
+ my $len := pir::length($text);
+ if $pos < $len {
+ @result.push(pir::substr__ssi($text, $pos) );
+ }
+ $looking := 0;
+ }
+ }
+ return @result;
+}
+# Use parrot's split for plain strings.
+our multi sub split($string, $text) {
+ # op split produces RSA. So, convert it to RPA.
+ my @res;
+ @res.push($_) for pir::split($string, $text);
+ @res;
+}
# vim: ft=perl6
# From src/setting/IO.pm
@@ -5,7 +5,7 @@ pir::load_bytecode('nqp-setting.pbc');
my @array := <0 1 2>;
my @reversed := @array.reverse();
-plan(13);
+plan(16);
ok( @reversed[0] == 2, 'First element correct');
ok( @reversed[1] == 1, 'Second element correct');
@@ -31,4 +31,9 @@ my @res := @test.map(-> $a { $a~$a; });
ok( +@res == 3, 'Map produced same number of elements');
ok( @res.join() eq '112233', 'Map produced correct elements');
+@res := @test.grep(-> $a { $a % 2 });
+ok( +@res == 2, 'Grep produced correct number of elements');
+ok( @res[0] == 1, 'Grep produced correct elements');
+ok( @res[1] == 3, 'Grep produced correct elements');
+
# vim: ft=perl6

0 comments on commit 0bf82c9

Please sign in to comment.