Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement more .tree forms
  • Loading branch information
moritz committed Feb 6, 2012
1 parent 5a2c7d2 commit 33fb020
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/Any.pm
Expand Up @@ -16,7 +16,7 @@ my class Any {
method uniq() { self.list.uniq }
method infinite() { Mu }
method flat() { nqp::p6list(nqp::list(self), List, Bool::True) }
method tree() { self.list.tree }
method tree(*@a) { self.list.tree(|@a) }
method hash() { my %h = self }
method list() { nqp::p6list(nqp::list(self), List, Mu) }
method pick($n = 1) { self.list.pick($n) }
Expand Down
24 changes: 21 additions & 3 deletions src/core/List.pm
Expand Up @@ -28,9 +28,27 @@ my class List does Positional {
method list() { self }
method flattens() { $!flattens }

method tree() {
MapIter.new(:list(self), :block({.elems == 1 ?? $_ !! [.list] })).list;
}
my &itemify = { .elems == 1 ?? $_ !! [.list] };
proto method tree(|$) {*}
multi method tree(List:U:) { self }
multi method tree(List:D:) {
MapIter.new(:list(self), :block(&itemify)).list;
}
multi method tree(List:D: Cool $count as Int) {
$count <= 0 ?? self
!! $count == 1 ?? self.tree
!! MapIter.new(
:list(self),
:block({.elems == 1 ?? $_ !! [.tree($count - 1)]})
).list;
}
multi method tree(List:D: &c) {
MapIter.new(:list(self), :block(&c)).list;
}
# uncommenting causes "Circularity detected in multi sub types"
# multi method tree(List:D: *@ [$first, *@rest] where {.elems >= 2 }) {
# MapIter.new(:list(self), :block(*.list(|@rest))).list.tree($first)
# }

method Capture() {
self.gimme(*);
Expand Down

0 comments on commit 33fb020

Please sign in to comment.