2
\$\begingroup\$

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

Please post one tip per answer.

\$\endgroup\$
  • \$\begingroup\$ It's not a great idea to accept an answer for a tips question \$\endgroup\$ – Jo King Oct 15 at 3:54
4
\$\begingroup\$

Brackets autocomplete

This is my biggest tip for anyone golfing in Keg: when writing programs, brackets used in loops or if-statements are automatically balanced if there are unclosed brackets.

For example:

(9|0.)

Can be shortened to:

(9|0.

And:

{:1=[1.]}

Can be shortened to:

{:1=[1.
\$\endgroup\$
2
\$\begingroup\$

Use Unicode characters when possible

In Keg, you can represent large constants using simply Unicode characters. Sometimes this is really hard to do, sometimes it is easy and can lead to creative solutions. And there is almost nothing to say about this; this is a canonical feature for golfed Keg programs.

\$\endgroup\$
1
\$\begingroup\$

Using the ; operator

The ; operator in Keg can be very powerful. For example for building NOT gates for if statements;

11$-[Blah|Blah2]

Can be golfed into:

1;[Blah|Blah2]
\$\endgroup\$
1
\$\begingroup\$

Use Functions where possible

Functions are defined using @ and store code that would potentially be repeated many times.

\$\endgroup\$
1
\$\begingroup\$

There's A Dictionary!

Like other golfing languages, Keg has a built-in ~60.5k word (~6.05 times better than 05AB1E's) dictionary that is available to be used. There are 6 ways to access the goodness that is words.txt:

  • Standard Spaceless Strings (Done with backticks)
  • Standard Spaceful Strings (Done with )
  • SCC-Only Spaceless Strings (Done with )
  • SCC-Only Spaceful Strings (Done with )
  • Special Spaceless Strings (Done with )
  • Special Spaceful Strings (Done with «)

Note that when the terms spaceful and spaceless are used, they refer to whether or not the string will place spaces between string compression codes when expanding.

I feel like I should take the time to explain what String Compression Codes are:

Every word within the Keg dictionary has a two byte code associated with it. These range from 00 through to ↫↫. When the uncompressor sees a string, it will go through and expand each SCC with it's corresponding word from the dictionary (for example, H% is Hello, is World and is Good). Even though all of the examples had uppercase letters, there are words starting with lower case letters.

Obtaining SCCs

The best and easiest way to find out what the required SCCs for programs is to navigate to uncompress.py and run the program (preferably in some sort of terminal/something that isn't IDLE). "But that's a library, not a user-friendly program!", I hear you say. Well, when run by a user as a program, it will branch into if __name__ == "__main__" and run a helpful session for golfing. If a word is in the dictionary, the corresponding SCC will be printed (single character SCCs start with 0), otherwise, -1 will be printed. Here's an example session:

Enter a search term: Hello
H% [43, 66]

Enter a search term: world
⬥ [188]

Enter a search term: Keg    
-1

Enter a search term: keg    
←z [130, 35]

Enter a search term: Um    
-1

Enter a search term: Keg   
-1

Enter a search term: isn't   
4🄂 [4, 239]

Enter a search term: in   
-1

Enter a search term: the  
0 [0]

Enter a search term: dictionary  
V¿ [57, 88]

Don't mind the fact that Keg isn't in the dictionary.

\$\endgroup\$
  • \$\begingroup\$ I feel like the amount of words in the dictionary does not mean it is a good dictionary. The extra words occur in strings less frequently and will be used less (they will be too space-consuming too). \$\endgroup\$ – A _ Oct 14 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.