Balanced brackets

Author: Filip Sergot

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.

More

http://rosettacode.org/wiki/Balanced_brackets#Perl_6

What's interesting here?

  • idiomatic solutions

  • hyper operators

  • switch statement

  • roll

  • grammar

Features used

Depth counter

FP oriented

String munging

Parsing with a grammar

Source code: balanced-brackets.pl

use v6;

{

    grammar BalBrack {
        token TOP { ^ <balanced>* $ };
        token balanced { '[]' | '[' ~ ']' <balanced> }
    }

    my $s = <[ ]>.roll($n*2).join;
    say "Parsing brackets with a grammer";
    say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";

}