# Tips for golfing in Retina

What general tips do you have for golfing in Retina? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Retina (e.g. "remove comments" is not an answer). Please post one tip per answer.

For reference, the online compiler is here.

@Sp3000 pointed out there is also Tips for Regex Golf. Answers here should focus specifically on Retina features and not on general regex golfing tips.

• Related: Tips for regex golf – Sp3000 Feb 9 '16 at 21:29
• Hmmm, I've been holding off posting this because Retina is still much in development and I was afraid most answers would end up being plain regex golfing tips, not very specific to Retina. But we might as well give it a go, I guess... :) – Martin Ender Feb 9 '16 at 21:32
• @MartinBüttner You and some others have given me a lot of good tips and hints since I started looking at Retina, so I think its probably about time for this. I added a clarification that general regex tips should go to the linked question. – Digital Trauma Feb 9 '16 at 21:35
• @MartinBüttner Here is as good a place as any to ask - I've been wondering for a while - out of curiosity what is the inspiration for the name "Retina"? I assume the "Re" part is for Regular Expression, but what about the "tina"? – Digital Trauma Feb 9 '16 at 21:38
• @DigitalTrauma I was trying to come up with a nice word that would work as an acronym, but failed. The word "retina" was quite close to some of the attempts, and I liked the word. I never managed to retcon it into an acronym though and have since given up on that. So yeah the "re" is sort of for "regular expressions" and maybe the "n" for ".NET", but ultimately it's just a word that sounded nice. – Martin Ender Feb 9 '16 at 21:41

## Combine loops if possible

In non-trivial computations you'll often find yourself using several loops to process data:

+stage1
+stage2
+stage3


So this runs stage1 until the output converges, then stage2 until the output converges and then stage3 until the output converges.

However, it's always worth examining the stages in detail. Sometimes it's possible to run the loop in an interleaved fashion as stage1, stage2, stage3, stage1, stage2, stage3, ... instead (this depends a lot on what the stages actually do, but sometimes they make completely orthogonal changes or work well as a pipeline). In this case you can save bytes by wrapping them in a single loop:

{stage1
stage2
}stage3


If stage1 is the first stage or stage3 is the last stage of the program you can then even omit on of those parentheses as well (which means this can already save bytes for a loop of two stages).

A recent use of this technique can be seen in this answer.

## Splitting strings into chunks of equal length n

As in most "normal" languages TMTOWTDI (there's more than one way to do it). I'm assuming here that the input doesn't contain linefeeds, and that "splitting" means splitting it into lines. But there are two quite different goals: if the length of the string isn't a multiple of the chunk length, do you want to keep the incomplete trailing chunk or do you want to discard it?

### Keeping an incomplete trailing chunk

In general, there are three ways to go about the splitting in Retina. I'm presenting all three approaches here, because they might make a bigger difference when you try to adapt them to a related problem. You can use a replacement and append a linefeed to each match:

.{n}

### Bonus: overlapping chunks

Remember that M has the & option which returns overlapping matches (which is normally not possible with regex). This allows you to get all overlapping chunks (substrings) of a string of a given length:

M!&.{n}

• Is it somehow possible to split a string exactly in halve with variable length? So 123456 becomes 123\n456 and 1234567890 becomes 12345\n67890? – Kevin Cruijssen Oct 25 '18 at 18:47
• @KevinCruijssen I don't think I added any specific feature for that. You'll probably have to use balancing groups: tio.run/##K0otycxLNPyvquGe8D/YIEHD3sZWQ09TW1PD3hbI1jW0A3JUNP//… If you don't mind a trailing linefeed, you can omit the ?=`. – Martin Ender Oct 26 '18 at 7:22
• I was able to complete the challenge where I thought I needed it differently, but the balancing groups is indeed very useful! I knew it had to be something along those lines, but my regex/Retina skills are not nearly good enough. Thanks for answering! :) – Kevin Cruijssen Oct 26 '18 at 7:26