5
\$\begingroup\$

Arturo is a bytecode-compiled/interpreted, garbage-collected general-purpose language filling a role similar to JavaScript, Python and Ruby. It is centered around the concept of blocks, which hold code just as easily as data. It uses fixed-arity prefix syntax and emphasizes the procedural and functional paradigms.

What general tips do you have for golfing in Arturo? I'm looking for ideas that are specific to Arturo (e.g. "remove comments" is not an answer). Please post one tip per answer.

\$\endgroup\$

4 Answers 4

3
\$\begingroup\$

Converting integers to strings and vice versa

Instead of

to :integer s

use Arturo's eval function:

do s

Instead of

to :string n

use string interpolation:

~"|n|"
\$\endgroup\$
1
\$\begingroup\$

Don't use .with:

All iterators in Arturo can take a .with: attribute label that lets you assign the index of the iteration to a variable. For instance:

loop.with:'i[33 95 7]'n[print~"Index: |i|, Value: |n|"]
Index: 0, Value: 33
Index: 1, Value: 95
Index: 2, Value: 7

However, you can always save a byte by declaring your own index variable and incrementing it manually:

i:0loop[33 95 7]'n[print~"Index: |i|, Value: |n|"'i+1]

And in the best-case scenario, you don't need to use variables at all. Abuse Arturo's (hidden?) stack instead:

0loop[33 95 7]'n[print~"Index: |<=|, Value: |n|"1+]

Here, we place 0 on top of the stack, duplicate it with <=, and increment it with 1+.

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

Shortening ranges

Consider the following function that finds the sum of squares from 1 to n.

$->n[∑map 1..n'x->x^2]

You might think this is as short as we can go, since removing the space causes map1 which is a valid, albeit undefined word name. However, we can remove the space by realizing the symbol .. (range) can go in the prefix position!

$->n[∑map..1n'x->x^2]

Since words can't start with numbers, 1n gets parsed as [1 n] instead of [1n]. So ..1n is equivalent to 1..n and range 1 n. But is this really as short as we can go? No, it isn't! In Arturo, you may iterate over numbers themselves with the meaning 1..n. So

$->n[∑map n'x->x^2]

is equivalent to the above. Finally, we can shorten this even more by using fat-arrow notation:

$=>[∑map&'x->x^2]

This causes & to become the input parameter. Since it's a symbol instead of the word n, it can butt up against its surroundings no problem.

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

Consider outputting strings via the clipboard

clip s sets the clipboard to string s. As long as you're outputting a string all at once, this can save a byte over print s and function return (which are tied in length). Outputting via the clipboard is allowed by default.

For example,

clip~"It's |now\year| already, folks, go home." ; clipboard
print~"It's |now\year| already, folks, go home." ; stdout
$=>[~"It's |now\year| already, folks, go home."] ; function return
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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