Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
jnthn committed Nov 13, 2010
1 parent 784b227 commit 08510b7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions common/NQP/NQPSetting.pm
Expand Up @@ -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)
Expand All @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 08510b7

Please sign in to comment.