Permalink
Browse files

Fix POD, fix bug in 3rd example

  • Loading branch information...
1 parent 86ca520 commit b9095f241ce1f6488028d262082224c8b739d2da Salve J. Nilsen committed Apr 21, 2012
Showing with 45 additions and 32 deletions.
  1. +45 −32 best-of-rosettacode/balanced-brackets.pl
@@ -1,6 +1,8 @@
+use v6;
+
=begin pod
-=head1 Problem
+=head1 Balanced brackets
Generate a string with N opening brackets (“[”) and N closing brackets (“]”), in some arbitrary order.
Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.
@@ -10,17 +12,19 @@ =head1 More
L<http://rosettacode.org/wiki/Balanced_brackets#Perl_6>
=head1 What's interesting here?
-* idiomatic solutions
-* hyper operators
-* switch statement
-* roll
-* grammar
+
+=item idiomatic solutions
+=item hyper operators
+=item switch statement
+=item roll
+=item grammar
=head2 Depth counter
=end pod
-sub balanced($s) {
+{
+ sub balanced($s) {
my $l = 0;
for $s.comb {
when "]" {
@@ -32,65 +36,74 @@ ($s)
}
}
return $l == 0;
-}
+ }
-my $n = prompt "Number of brackets";
-my $s = (<[ ]> xx $n).pick(*).join;
-say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"
+ my $n = prompt "Number of brackets >";
+ my $s = (<[ ]> xx $n).pick(*).join;
+ say "$s {balanced($s) ?? "is" !! "is not"} well-balanced";
+}
=begin pod
=head2 FP oriented
=end pod
-sub balanced($s) {
+{
+ sub balanced($s) {
.none < 0 and .[*-1] == 0
- given [\+] '\\' «leg« $s.comb;
-}
+ given [\+] '\\' «leg« $s.comb;
+ }
-my $n = prompt "Number of bracket pairs: ";
-my $s = <[ ]>.roll($n*2).join;
-say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"
+ my $n = prompt "Number of bracket pairs: ";
+ my $s = <[ ]>.roll($n*2).join;
+ say "$s { balanced($s) ?? "is" !! "is not" } well-balanced";
+}
=begin pod
=head2 String munging
=end pod
-sub balanced($_ is copy) {
- () while s:g/'[]'//;
+{
+ sub balanced($_ is copy) {
+ s:g/'[]'// while m/'[]'/;
$_ eq '';
-}
+ }
-my $n = prompt "Number of bracket pairs: ";
-my $s = <[ ]>.roll($n*2).join;
-say "$s is", ' not' xx not balanced($s)), " well-balanced";
+ my $n = prompt "Number of bracket pairs: ";
+ my $s = <[ ]>.roll($n*2).join;
+ say "$s is", ' not' xx not balanced($s), " well-balanced";
+}
=begin pod
=head2 Prasing with a grammar
=end pod
-grammar BalBrack {
+{
+
+ grammar BalBrack {
token TOP { ^ <balanced>* $ };
token balanced { '[]' | '[' ~ ']' <balanced> }
+ }
+
+ my $n = prompt "Number of bracket pairs: ";
+ my $s = <[ ]>.roll($n*2).join;
+ say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";
+
}
-
-my $n = prompt "Number of bracket pairs: ";
-my $s = <[ ]>.roll($n*2).join;
-say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";
=begin pod
=head1 Features used
-C<roll> - L<http://perlcabal.org/syn/S32/Containers.html#roll>
-C<given> - L<http://perlcabal.org/syn/S04.html#Switch_statements>
-C<prompt> - L<http://perlcabal.org/syn/S32/IO.html#prompt>
-C<grammar> - L<http://perlcabal.org/syn/S05.html#Grammars>
+=item C<roll> - L<http://perlcabal.org/syn/S32/Containers.html#roll>
+=item C<given> - L<http://perlcabal.org/syn/S04.html#Switch_statements>
+=item C<prompt> - L<http://perlcabal.org/syn/S32/IO.html#prompt>
+=item C<grammar> - L<http://perlcabal.org/syn/S05.html#Grammars>
=end pod

0 comments on commit b9095f2

Please sign in to comment.