Skip to content

Commit

Permalink
Implement Cool.grep, Cool.map, Cool.for
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Jul 25, 2010
1 parent 8e38cd1 commit a6f61b0
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion CORE.setting
Expand Up @@ -455,7 +455,7 @@ sub unfold($fn) {
@l;
}

my class Parcel {
my class Parcel is Cool {
# $!ll

method flat() {
Expand Down Expand Up @@ -584,4 +584,41 @@ my class List is Cool {
}
}
sub _it_shift($it) {
$it.validate;
my $oit = $it;
$it = $it.next;
$oit.value;
}
PRE-INIT {
Cool.HOW.add-method(Q:CgOp { (w (clr_string "grep")) },
anon method grep($sm) {
my $it = self.flat.iterator;

unfold(sub () {
my $item = _it_shift($it);
($item === EMPTY) ?? EMPTY !!
($item ~~ $sm) ?? $item !! Nil;
});
});
Cool.HOW.add-method(Q:CgOp { (w (clr_string "map")) },
anon method map($func) {
my $it = self.flat.iterator;
unfold(sub () {
my $item = _it_shift($it);
($item === EMPTY) ?? EMPTY !! $func($item);
});
});
Cool.HOW.add-method(Q:CgOp { (w (clr_string "for")) },
anon method for($func) {
my $it = self.flat.iterator;
while $it {
my $item = _it_shift($it);
($item === EMPTY) ?? ($it = Any) !! ($func($item));
}
});
}

{YOU_ARE_HERE}

0 comments on commit a6f61b0

Please sign in to comment.