43
\$\begingroup\$

CJam is a GolfScript-inspired stack-based golfing language, created by PPCG user aditsu.

So, in the vein of other language-specific tips questions:

What general tips do you have for golfing in CJam? Please post one tip per answer.

\$\endgroup\$
  • 4
    \$\begingroup\$ See also Tips for golfing in GolfScript; the languages are similar enough that many of the tricks can be adapted either way. \$\endgroup\$ – Ilmari Karonen Oct 12 '14 at 12:38
  • 2
    \$\begingroup\$ @IlmariKaronen After going through the answers in that question, I'd say only around half of them apply to CJam, due to syntactical or logical differences in the languages. \$\endgroup\$ – Optimizer Oct 12 '14 at 13:16

34 Answers 34

1
2
2
\$\begingroup\$

Modify an element instead of deleting it

If you need the sum of a list except for the last element, increased by T, instead of:

W<:+T+

You can use:

WTt:+

Or if you just need the sum of a list except for element K, which isn't the first or last element, you can use:

K0t:+

Except for the case there are only 3 elements and K=1, where this is shorter:

2%:+
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Understand How Array Literals Work

The [ and ] characters in CJam are not part of any special syntax; they are operators.

What [ does is puts a "flag" on the stack. Attempting to pop from the stack will pop from below it, but pushing to the stack will push above it.

This means that this:

1 2 [+]

Will result in the stack looking like this:

[3]

This can be used for golfing. For example, if I wanted to duplicate the final element in an array (i.e. turn ["foo" "bar" "baz"] into ["foo" "bar" "baz" "baz"]), I could do this:

_W=a+

But I can shorten this by 1 byte:

[~_]

If I wanted to put the top two stack elements into an array, I'd do:

a\a\+

But again, I can shorten this:

[\\]

If I was doing some computation earlier that pushed the two values onto the stack, I could wrap part of it in []. This means I don't need the \\, which saves even more bytes.

You can manipulate this in even crazier ways. For example, you can accumulate an array ([1 2 3] -> [1 3 6]) using:

[{1$+}*]

(I still wish CJam had a builtin for that, though, as I find myself needing it oftentimes.)

In general, this technique allows us to treat stack elements as array elements and vice versa.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Check if array contains sub-array

Note: If you're only looking for a single element use jimmy's method.

If you want to see if an array contains a specific sub-array, use #). # is "index of" and ) increases it by 1. If the sub-array is not in the array # returns -1. When you increment it by 1, you get 0 which is falsey. If the sub-array is in the array # returns a number that is at least 0, which when incremented by 1 returns a truthy value.

& and e= don't work for sub-arrays.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Index into an array, use a default value or run code if not in range

This could be done using a single character: j.

For example, this code would output GolfScript, CJam, Pyth for 0, 1, 2, and Seriously for all other cases:

ri"GolfScript CJam Pyth"S/{;"Seriously"}j

Compared to:

ri3,#"GolfScript CJam Pyth Seriously"S/=

Actually used more characters. But it might be considerable if the type is not string or you have to run code in a block anyway.

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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