Tips for golfing in Haxe

12

2

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).

Please post one tip per answer.

Haxe can be run online here!

Yytsi

Posted 2016-09-18T16:46:25.060

Reputation: 3 582

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 – 2016-09-18T19:23:47.387

@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 – 2016-09-18T19:28:54.890

Answers

8

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.

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880

7

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

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880

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 – 2016-09-18T20:17:34.903

@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 – 2016-09-18T20:20:53.103

The one that calculates the cartesian product is neat. I think that would be a good addition to your post :) – Yytsi – 2016-09-18T20:22:17.913

6

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.

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880

1

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.

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880

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 – 2017-01-18T13:53:00.453

@TuukkaX Heh, thanks. I'll use Math.pow(3,n) instead ;-) – ETHproductions – 2017-01-18T14:02:18.037

1

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.

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880

1

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;

ETHproductions

Posted 2016-09-18T16:46:25.060

Reputation: 47 880