Try it online!
What a mess!
$args, which is a single element array containing the RLE string, I'm forcing into an actual string by wrapping it quotes.
Then split it by word boundary (
\b in regex). That will give me an array of strings, where each element is either a number or the BF token(s) that come after the number. So in the example, the first 4 elements of this split array are
+> (all are string).
Next, I pipe that into
%) to deal with each element.
The middle is a well-known PowerShell golfism, with a twist; it's essentially a DIY ternary operator, in which you create a 2 element array then index into it using the boolean expression you want to test, whereby a false result gives you element 0 and a true result gives you element 1.
In this case, I actually create a single element array with the unary comma
, operator, because I don't want output in the true case.
First let's look at the indexer, even though it gets executed later.
The idea of this is that
$_ (the current element) could either be a valid number, or some other string. If it's a number, I want
$n to be the value of that number minus 1 (as a number, not a string). If it's not, I want
$n to be false-y.
PowerShell usually tries to coerce the right-hand value to the type of the left side, but it can depend on the operation. For addition,
"10"+5 would give you a new string,
10+"5" will give you an integer (
But strings can't be subtracted so instead PowerShell can infer the numeric value automatically with a string on the left side of subtraction, therefore
SO, I start with
$_-1, which will give me the number I want when
$_ is actually a number, but when it's not I get nothing. On the surface, "nothing" is falsey, but the problem is that is stops execution of that assignment, so
$n will retain its previous value; not what I want!
If I wrap it in a subexpression, then when it fails, I get my falsey value:
That all gets assigned to
$n and since that assignment is itself wrapped in parentheses, the value that was assigned to
$n also gets passed through to the pipeline.
Since I'm using it in the indexer, and I want
1 if the conversion succeeded, I use two boolean-
!! to convert this value to boolean. A successful number conversion ends up as true, while the falsey nothingness gives us that sweet, sweet
0 that allows for returning the only element in that fake ternary array.
Getting back to that array, the element is this:
"$($_)" - this is an annoyingly long way of getting the first character of the current element (let's say, getting
+[>+), but as a string and not as a
[char] object. I need it to be a string because I can multiply a string by a number to duplicate it, but I can't do that with a character.
Actually I managed to save 4 characters by using a
[char] array instead of a string (by using another unary comma
,), so I was able to remove the quotes and extra sub-expression. I can multiply an array to duplicate its elements. And since the entire result of this iteration ends up being an array anyway and needs to be
-joined, using an array here incurs no additional cost.
Then, I multiply that
string array by
$n, to duplicate it
$n times. Recall that
$n could be
$null or it could be the value of the preceding digits minus one.
+$_ adds the current element onto the end of the duplicated first character of that element. That's why
$n is minus one.
10+[>+ ends up with
$n equal to 9, then we make 9
+'s and add that back to the
+[>+ string to get the requisite 10, plus the other single elements along for the ride.
The element is wrapped in a subexpression
$() because when
$null, the entire expression fails, so creating the array fails, so the indexer never runs, so
$n never gets assigned.
The reason I used this ternary trick is because of one of its peculiarities: unlike a real ternary operator, the expressions that define the elements do get evaluated whether or not they are "selected", and first for that matter.
Since I need to assign and then use
$n on separate iterations, this is helpful. The ternary array element value gets evaluated with the previous iteration's
$n value, then the indexer re-assigns
$n for the current iteration.
ForEach-Object loops ends up outputting everything its supposed to (a bunch of errors we ignore), but as an array of new strings.
So that whole thing is wrapped in parentheses and then preceded by unary
-join to give the output string.