12
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

Higher-level function shorthand

This seems like a pretty interesting golfing point. E.g.

list(1,2,3)map(i,i+1)print

However, Io is pretty permissive on not specifying the counter; the map body can be used as a point-free function, as Io tries to fill in the operand of this expression. This can be golfed into

list(1,2,3)map(+1)print

Due to the helpful work of the language creator, you can even do this to reduces!

list(1,2,3)reduce(a,b,a*b)

Can easily be reduced into:

list(1,2,3)reduce(*)

(Unfortunately, I still haven't managed to understand how exactly this works, so I never managed to make the expression more complex.)

| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

You can stick methods onto the back of most literals

"text" print    # 12 bytes
"text"print     # 11 bytes

12 print        # 8 bytes
12print         # 7 bytes

(0<1,0,1) print # 15 bytes
(0<1,0,1)print  # 14 bytes

Not everything

0x12print # prints nothing
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

You can leave out the else part of the if function

This isn't in the documentation... I initially thought that you have to include the else part, like the elvis operator in other languages; turns out that I can leave out the else part. (Please add this to the tutorial/documentation!)

if("bug"size>2,"True",nil)

So, if you don't want the else part to return anything, you could just do

if("bug"size>2,"True")
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Use ~, :, and \ for variable names

You can stick methods onto the back of ~. E.g. instead of this:

method(i,i pop)

You can do this:

method(~,~pop)

You can even put this between them. Instead of this:

and a sqrt

You can do this

and~sqrt

: also does this trick (I discovered it from the := bug), as well as \. (This can empirically save a lot of bytes in your source code, if you use variables a lot)

Some other valid symbolic names that you can't stick

  • _
  • .

Can you stick these to other symbols as well?

Unfortunately, things get a little complicated here. Here's a table of how powerful these variable names are:

Unfortunately, you can't stick any of them before := or on the rhs of /, so pick whatever name you like. :P

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Now I'm seriously wondering, what have I been doing in Io all these years... \$\endgroup\$ – user96495 Aug 24 at 12:37
1
\$\begingroup\$

Literal newlines

Just like JavaScript's ``, you can insert literal newlines. i.e. you can do:

"
"
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Parenthesis dropping in method applications

Say, I want to split a string by whitespace. I am pretty used to this method when I first got used to Io:

a_string split()

After I heard about how Io parses the arithmetic operations, I realized that this is perfectly valid:

a_string split

Sometimes, you can do that to some one-operand functions as well. If you want to join by spaces, you can do:

a string join" "

I guess this doesn't work sometimes, though I can't find any examples here.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Prefer push over append

There are 2 methods that append values towards the caller object: add, push, and append. I didn't count add because add doesn't work. Try it online!

push and append basically do the same thing:

list(1,2,3)push(4) println
list(1,2,3)append(4) println

Try it online!

Therefore, whenever you want to write append, remember that you can substitute it for push to do the exact same thing.

| improve this answer | |
\$\endgroup\$

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.