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.
Higher-level function shorthand
This seems like a pretty interesting golfing point. E.g.
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
Due to the helpful work of the language creator, you can even do this to reduces!
Can easily be reduced into:
(Unfortunately, I still haven't managed to understand how exactly this works, so I never managed to make the expression more complex.)
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!)
So, if you don't want the else part to return anything, you could just do
\ for variable names
You can stick methods onto the back of
~. E.g. instead of this:
You can do this:
You can even put this between them. Instead of this:
and a sqrt
You can do this
: 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
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:
After I heard about how Io parses the arithmetic operations, I realized that this is perfectly valid:
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.
Prefer push over append
There are 2 methods that append values towards the caller object:
append. I didn't count
add doesn't work. Try it online!
append basically do the same thing:
list(1,2,3)push(4) println list(1,2,3)append(4) println
Therefore, whenever you want to write
append, remember that you can substitute it for
push to do the exact same thing.