Tree datastructures: Difference between revisions
Content added Content deleted
(→{{header|zkl}}: added code) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
||
Line 159: | Line 159: | ||
Round trip test satisfied? true |
Round trip test satisfied? true |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2019.07.1}} |
|||
Code golf is a entertaining passtime, even if it isn't appropriate for this site. To a large extent, I agree with [[User:Hout|Hout]], I am not really on board with mocking anybody, especially espousing it as an official RosettaCode position. So, feel free to mark this incorrect. |
|||
<lang perl6>#`( |
|||
Sort of vague as to what we are trying to accomplish here. If we are just |
|||
trying to transform from one format to another, probably easiest to just |
|||
perform string manipulations. |
|||
) |
|||
my $level = ' '; |
|||
my $tree = q:to/END/; |
|||
RosettaCode |
|||
encourages |
|||
code |
|||
diversity |
|||
comparison |
|||
discourages |
|||
golfing |
|||
trolling |
|||
emphasising execution speed |
|||
code-golf.io |
|||
encourages |
|||
golfing |
|||
discourages |
|||
comparison |
|||
END |
|||
sub nested-to-indent { $^str.subst: /^^ ($($level))*/, -> $/ { "{+$0} " }, :g } |
|||
sub indent-to-nested { $^str.subst: /^^ (\d+) \s* /, -> $/ { "{$level x +$0}" }, :g } |
|||
say $tree; |
|||
say my $indent = $tree.&nested-to-indent; |
|||
say my $nest = $indent.&indent-to-nested; |
|||
use Test; |
|||
is($tree, $nest, 'Round-trip equals original'); |
|||
#`( |
|||
If, on the other hand, we want perform more complex transformations, better to |
|||
load it into a native data structure, which will then allow us to manipulate it |
|||
however we like. |
|||
) |
|||
my @forest = $tree.comb( /^^\S.+? <?before $ | ^^\S>/); |
|||
my $forest; |
|||
{ |
|||
my $last = -1; |
|||
for @forest -> $tree { |
|||
for $tree.lines -> $line { |
|||
$line ~~ /^^ ($($level))* /; |
|||
given (my $this = +$0) cmp $last { |
|||
when More { $forest ~= "\['{$line.trim}', "; $last = $this } |
|||
when Same { $forest ~= "'{$line.trim}', " } |
|||
when Less { $forest ~= "{']' x $last - $this}, '{$line.trim}', "; $last = $this } |
|||
} |
|||
} |
|||
} |
|||
$forest ~= "{']' x 1 + $last}"; |
|||
use MONKEY-SEE-NO-EVAL; |
|||
$forest.=EVAL; |
|||
} |
|||
say "\nNative data structure:\n", $forest.perl; |
|||
{ |
|||
use JSON::Fast; |
|||
say "\nJSON:\n", $forest.&to-json; |
|||
} |
|||
{ |
|||
use YAML; |
|||
say "\nYAML:\n", $forest.&dump; |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre>RosettaCode |
|||
encourages |
|||
code |
|||
diversity |
|||
comparison |
|||
discourages |
|||
golfing |
|||
trolling |
|||
emphasising execution speed |
|||
code-golf.io |
|||
encourages |
|||
golfing |
|||
discourages |
|||
comparison |
|||
0 RosettaCode |
|||
1 encourages |
|||
2 code |
|||
3 diversity |
|||
3 comparison |
|||
1 discourages |
|||
2 golfing |
|||
2 trolling |
|||
2 emphasising execution speed |
|||
0 code-golf.io |
|||
1 encourages |
|||
2 golfing |
|||
1 discourages |
|||
2 comparison |
|||
RosettaCode |
|||
encourages |
|||
code |
|||
diversity |
|||
comparison |
|||
discourages |
|||
golfing |
|||
trolling |
|||
emphasising execution speed |
|||
code-golf.io |
|||
encourages |
|||
golfing |
|||
discourages |
|||
comparison |
|||
ok 1 - Round-trip equals original |
|||
Native data structure: |
|||
$["RosettaCode", ["encourages", ["code", ["diversity", "comparison"]], "discourages", ["golfing", "trolling", "emphasising execution speed"]], "code-golf.io", ["encourages", ["golfing"], "discourages", ["comparison"]]] |
|||
JSON: |
|||
[ |
|||
"RosettaCode", |
|||
[ |
|||
"encourages", |
|||
[ |
|||
"code", |
|||
[ |
|||
"diversity", |
|||
"comparison" |
|||
] |
|||
], |
|||
"discourages", |
|||
[ |
|||
"golfing", |
|||
"trolling", |
|||
"emphasising execution speed" |
|||
] |
|||
], |
|||
"code-golf.io", |
|||
[ |
|||
"encourages", |
|||
[ |
|||
"golfing" |
|||
], |
|||
"discourages", |
|||
[ |
|||
"comparison" |
|||
] |
|||
] |
|||
] |
|||
YAML: |
|||
--- |
|||
- RosettaCode |
|||
- - encourages |
|||
- - code |
|||
- - diversity |
|||
- comparison |
|||
- discourages |
|||
- - golfing |
|||
- trolling |
|||
- emphasising execution speed |
|||
- code-golf.io |
|||
- - encourages |
|||
- - golfing |
|||
- discourages |
|||
- - comparison |
|||
...</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |