Permalink
Browse files

[common] Add an NQPMapIter that just knows how to eagerly reify a lis…

…t and a .map that returns it for lists and arrays.
  • Loading branch information...
1 parent 784b227 commit 08510b7d5134966657935cf419ffd1e8cb1311be @jnthn committed Nov 13, 2010
Showing with 30 additions and 0 deletions.
  1. +30 −0 common/NQP/NQPSetting.pm
View
@@ -85,6 +85,30 @@ my knowhow Any {
method defined() { 0 }
}
+my knowhow NQPMapIter {
+ has $!block;
+ has @!list;
+ method new($block, @list) {
+ my $iter := nqp::instance_of(self.WHAT);
+ $iter.BUILD($block, @list);
+ $iter
+ }
+ method BUILD($block, @list) {
+ $!block := $block;
+ @!list := @list;
+ }
+ method eager() {
+ my $i := 0;
+ my $elems := +@!list;
+ my @result;
+ while $i < $elems {
+ @result.push($!block(@!list[$i]));
+ $i := $i + 1;
+ }
+ @result
+ }
+}
+
my knowhow NQPList is repr('P6list') {
method new() {
nqp::instance_of(self.WHAT)
@@ -101,6 +125,9 @@ my knowhow NQPList is repr('P6list') {
method defined() {
nqp::repr_defined(self)
}
+ method map($block) {
+ NQPMapIter.new($block, self)
+ }
}
my knowhow NQPArray is repr('P6list') {
@@ -134,6 +161,9 @@ my knowhow NQPArray is repr('P6list') {
method defined() {
nqp::repr_defined(self)
}
+ method map($block) {
+ NQPMapIter.new($block, self)
+ }
}
my knowhow NQPHash is repr('P6mapping') {

0 comments on commit 08510b7

Please sign in to comment.