<commit_before>use v6;

=begin pod

=TITLE Balanced brackets

=AUTHOR <NAME>

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.

=head1 More

L<http://rosettacode.org/wiki/Balanced_brackets#Perl_6>

=head1 What's interesting here?

=item idiomatic solutions
=item hyper operators
=item switch statement
=item roll
=item grammar

=head2 Depth counter

=end pod

{
    sub balanced($s) {
        my $l = 0;
        for $s.comb {
            when "]" {
                --$l;
                return False if $l < 0;
            }
            when "[" {
                ++$l;
            }
        }
        return $l == 0;
    }

    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) {
        .none < 0 and .[*-1] == 0
        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";
}

=begin pod

=head2 String munging

=end pod

{
    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";
}

=begin pod

=head2 Parsing with a grammar

=end pod

{

    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";

}

=begin pod

=head1 Features used

=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

# vim: expandtab shiftwidth=4 ft=perl6
<commit_msg>Use consistent prompts in balanced-brackets<commit_after>use v6;

=begin pod

=TITLE Balanced brackets

=AUTHOR <NAME>

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.

=head1 More

L<http://rosettacode.org/wiki/Balanced_brackets#Perl_6>

=head1 What's interesting here?

=item idiomatic solutions
=item hyper operators
=item switch statement
=item roll
=item grammar

=head2 Depth counter

=end pod

{
    sub balanced($s) {
        my $l = 0;
        for $s.comb {
            when "]" {
                --$l;
                return False if $l < 0;
            }
            when "[" {
                ++$l;
            }
        }
        return $l == 0;
    }

    my $n = prompt "Number of bracket pairs: ";
    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) {
        .none < 0 and .[*-1] == 0
        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";
}

=begin pod

=head2 String munging

=end pod

{
    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";
}

=begin pod

=head2 Parsing with a grammar

=end pod

{

    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";

}

=begin pod

=head1 Features used

=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

# vim: expandtab shiftwidth=4 ft=perl6

{'fraction_non_alphanumeric': 0.17039288039939224, 'fraction_numerical': 0.012155415671803777, 'mean_word_length': 2.4108068097705404, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5101877', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1668, 'n_words': 477}