Previous part: Balancing brackets - 2
Instead of taking a sequence of brackets, we would accept any ASCII
symbols. Let’s try on this:
We don’t have to touch old functions, it’s enough to pattern match on ASCII
character groups.
ASCII
is either aGlyph
or aCaret
modifying signal (non-printable character)Glyph
is either aNumber
, aLetter
or aSymbol
Symbol
is either a mark toPunctuate
or aBracket
How do I exactly know to get Bracket
from ASCII
? By looking at constructors here.
Having this information in mind, we can pattern match on exact types:
… and we would end up with many options! You can see how we literally case split:
If you rather want to show what is exactly you are supposed to handle, just evaluate subtraction:
The bright side of opened interface like ASCII
in ya-ascii package is that you have more freedom to refactor things. Since almost all operators are left biased (including Sum) you can rewrite the code snippet above to its shorter form:
Let’s say we actually don’t care about other characters except for brackets:
Full source code is available here.
Next time we are going to elaborate on error messages.