g | x | w | all
Bytes Lang Time Link
nan250109T210227ZConor O&
nan180725T194734ZConor O&
nan210701T024004ZBubbler
nan200528T073719ZBubbler
nan181008T085417ZBubbler
nan200409T064604ZBubbler
nan& is your friend191115T011859ZBubbler
nan171230T230127Zcole
004Getting numbers from 0 to171118T170952ZFrownyFr
nan160826T043241Zmiles
nan160315T165503ZConor O&
nanConsider using explicit definition instead of writing a tacit verb sure the 3 ' and ' cost 5 bytes150305T180944ZOmar
nanThe most important thing when golfing in J is to not only understand the problem150224T145727ZFUZxxl
nanThere are a number of subtleties to squeezing out the last few characters in J. For the following140318T183742Zalgorith
nan120711T235525ZGareth
nan120714T172429ZGareth

Builtin Constants

I find myself constantly looking for the same specific predefined verbs and nouns useful for golfing. Although much of this information can be found on the Standard Library page (as referenced in @miles's answer), and I imagine many seasoned J golfers know many, if not all of these builtins, this quick reference should be a bit more accessible than dredging through all of the functions. I will also include an example of using each, where applicable.

If you're curious how I obtained these from a more "reverse engineering perspective", especially the j locale, see the "General Method" section after the quick reference.

z locale (always accessible):

j locale (requires _j_ suffix to specify the locale):

Dubiously useful constants

z locale:

j locale:

General Method

The predefined names method gives all variables defined in the current locale. For example:

   a =: b =: 3
   names ''
a b
   bob =: jill =: +
   names ''
a    b    bob  jill

The default locale is base, and the standard library is contained in the z locale, and is always visible, no matter the locale. There is a little known locale, the j locale, which contains many nouns and verbs which seem to be relevant to the J environment itself, but are also useful for golf.

To see all the variables defined in a specific locale, you can either use cocurrent 'locale' before using names (e.g. names cocurrent 'j'), or you can specify the locale explicitly, e.g., names_j_ ''. There are lots of niche functions and constants in both names_z_ '' and names_j_ '', and I would recommend perusing them at your own leisure, especially for restricted source challenges. Something that surprised me was the inclusion of various IF constants in the z locale corresponding to the executing platform, e.g. 1 -: IFWIN iff the user is on a Windows system, and similarly for IFUNIX.

Shorter ways to mess with ranks

Sometimes, you'll have code like <"0 i.3 3, where you want to apply a verb v at rank r. However, if you use a noun (like 0), you'll often have to include a space. To avoid this, you can use another verb u of equivalent rank and use u"v instead. For example, since + has rank 0 0 0, we can use <"+ instead of <"0.

Rank mini-lesson: This 3-element rank notation is monadic dyad-left dyad-right. For example, = has rank _ 0 0, so (v"=) x is equivalent to (v"_) x (applying v to all of x), but x (v"_) y is equivalent to x (v"0 0) y, applying v pairwise to all cells in x and y.

Here is a table of all verbs and their ranks (obtainable by using v b. 0):

New J (e.g. J9.4)

Edit 2024: Some of the ranks changed and new verbs have been added. In particular, ~. {: }: L. were changed from _ 0 0 to _ _ _. No new shortest representations were added/removed, though.

0 0 0     > + * - % ^ | ! ? <. <: >. >: +. +: *. *: %: ^. j. o. q: r.
0 _ _     -. -: E. i: p:
1 0 1     p..
1 0 _     { A.
1 1 0     p.
1 1 1     #.
1 1 _     C.
1 _ _     ;: ". i. I.
2 _ 2     %.
_ 0 0     = < ~: ?.
_ 1 0     #:
_ 1 _     $ # |. |: {. }. ": {::
_ _ _     , ; [ ] _: $. $: ,. ,: /: \: [: e. s: u: x: 0:
          ~. {: }: L. T. Z: u. v. {{}}

Old J (e.g. TIO J)

0 0 0     > + * - % ^ | ! ? <. <: >. >: +. +: *. *: %: ^. j. o. q: r.
0 _ _     -. -: E. i: p:
1 0 1     p..
1 0 _     { A.
1 1 0     p.
1 1 1     #.
1 1 _     C.
1 _ _     ;: ". i. I.
2 _ 2     %.
_ 0 0     = < ~. ~: {: }: ?. L.
_ 1 0     #:
_ 1 _     $ # |. |: {. }. ": {::
_ _ _     , ; [ ] _: $. $: ,. ,: /: \: [: e. s: u: x: 0:

To use this table, find the desired rank r on the left hand side, then choose an appropriate verb v from the right hand side. E.g., if I need to vectorize a verb v at depth 2 _ 2, then I find that rank on the left and choose %. from the right. Then I use v"%. instead of v"2 _ 2.

Know (and abuse) J's truthy/falsy ()

When truthy/falsy is not defined by the language spec, it is defined as the one that is accepted by an if statement. Truthy ones are those that make the if clause run, and falsy ones are the opposite.

Unlike APL (where only 1/0 are truthy/falsy respectively), J is quite liberal. J's if. clause accepts any array, and evaluates to falsy if and only if the array is nonempty and its first atom is a numeric zero. Any other arrays (including empty arrays, character or boxed arrays) are truthy.

Demonstration:

f =: 3 : 0
if. 0         do. echo 'yes 0' end.
if. 1         do. echo 'yes 1' end.
if. 2         do. echo 'yes 2' end.
if. 0 1 2 3 4 do. echo 'yes 3' end.
if. 2 0 0 1 3 do. echo 'yes 4' end.
if. 0$0       do. echo 'yes 5' end.
if. 'abcd'    do. echo 'yes 6' end.
if. '';'def'  do. echo 'yes 7' end.
)
f ''

Output:

yes 1
yes 2
yes 4
yes 5
yes 6
yes 7

yes 0 and yes 3 were not printed because the array is nonempty and the first element is zero. Having zeros at any other positions does not affect the truthy/falsy interpretation. Try it online!

Miscellaneous ideas

Cross-post of my own APL tip, because APL and J are so closely related that the two can share many ideas.

If you're facing [:-~/, use _1#. instead

A common reason to use -~/ is to reverse-subtract over a length-2 axis, e.g. given a 2-element array x y, compute y-x.

If this is the case, _1#. is an equivalent expression; just like 2#.x y computes 2x+y, _1#.x y computes -x+y. While -~/ most likely requires the use of [: (capped fork) or @ (atop), _1#. is likely to fit just well in trains as-is.

Note that, if you apply it to longer axis, _1#. gives you more similar result to -/ (alternating sum), with negated result when the length is even.

Construct a boolean square matrix where the border is ones/zeros and the interior is the opposite

This is mainly for large fixed-size matrices, say 6 by 6.

The matrix

1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

can be generated by +./~ -ing the boolean vector 1 0 0 0 0 1. The shortest known way to generate that vector is #:33, so we can get the matrix above in just 8 bytes:

+./~#:33

If we're to put this in a train, a constant function +./~@#:@33 and parenthesized noun left arg (+./~#:33) have equal length of 10 bytes. The two forms have their own pros and cons: the former can be placed at the right end of a train, while the latter may serve better by not messing up with something that comes on its right (e.g. 33e. is a syntax error).

We can also get negation of it at no extra cost, by swapping +. with +:! (Remember that J, like APL, has NOR +: and NAND *:; they are rarely needed, but definitely are a byte saver when we do need them.)

Tacit programming

Basics

Dyadic verb

x (F G H) y == (x F y) G (x H y)
x (F G) y == x F (G y)
x ([: G H) y == G (x H y)  NB. G is called monadically

NB. Verbs are grouped from the right by units of 3.
NB. For the following, think like G, I, K are replaced by the results of (x G y) etc.
NB. and then the sentence is run as usual.
x (F G H I J K) y == x (F (G H (I J K))) y
                  == x F ((x G y) H ((x I y) J (x K y)))

NB. Using conjunctions for dyadic verb
x F@G y == F (x G y)  NB. Atop; Same as x ([: F G) y; Consider as golfing alternatives
x F&G y == (G x) F (G y)  NB. Compose; G is applied monadically to both arguments

Monadic verb

(F G H) y == (F y) G (H y)
(G H) y == y G (H y)  NB. Note that this is different from APL
([: G H) y == G (H y)
(F G H I J K) y == (F (G H (I J K))) y
                == y F ((G y) H ((I y) J (K y)))
F@G y == F (G y)

Misc

x&F y == x F y
F&y x == x F y
y F~ x == x F y
F~ y == y F y

Tricks

(F x) G (H y)

Tacit solution: (G~F)~H; depending on the actual verbs, consider rearranging the left and right arguments to remove ~.

x ((G~F)~H) y
x (G~F)~ (H y)
(H y) (G~F) x
(H y) G~ (F x)
(F x) G (H y)

Monadic-Dyadic replacements

>:y == 1+y
<:y == 1-~y or _1+y
+:y == 2*y
-.y == 1-y
-:y == 2%~y
*:y == 2^~y
#.y == 2#.y
#.inv y == 2#.inv y  NB. #: doesn't work this way
{.y == 0{y
{:y == _1{y
}.y == 1}.y
+/y == 1#.y  NB. Vectors only
+/"1 y == 1#.y  NB. Higher-dimension arrays

x$y does something different than you think

Dyadic $ is called reshape. If you're coming from APL, you'd know under the same name which lists down all elements of y before creating the x-shaped array.

J handles it differently: it does not flatten y; instead it uses y's items, or major cells, to fill in each cell of the x-shaped array. So the resulting shape becomes x,}.$y. It doesn't look very useful by this description.

There is one use case where this becomes useful in code golf: when x is a positive singleton. Then x$y acts like a take or repeated overtake.

Note that, in both cases, the result is equivalent to using the individual elements to fill in x,}.$y-shaped array.

& is your friend, use it wisely

v is a verb, n is a noun, x and y are left and right arguments, respectively.

Monad &: Introduce ~ inside adverb/conjunction chain

An adverb/conjunction chain evaluates from the left. So something like _2&+/\&.> won't work because it parses like (_2&+)/\&.> while we want _2&(+/\)&.>. In this case, swapping the left/right of +/\ can save a byte, as in +/\~&_2&.> because this one parses as ((+/\)~)&_2&.>. To see why this works:

+/\~&_2 y
is equivalent to
y +/\~ _2
is equivalent to
_2 +/\ y
is equivalent to
_2&(+/\) y

Dyad &: Repeat x times

Did you know that if you give a left argument x to &, the function applies it x times to y? Quite a few challenges ask you to do certain operation x times. It is mainly achievable in two ways:

If the operation is v, then v^: becomes an adverb train that, when given a left operand, becomes a monadic verb. So v is applied to y, x times.

x(v^:)y
is equivalent to
(v^:x)y

To use this, you need to identify a constant n and a dyadic verb u, so that either n u y or y u n is equivalent to v. Then you can write n&u or u&n to solve the entire task. This form is most effective when the choice of the constant is obvious, e.g. 3 in 3 u: (convert chars to ASCII values).

Also, u&n is slightly preferred over n&u when the outermost structure of u is a conjunction or adverb (in which case n&u should be n&(u); you can do u~&n instead).

Note that you can place the dyadic & anywhere in a train to achieve repeating arbitrary function to arbitrary argument, in the similar sense to dynamic ^:.

Some (fairly) common tricks I've seen

I'm sharing a few things that have come in handy for me. Basically all of these are tips I've received myself, but I don't have credits for most.

Sum of a rank one array

Instead of using +/@:(FGH) use (1#.FGH). This means debase to base 1, which effectively means summing an array. Although it's longer than +/, it doesn't require a cap or composition, which often makes it much shorter than using +/.

Counting trailing truths

If you have a boolean list and you want to count the number of trailing truths, use #.~. See here. The APL answer provides a good explanation for how this works. Granted, this has only been helpful to me twice but I figured I'd share it anyways.

Under (&.)

Not a specific trick, but just a general suggestion: the adverb &.-under often leads to elegant and (more importantly) short solutions. Keep it in mind when you're golfing.

Often times it's useful for and other base conversion challenges, e.g. this code which removes the most significant bit from a number: }.&.#: (convert to list of binary digits, remove the first digit, then undo the conversion to a list of binary digits and convert back to decimal). The straightforward solution is two more bytes: #.@}.@#:.

Under is also helpful for challenges where you need to work with decimal digits, since you can use u&.":. For example, the short way miles gives to split to decimal digits uses under: ,.&.":.

A final example is finding the magnitude of a vector: +/&.:*:, note that you need to collect all of the results from *:-square with &.:-under since *:-square is rank zero.

Getting numbers from 0 to 4

If there’s a restriction on using numbers in your code:

0 %_: one divided by infinity.
1 #_: how many infinities?
2 #_ _: two infinities.
3 verb: there’s a built-in.
4 dyad: another built-in.

Getting numbers from 10 to 35

Base-inifinity literals: 11:_bb, 26:_bq etc.

Using iteration to compute sequences

Typically, solving an OEIS sequence challenge will require using one of the formulas given on its page. Some of these adapt well for J, and others not so much. Recursive formulas are straight-forward, however, iteration might not be simple. A pattern I've begun to use is

(s(]f)^:[~]) n
          ]  Gets n
 s           The first value in the sequence
         ~   Commute the argument order, n is LHS and s is RHS
        [    Gets n
      ^:     Nest n times with an initial argument s
  (]f)         Compute f s
             Returns (f^n) s

where s is the first value in the sequence, f is a verb that will compute the next term given the previous term, and n is the zero-based index of the term you want to compute. This method relies on the fact that when computing the power of a dyad, the LHS is bound to the dyad to form a new monad, and that monad is nested on the initial value. The dyad given to the power adverb is a hook where (]f) is given the index n on the LHS and the value of a term in the sequence s. The hook will apply f on s as a monad, and then ignore n to return the result of f s.

Standard library

Sometimes, you might find that J will have support for a verb in its standard library. For example, most of the bitwise integer operations are bound to names which are shorter than using the primitive call.

AND =: (17 b.) NB. it is actually '$:/ :(17 b.)'

Date and time builtins are also available.

Ranges

If you have a set of values [a, b, c] and you want to form a range based on their product like [0, 1, 2, ..., a*b*c-1], the typical approach would be to find their product and then form a range which might be [:i.*/ which costs 6 bytes. A shorter way is ,@i. for 4 bytes since i. can form multidimensional arrays while still counting up, and flattening it will produce an equivalent range.

Printing continuously

A tacit way to print a value and continue to use it without an explicit loop is ([echo) for a monadic case. echo is a verb in the standard library that prints its contents to stdout in the same format used in the interpreter. The hook then passes the same input value out using the left [ verb.

Base 10 digits of an integer

The standard way of acquiring the base 10 digits of an integer is 10#.inv] which costs 8 bytes, too much! An alternative is to convert it to a string and parse it at rank 0 "."0@": which saves a byte, but an even better way is ,.&.": which saves another byte making the final cost 6 bytes instead of 8.

strings library: golfing tips

The strings library is vastly helpful for doing anything with string manipulation. Sure, it takes include'strings' (which is very costly, considering J), but you may at times reap the benefits.

stringreplace

Find yourself using string replace? Observe that A stringreplace B is the same as B rplc A.

In fact, this is how rplc is implemented:

   rplc
 stringreplace~

cuts

The verb cuts provides thus:

cut y at x (conjunction)
string (verb cuts n) text
  n=_1  up to but not including string
  n= 1  up to and including string
  n=_2  after but not including string
  n= 2  after and including string

So it's really slicing a string.

Consider using explicit definition instead of writing a tacit verb; sure the 3 :' and ' cost 5 bytes, but you can save a lot of @, @: and [: that way.

The most important thing when golfing in J is to not only understand the problem, but to reduce the problem to a series of array transformations. You need to understand this way of thinking to have any success with J code.

For example, a recent challenge asked to solve the largest subarray problem. The stock algorithm to solve this problem is Kadane's algorithm has the following informal description:

Go through the array and at each position and find the sum of the largest subarray ending here, which is the maximum of 0 or the value at the current index plus the sum of the largest subarray ending at the previous position. Compute the maximum of these subarrays as you go to find the largest subarray in the entire array.

A translation into imperative code is straightforward:

  1. let A be the input array.
  2. hmi ← 0.
  3. if i ≥ len(A) return m.
  4. h ← max(0, h + A[i]).
  5. m ← max(m, h).
  6. ii + 1.
  7. goto 3.

This algorithms seems complicated for J at a glance as there is an explicit loop that doesn't look like a reduction at first. If you realize what the algorithm is doing you can untangle the individual steps and see that it actually performs two simple array operations:

  1. Scan through the array to compute the lengths of the largest subarrays ending at each index.
  2. Reduce these lengths with the max function to find the maximum.

Now these two steps are very easy to implement in J. Here is a translation:

  1. (0 >. +)/\. y , 0 – This step operates from the other end of the array to better fit J's paradigm. 0 >. + is tacit for 0 >. x + y.
  2. >./ y

Put together, we get a very terse implementation of the algorithm:

>./ (0 >. +)/\. y , 0

If you learn this way of approaching the implementation of algorithms, your solutions will be as terse as this code.

Here are some tricks I accumulated over time. This list will be expanded as I get more knowledge in J golfing.

There are a number of subtleties to squeezing out the last few characters in J. For the following, assume that each capital letters is a primitive verb (i.e. I am removing the spaces that would otherwise be required to delimit names).

Input

1!:1[1 will take one line of input terminated by pressing the enter key.

1!:1[3 will take a number of lines of input (terminated by Ctrl-D on my Mac, Ctrl-C on Windows).

If you're trying to input numbers, using ". will evaluate the string and return a list of numbers ready to be manipulated. If you're taking in one number but need to operate on the digits individually, ".,. (thanks to Jan Dvorak's comment for this) or "."0 will split the string into separate digits:

   "."0[1!:1[1
12345
1 2 3 4 5

   ".,.1!:1[1
12345
1 2 3 4 5

If you're reading in strings, the shortest way to get a boxed list of separate strings is to use ;:. This works best for space separated strings:

   ;:1!:1[1
hello world
┌─────┬─────┐
│hello│world│
└─────┴─────┘

Be wary of using loops.

While J has looping structures (for. do. end., while. do. end. and variations), if you find yourself using them there's a possibility that your algorithm is not playing to J's golfing strengths and that there are character savings to be made.

^: the power conjunction is your friend. To execute a verb x times:

verb^:x

If you need the result of each iteration in a list:

verb^:(i.x)

You can also use ^: to execute a verb conditionally:

  +:^:(3<])"0[ 1 2 3 4 5 6
1 2 3 8 10 12

Double +: if ^: the item is greater than 3 3<] (the "0 changes the rank of the verb so it works an item at a time).