# Tips for golfing in Element

Since my use of Element has increased recently, I am interested in tips for golfing programs written in it.

What general tips do you have for golfing in Element? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Element.

• I threw a link in because I found it, but if you'd rather put it somewhere else in the post I won't be offended if you revert it. Apr 17, 2015 at 14:03
• @manatwork Always check esolangs.org for the language. It's like wikipedia for esoteric programming languages. Here's Element: esolangs.org/wiki/Element Apr 17, 2015 at 14:22
• @mbomb007 I actually did not realize that there was an Esolangs page for it. Apr 17, 2015 at 14:37
• Ah, you updated the documentation. Nice. Apr 17, 2015 at 14:45

# Ways to put extra 0s on the stack

A lot of times, you will be faced with the following 4-character piece of code:

0 2@


Element's stack-item-movement operator @ is very general-purpose, sometimes too much so for golfing, since it always takes two arguments, which may need to be separated by a space. So, it can take several characters to perform a single movement.

Usually, there is a better way to do this.

You can often produce empty values from the hash. The code 2:0 2@ can almost always be shortened to 3:~2@ to save one character because chances are that nothing is stored in the hash for that particular key.

If the top thing on the stack is the input, you can sacrifice the newline at the end of it like so:

_0 2@
_)2@


In a limited number of cases, usually with 0 1@, you don't need the @ at all. This works with input or when putting a constant on the stack.

_0 1@
'_"

text 0 1@
'text"


## Performing logic operations on a list of numbers.

Let's say you had a simple task: Take a list of 5 numbers and verify that all of them are multiples of 7. (You can replace "multiple of 7" with any other test.) You will soon run into a problem: in order to perform a loop, you must put a value on the control stack, but this this can interfere with the logic that you are performing on the list of numbers.

The following solution does not work because the value 5 is accidentally included as one of the arguments in the chain of ORs.

5'[_7%?|]!"  #does not work


There are a few ways to overcome this.

5'0[_7%+]?!" #self-contained
5'[_7%+]?!"  #if main stack is empty
5'[_7%?!&]"  #main stack untouched, but consumes the 5


If the numbers you need to compare are already on the stack, then the above approach will not work.

2 3 7 4 8 5'[7%?|]!"  #does not work
2 3 7 4 8 5'[!7%?|!]" #works
2 3 7 4 8 5'[7%?!&]"  #shorter, notice the | -> &
5'2 3 7 4 8[7%?!&]"   #can save a space in some cases


# Using the hash as an array

Element does not have native support for arrays, but the hash can be used as a makeshift array for simple operations. This is done by using numbers as keys. The following code initializes an array [7,32,12,8,3]

7 0;32 1;12 2;8 3;3 4;


For a much larger array, this is a more compact way to go:

7 32 12 8 3 13 5 1 0 67 12 54 11 13'["1-+2:';]


As you can tell, using the hash to hard-code an array is a measure of last resort. They work nicer when the array is created in a less hard-coded fashion. Here is code that can take as input an array size and a list of values and put it in an array.

_'0[2:_)1@;1+]


Retrieving a value is easy though and can be where this convoluted scheme pays off. If i contains the index you want to retrieve, then the code is simple:

i~~


To give an array a name, you can append a single letter to the index when storing and retrieving.

# "Factoring Out" things from If-Else constructions

If you ever have the code

?[...]![..]


Then you probably have an If-Else construction. You really want to minimize the amount of duplicate code you put in these things. Generally speaking, if V W X Y Z are blocks of code, then the following compression should be tried:

?[WXVZ]![WVYZ]
?W[X]V![Y]Z


This usually works even if V/W/Z is a ~; type of operator, but probably does not work if V/W/Z is "`.