15
\$\begingroup\$

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.

noodle person
12.6k1 gold badge31 silver badges90 bronze badges
asked Jan 18, 2024 at 19:13
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is "use an SBCS" a reasonable tip? \$\endgroup\$ Commented Jan 18, 2024 at 19:44
  • \$\begingroup\$ @Tbw Yes, because an SBCS gives you less bytes \$\endgroup\$ Commented Jan 18, 2024 at 19:45

9 Answers 9

12
\$\begingroup\$

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.

answered Jan 18, 2024 at 19:41
\$\endgroup\$
9
\$\begingroup\$

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
)
answered Feb 2, 2024 at 15:13
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also /◇f instead of しろいしかく2 \$\endgroup\$ Commented Feb 15 at 15:34
7
\$\begingroup\$

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.

answered Jul 3, 2024 at 13:09
\$\endgroup\$
3
  • \$\begingroup\$ I don't know how useful this is, but °▽ splits a list into its values and counts, basically RLE-encoding it \$\endgroup\$ Commented Jul 4, 2024 at 19:04
  • \$\begingroup\$ °/× only works up to 4,294,967,295. \$\endgroup\$ Commented Aug 9, 2024 at 15:21
  • \$\begingroup\$ °⤸ is equivalent to ⇡⧻しろさんかく., or, in simpler words, gives you the range of the rank. \$\endgroup\$ Commented Sep 14, 2024 at 17:26
7
\$\begingroup\$

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.

Europe2048
6403 silver badges8 bronze badges
answered Jan 18, 2024 at 19:55
\$\endgroup\$
6
  • 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\$ Commented 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\$ Commented 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\$ Commented Dec 4, 2024 at 21:46
  • 1
    \$\begingroup\$ Does the interpreter understand the SBCS without extra flags? \$\endgroup\$ Commented 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\$ Commented Jun 15 at 19:12
5
\$\begingroup\$

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.

answered Sep 14, 2024 at 17:37
\$\endgroup\$
3
  • \$\begingroup\$ I can't help but wonder if there's a hint of Brachylog influence here! \$\endgroup\$ Commented Sep 14, 2024 at 21:24
  • 2
    \$\begingroup\$ @UnrelatedString There actually isn't. We were talking about both recently, and then we got the idea to make it have a custom number of arguments, so we got to this. \$\endgroup\$ Commented Sep 15, 2024 at 11:43
  • 2
    \$\begingroup\$ Subscripts are now stable! as of 0.14.0-dev.2 \$\endgroup\$ Commented Nov 25, 2024 at 1:00
3
\$\begingroup\$

If you have a sorted array, you can use either rise or fall instead of taking a length-range or that reversed.

answered Mar 6 at 16:07
\$\endgroup\$
1
\$\begingroup\$

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.

answered Feb 15 at 15:37
\$\endgroup\$
1
\$\begingroup\$

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.

answered Feb 21 at 14:05
\$\endgroup\$
1
\$\begingroup\$

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 +.

answered Mar 6 at 16:04
\$\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.