r/ProgrammingLanguages • u/TizioCaio84 • Apr 12 '21
What are some cool/wierd features of a programming language you know?
I'm asking this question out of curiosity and will to widen my horizons.
146
Upvotes
r/ProgrammingLanguages • u/TizioCaio84 • Apr 12 '21
I'm asking this question out of curiosity and will to widen my horizons.
1
u/b2gills Apr 21 '21
In Raku, regexes are code.
It's just code with a different base syntax and semantics.
As such they can have parameters and variables.
In the case of a regex like
rob|robertit acts a bit like this pseudo code:Note that it gets sorted by length of the part of the match we know about. (
rob.*|robertwould get sorted the same as above even thoughrob.*may end up longer.)It would even get sorted by specificity. so
robertwould be beforerob...but afterrobert.It of course can cheat:
This is important since Raku's sub languages can be embedded inside of the others.
If we rewrite into the pseudo language I have above:
If you were to re-write
rob|robertas a Raku grammar, you could have this:Note that it can still do the optimization upon the call to the
<name>method/regex.You have
rob|roberttransforming into another similar (but potentially faster) regexrob(ert)?, but in doing so you lose a piece of information. Effectively what Raku does is add branching paths instead. So then it can keep track of more information.I think that you are thinking about regexes as their own thing, separated from everything else. You also seem to think about it as something that is both flat and linear. If so, those thought processes are limiting how you think about them.
I got really good at regexes in Perl because I saw regexes as the programming language that they are. Regexes in Raku are nicer for the very reason that it expands upon this fact.