Balanced brackets: Difference between revisions

→‎{{header|J}}: break up check for readability, note issue with uneven brackets.
(→‎{{header|J}}: generate bracket pairs, rename checker)
(→‎{{header|J}}: break up check for readability, note issue with uneven brackets.)
Line 46:
=={{header|J}}==
'''Solution''': <lang j>genBracketPairs =: ?~@+: { #&'[]' NB. bracket pairs in arbitrary order
checkBalancedbracketDepth =: _1 *./ . < '[]' -&(+/\)/@:(=/) ]</lang>
checkBalanced =: _1 -.@e. bracketDepth</lang>
'''Examples''':<lang j> 'TESTS EXPECTED'=:( a:"_`0:`[} ,&< (<'OK') = ])/ |: cut;._2 noun define
(empty) OK
Line 72 ⟶ 73:
EXPECTED -: checkBalanced &> TESTS
1</lang>
'''Comments''': This task highlights the versatility and usefulness of J's scanning modifiers, <ttcode>/</ttcode> and <ttcode>\</ttcode>.
 
The <code>checkBalanced</code> verb would need modification if the task was extended to include uneven numbers of opening and closing brackets.
 
=={{header|Perl 6}}==
892

edits