Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix POD, fix bug in 3rd example
  • Loading branch information
Salve J. Nilsen committed Apr 21, 2012
1 parent 86ca520 commit b9095f2
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions 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.
Expand All @@ -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 "]" {
Expand All @@ -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

Expand Down

0 comments on commit b9095f2

Please sign in to comment.