What general tips do you have for golfing in Uiua?
I'm looking for ideas that can be applied to code golf problems in general that are specific to Uiua. (e.g. "remove comments" would not be an answer.)
Please post one tip per answer.
-
1\$\begingroup\$ Is "use an SBCS" a reasonable tip? \$\endgroup\$Tbw– Tbw2024年01月18日 19:44:26 +00:00Commented Jan 18, 2024 at 19:44
-
\$\begingroup\$ @Tbw Yes, because an SBCS gives you less bytes \$\endgroup\$Joao-3– Joao-32024年01月18日 19:45:33 +00:00Commented Jan 18, 2024 at 19:45
9 Answers 9
Use complex numbers
The length of a vector given by a pair of numbers can be found with sqrt + both(pow 2) for 7 glyphs (or more idiomatically under both un sqrt + for 5).
But using complex numbers, this problem can be solved in just 2 glyphs: abs complex.
Other problems can benefit from clever uses of complex numbers, e.g. to represent transformations.
Use /f instead of f°⊟
If you have an array with two rows, and want to apply a |2.1 function to its rows, use / reduce instead of °⊟ un couple.
An example (Pad):
°△しろさんかく2_3
⊃(+°⊟ # 3_5_7
| /+ # also 3_5_7
)
-
1\$\begingroup\$ Also
/◇finstead off°□しろいしかく2\$\endgroup\$noodle person– noodle person2025年02月15日 15:34:01 +00:00Commented Feb 15 at 15:34
Handy inverses
There are some inverses which may not be entirely obvious but can be very handy for golfing.
There is a full list of inverses at this link, here are some of the most useful for golf:
°⊏(un select) is equivalent to⇡⧻.(range length duplicate) which is a very common golf pattern.°⊡(un pick), similarly, is equivalent to⇡△しろさんかく., if you ever need multidimensional range.
°⊚(un where) on a rank-2 array turns a list of nonnegative-integer coordinate pairs into a boolean matrix with ones at those coordinates.°△しろさんかく(un shape) gives the range of positive integers that fill the given shape. Roughly equivalent to×ばつ..×ばつ(un reduce multiply) gives the prime factors of a positive integer.
You should go over all the inverses mentioned at that page because many of them are helpful in various cases.
-
\$\begingroup\$ I don't know how useful this is, but
°▽splits a list into its values and counts, basically RLE-encoding it \$\endgroup\$Joao-3– Joao-32024年07月04日 19:04:36 +00:00Commented Jul 4, 2024 at 19:04 -
\$\begingroup\$
°/×only works up to 4,294,967,295. \$\endgroup\$Europe2048– Europe20482024年08月09日 15:21:03 +00:00Commented Aug 9, 2024 at 15:21 -
\$\begingroup\$
°⤸is equivalent to⇡⧻△しろさんかく., or, in simpler words, gives you the range of the rank. \$\endgroup\$Joao-3– Joao-32024年09月14日 17:26:13 +00:00Commented Sep 14, 2024 at 17:26
Use an SBCS (single-byte character set)
Uiua uses Unicode glyphs that would usually take up multiple bytes each. But with an SBCS, each character is represented with one byte.
Because Uiua is currently in flux, such an encoding will also have to change to fit the changes in the language.
Here is a link to the SBCS for Uiua version 0.17.0-dev.1, updated June 10, 2025: https://tinyurl.com/Uiua-SBCS-Jun-10.
-
4\$\begingroup\$ Tbw, I updated the SBCS for Uiua 0.12.0-rc. 1. If you don't want to continue maintaining this, I can take over, but if you want to keep doing it that's fine too. \$\endgroup\$noodle person– noodle person2024年08月09日 14:54:10 +00:00Commented Aug 9, 2024 at 14:54
-
\$\begingroup\$ Thanks. I've been away at work and haven't had much time to stay updated with CGSE and Uiua. I probably won't be very active for the next month at least, so you can take over if you'd like. Just make sure you update the date on the code :) \$\endgroup\$Tbw– Tbw2024年08月10日 00:41:54 +00:00Commented Aug 10, 2024 at 0:41
-
4\$\begingroup\$ I have added a feature where if you hover a function's name, it tells you how many characters in its definition. It also calculates an SBCS byte coune, and will show it if it is for some reason different than the character count. For example, try pasting and hovering
F ← +"┌". \$\endgroup\$Kaikalii– Kaikalii2024年12月04日 21:46:14 +00:00Commented Dec 4, 2024 at 21:46 -
1\$\begingroup\$ Does the interpreter understand the SBCS without extra flags? \$\endgroup\$Maki– Maki2025年06月12日 11:16:57 +00:00Commented Jun 12 at 11:16
-
\$\begingroup\$ No it does not. See codegolf.meta.stackexchange.com/questions/14329/… for why this is still ok. At least in my answers, I always include a link to the commented transliteration tool. \$\endgroup\$Tbw– Tbw2025年06月15日 19:12:02 +00:00Commented Jun 15 at 19:12
Subscripts
Subscripts (or, officially, subscript modifiers) are a feature added in 0.13.0 that can modify functions, by doing things such as giving them constant arguments or making them repeat.
You can type them with ,<value>.
What makes these useful is that these, along with the function they're modifying, count as a single unit, meaning you can do stuff like this:
×ばつ10 1_2_3_4_5 # outputs 10_20_3_4_5
For more information, check out this page.
There's now (as of 0.14) sided subscripts, which either ¤ fix one of their function's arguments (pervasive dyadics, ≡ rows, ⍚ inventory, ∵ each), add more stack manipulation capabilities to a modifier (∩ both, ⊓ bracket, ∪ reach), or change which side a function works on (⬚ fill, ⊂ join).
You can type them with ,< or ,>.
These can help save a byte or two:
≡⌟⊂ 1_2_3 4_5_6 # saves a byte over ≡⊂⊙¤
∩⌞⊟ @a@b@c # saves a byte over ∩⊟⟜:
As of version 0.16.0, you can mix subscripts!
Now, you can use a numerical subscript on the left or (inclusive) right of a sided subscript. Currently, this is supported for ∩ both, ≡ rows, ⍚ inventory and ∪ reach, and I don't know how often they will be used in golf.
-
\$\begingroup\$ I can't help but wonder if there's a hint of Brachylog influence here! \$\endgroup\$Unrelated String– Unrelated String2024年09月14日 21:24:38 +00:00Commented Sep 14, 2024 at 21:24
-
2\$\begingroup\$ @UnrelatedString There actually isn't. We were talking about
bothrecently, and then we got the idea to make it have a custom number of arguments, so we got to this. \$\endgroup\$Joao-3– Joao-32024年09月15日 11:43:40 +00:00Commented Sep 15, 2024 at 11:43 -
2\$\begingroup\$ Subscripts are now stable! as of 0.14.0-dev.2 \$\endgroup\$noodle person– noodle person2024年11月25日 01:00:30 +00:00Commented Nov 25, 2024 at 1:00
If you have a sorted array, you can use either rise or fall instead of taking a length-range or that reversed.
Use ⦷1 to assign increasing numbers to ones in an array
[1 1 0 0 1 0 1 0 0 0 1 0 1 1 1 1]
⦷1
[1 2 0 0 3 0 4 0 0 0 5 0 6 7 8 9]
The longer way is ×ばつ⟜\+, which is also a bit more general if your array is not just booleans.
Use ° ̃ to get the outputs of an inverse in reverse order
° ̃°F doesn't save any bytes over (:F), but if you're already calling an inverse, it saves two - (:°F) becomes ° ̃F.
The most common example is with °⊏ to get the length-range behind the array.
(:°⊏) becomes ° ̃⊏.
Sometimes just :°⊏ without parentheses is okay, but this is shorter if you need to use ⊙ or any other stack modifiers.
This works for other inverses, too - °∠, °C, °⊟, to name a few.
Note that ° ̃⊂ does not give the first row behind, but instead extracts the last row of the array, which is also useful.
Construct a table from its dimensions
Say I want the 3x4 addition table and I have [3 4] on the stack. I might normally write:
⊞+∩⇡°⊟
but there's actually a nice and short way to write the same thing for a byte shorter, using multidimensional range:
/+°⍉⇡
If you have a matrix of the given shape, you can also use °⊡ for even more savings - /+°⍉°⊡.
Also, you can use any dyadic pervasive function in place of +.