Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make .squish()/.squish(:with) about 2.5x faster
  • Loading branch information
lizmat committed Sep 23, 2015
1 parent 1247986 commit 747c69f
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/core/Any-iterable-methods.pm
Expand Up @@ -773,15 +773,39 @@ augment class Any {
self.is-lazy ?? res.lazy !! res
}
multi method squish( :&with = &[===] ) {
my $last;
my \res := gather self.map: {
once { take $_; $last = $_; next };
unless with($_,$last) {
$last = $_;
take $_;
Seq.new(class :: does Iterator {
has Mu $!iter;
has &!with;
has $!last;
method BUILD(\list, &!with) {
$!iter = as-iterable(list).iterator;
$!last;
self
}
}
self.is-lazy ?? res.lazy !! res
method new(\list, &with) { nqp::create(self).BUILD(list, &with) }
method pull-one() {
my Mu $value;
until ($value := $!iter.pull-one) =:= IterationEnd {
once { $!last = $value; return $value }
unless with($value,$!last) {
$!last = $value;
return $value;
}
}
IterationEnd
}
method push-all($target) {
my Mu $value;
until ($value := $!iter.pull-one) =:= IterationEnd {
once { $!last = $value; $target.push($value); next }
unless with($value,$!last) {
$!last = $value;
$target.push($value);
}
}
IterationEnd
}
}.new(self, &with))
}

proto method pairup(|) is nodal { * }
Expand Down

0 comments on commit 747c69f

Please sign in to comment.