# Tips for golfing in Haxe

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

Haxe can be run online here!

• I've used Haxe quite a bit in the past, but since I started golfing I haven't used it much. I'll have to try using it for golfing... – ETHproductions Sep 18 '16 at 19:23
• @ETHproductions Same here. Today, I golfed two times with it, and it was fun. It has some good golfing capabilities with (for example) its list comprehensions and iterators, so I decided to open the question up to see if anyone has tips to share. – Yytsi Sep 18 '16 at 19:28

# String interpolation

Haxe supports string interpolation on single-quote strings. Like template strings in JavaScript ES6, you can include an expression in a string with ${...}: trace('2 + 2 =${2 + 2}');


Unlike ES6, however, you can omit the curly brackets when the expression is a single variable:

var x = 2 + 2;
trace('2 + 2 = \$x');


Both of these examples print 2 + 2 = 4.

# Array comprehensions

Haxe supports array comprehensions:

trace([for (i in 0...5) i]); // Prints 0,1,2,3,4


Unlike many other languages, you can also use while in comprehensions:

var i = 5;
trace([while (i > 0) i--]); // Prints 5,4,3,2,1


This can be very useful when you don't know how long of an array you need.

You can also chain for, while, and if statements:

trace([for (x in ['A','B','C','D'])
for (y in ['x','y'])
if (x + y != "Cx")
x + y
]); // Prints Ax,Ay,Bx,By,Cy,Dx,Dy

• Wow. I'll delete my answer. I literally built the same answer, just a minute late. Also, you may want to note that it actually prints square brackets around the the lists, so readers won't be confused, since some challenges require strict output formats. – Yytsi Sep 18 '16 at 20:17
• @TuukkaX Haha, that happens sometimes :) I'll include one of your examples in here, if you don't mind. Also, try.haxe.org doesn't seem to print square brackets... – ETHproductions Sep 18 '16 at 20:20
• The one that calculates the cartesian product is neat. I think that would be a good addition to your post :) – Yytsi Sep 18 '16 at 20:22

# Range operator

Haxe has a range operator ... which can be used to create ranges of integers. For example, instead of this:

var i = 0;
while (i < 10) trace(i++);


You can do this:

for (i in 0...10) trace(i++);


Specifics for x...y:

• x and y must both be Ints.
• x cannot be larger than y.
• This returns an IntIterator object, which can be used in the same places as any Iterable.

# Running a statement conditionally

Obviously you can run anything conditionally with if:

if(n>5)doSomething(n);


If, however, you have only one statement as above, you can use the ternary conditional operator to save a byte:

n>5?doSomething(n):0;


You can sometimes save another byte by using &&, though this is very rare because && only works if both expressions return booleans:

n>5&&doSomething(n);


One major exception to this is keywords: if Haxe runs into a return, break, or continue anywhere, it will immediately run it and quit whatever expression it was working on. This means that instead of this:

if(n>5)return n;


You can do this to save 2 bytes:

n>5&&return n;


# Use keywords in expressions

Another unusual feature of Haxe is that everything is an expression. For example, this code is perfectly valid:

function(n){while(n>0)n%4==1?return 6:n--;return 3;}


Okay, that's a fairly useless example, but hopefully you get my point. This works with most keywords:

function(n){while(n>0)n%4==1?break:n--;return n;}


This allows you to use if/else inline, like p=if(n>1)7else 4;, though of course p=n>1?7:4; is shorter.

### Keywords you cannot use inline

• var - The compiler will complain about trying to use Void as a value.
• for/while - Same as above, though you can use them in array comprehensions.

# Omit function brackets

Unlike most languages, everything in Haxe is an expression, including {blocks}. Thus, curly brackets anywhere in a Haxe program (with the exception of switch expressions) can be left off if they contain only a single statement. So instead of this:

function f(n){return Math.pow(3,n);}


You can do this:

function f(n)return Math.pow(3,n);


An easy two bytes saved on many functions.

Even if a function must contain multiple statements, you can often save a byte by moving the return outside the block:

function f(a){var b=a*a;return a<0?-b:b;}
function f(a)return{var b=a*a;a<0?-b:b;}


This works because a block evaluates to the last expression inside the block.

• Edit: if you need the cube of a number, it's better to use n*n*n, or equivalently n*n*n*n for the tesseract of a number, etc... However, Math.pow serves as a good example here. – Yytsi Jan 18 '17 at 13:53
• @TuukkaX Heh, thanks. I'll use Math.pow(3,n) instead ;-) – ETHproductions Jan 18 '17 at 14:02