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#Raku
What's interesting here?
idiomatic solutions
hyper operators
switch statement
roll
grammar
Features used
given
- https://doc.perl6.org/syntax/given#language_documentation_Control_Flowprompt
- https://doc.perl6.org/routine/promptgrammar
- https://doc.perl6.org/language/grammars
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"; }