13
\$\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.

asked Jan 26, 2014 at 8:30
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Could you add a link to Io? \$\endgroup\$ Commented May 21, 2017 at 14:11

7 Answers 7

5
\$\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.)

answered Mar 18, 2020 at 5:21
\$\endgroup\$
4
\$\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")
answered Mar 18, 2020 at 5:18
\$\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
answered Apr 18, 2018 at 13:13
\$\endgroup\$
0
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

answered Aug 24, 2020 at 12:00
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Now I'm seriously wondering, what have I been doing in Io all these years... \$\endgroup\$ Commented Aug 24, 2020 at 12:37
2
\$\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.

answered Aug 18, 2020 at 13:32
\$\endgroup\$
1
\$\begingroup\$

Literal newlines

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

"
"
answered Aug 18, 2020 at 13:08
\$\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.

answered Aug 18, 2020 at 13:16
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.