7
\$\begingroup\$

J-uby is the Language of the Month for June 2022, so I'm looking for tips for golfing in it. As usual, please post one tip per answer, and avoid general tips such as "remove whitespace" or "avoid comments".

\$\endgroup\$
1
  • \$\begingroup\$ Anybody have any tips on porting this answer to J-uby? It looks like it ought to be simple but has me stumped. \$\endgroup\$
    – Jordan
    Commented Oct 9, 2022 at 16:48

4 Answers 4

2
\$\begingroup\$

reduce to /

Let's say we have an array of:

a=[1,2]

If we want to find the sum:

a.reduce:+

Golfed:

:+/a

Just swap the a and :+ before placing a / smack in the middle!

\$\endgroup\$
2
\$\begingroup\$

join to *

Say we have an array:

a=[1,2]

To join it with commas:

a.join','

However this seems too long, let's replace it with *:

a*','

Or even better:

a*?,
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I don't know if maybe it's not idiomatic in J-uby, but you can omit the . for a*',' (or a*?,). This works in vanilla Ruby too. \$\endgroup\$
    – Jordan
    Commented Oct 3, 2022 at 15:23
2
\$\begingroup\$

Conversions

number.abs    #To abs value
number.|

Object.to_i    #To integer
Z[Object]

Object.to_f    #To float
Q[Object]

Object.to_s    #To string
S[Object]

Object.to_a    #To array
A[Object]

Hash[Object]    #To hash
H[Object]

All these make sure of single letters for conversions.

\$\endgroup\$
0
\$\begingroup\$

Flip your comparisons

x < 1 in tacit J-uby is ~:<&1 but the equivalent 1 > x is a byte shorter: :>&1.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.