Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
minor style rewrite
- Loading branch information
Showing
1 changed file
with
13 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,29 @@ | ||
| use v6; | ||
|
|
||
| my $dna = get; | ||
| sub suffixes(Str $str) { map *.flip, [\~] $str.flip.comb } | ||
| sub suffix-tree(@a) { | ||
| if (@a == 0) { [] } | ||
| elsif (@a == 1) { return hash @a[0] => [] } | ||
| else { | ||
| return hash gather for @a.classify(*.substr(0, 1)) { | ||
| my $subtree = suffix-tree([ grep *.chars, map *.substr(1), .value[] ]); | ||
| if $subtree.elems == 1 { | ||
| my $pair = $subtree.pick; | ||
| take .key ~ $pair.key => $pair.value; | ||
| } else { | ||
| take .key => $subtree; | ||
| } | ||
| } | ||
| @a == 0 ?? [] !! | ||
| @a == 1 ?? hash @a[0] => [] !! | ||
| hash gather for @a.classify(*.substr(0, 1)) { | ||
| my $subtree = suffix-tree([ grep *.chars, map *.substr(1), .value[] ]); | ||
| if $subtree.elems == 1 { | ||
| my $pair = $subtree.pick; | ||
| take .key ~ $pair.key => $pair.value; | ||
| } else { | ||
| take .key => $subtree; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sub show-edges($tree) { | ||
| return if $tree.elems == 0; | ||
| for $tree[] { | ||
| say .key; | ||
| show-edges .value; | ||
| say .key; | ||
| show-edges .value; | ||
| } | ||
| } | ||
|
|
||
| my $tree = suffix-tree(suffixes($dna)); | ||
|
|
||
| show-edges $tree; | ||
| show-edges suffix-tree suffixes get; | ||
|
|
||
|
|
||
| # vim: ft=perl6 |