Basics
Printing
- Use
echo
to write to standard output: echo 2^i.9
- 1D integer arrays are space-separated. Use
echo"+
(i.e. echo"0
) to print each element on its own line: echo"+2^i.9
echo,.2^i.9
(creating and printing a 9×1 array) is not so useful because the result is right-aligned.
echo
can optionally take a left argument. x echo y
is equivalent to (echo^:x)y
. This is particularly useful when x
is the result of some condition.
echo
truncates long lines (>256 characters). To avoid this, apply stdout
to a string argument: stdout 257$'x'
- J formats negative numbers with a leading
_
. If you need to print with a leading -
instead, the 8!:n
foreign function can be useful: echo>8!:0]_2^i.9
Hardcoding
0 :0
starts a "heredoc" multi-line string, from the next line until a line containing a single )
. But you can actually leave off the )
at the end of file.
echo 0 :0
wow I am
so good at
code golf!
ARGV
is a list of boxed-string program arguments.
2}.ARGV
chops off the first two (/usr/bin/j
and /tmp/code.ijs
).
f&>2}.ARGV
applies f
to each unboxed argument.
- Example: print each argument concatenated with its reverse (
|.
)
{{echo y,|.y}}&>2}.ARGV
(direct definition, y
is the argument)
echo&(,|.)&>2}.ARGV
(tacit)
Built-ins
- Everything in NuVoc.
- Automatically loaded libraries: stdlib (dfh, hfd, inv, LF), strings (rplc!!), and dll (AND, OR, XOR).
- Maybe more stuff? Look for
=:
in stdlib.ijs
Hidden Features
It is easy to miss or forget certain features of some of the primitives:
x(u&n)y
is the same as (u&n^:x)y
, x(n&u)y
is the same as (n&u^:x)y
u@n
is the same as (u n)"_
. For example, (*i.@4)10
is 0 10 20 30
- If part of the input to
i.
is negative, the output will be reversed along the corresponding axes. For example, i.2 _3 4
is the same as |."2 i.2 _3 4
- If a train ends with a conjunction, i.e
(f g h^:)
it is an adverb where x(f g h^:)
is the same as (f g h)^:x
. It is most useful for ^:
, but it works for other conjunctions too.
Other tips
- Tips for golfing in J (Code Golf SE)
- For testing code outside code.golf, installing the latest J locally is recommended. However, Attempt it online!/J or J Playground can be used to run J online and support the latest release.
- code.golf version does not have
dev/fold
installed, so you cannot use Fold conjunctions. Also regex functions that depend on PCRE do not work.
- J is quite different from any other language on the site (possibly barring Julia). If anyone wants to pick it up seriously, feel free to ask questions in The APL Orchard (requires StackExchange account), APL Farm (requires Discord account), or the J channel of
+aplfarm:matrix.org
Matrix channel collection. Many introductory resources can be found in the APL Wiki (which focuses on APL, but many contents are also relevant to J).
Packers
Simple 2:1
packer
Works with any ASCII-only code, and gives the length of ceil(original/2)+16
. Yet to find the 2:1+11
compression which primo and nwellnhof uses apparently.
NB. compressor
6 u:'source padded to even'
NB. decompressor (submission)
".1 ic 3 u:ucp'compressed'
3:1
packer (only works in special cases)
For this to work, the source code must not contain charcode 96 or higher (`{|}~
and lowercase), which means you cannot use echo
, stdout
(among others), and built-ins { {. {: | |. |: } }. }: ~ ~. ~:
. Also it's probably not worth it when you need to print some literal character of charcode 96 or higher. echo
can be replaced with 1!:2&2
, and ARGV
still works though.
NB. compressor
1 u:u:,_3(241,96+])\3 u:'source padded to multiple of 3'
NB. decompressor (submission)
".u:96|241-.~3 u:'compressed'
Near-3:1
packer (more liberal than above)
Encodes chars above 96 as the start byte of a 2-byte UTF-8 sequence. If there are not enough continuation bytes, it is extended with copies of ?
converted. Combined with the fact that the bytes c0
and c1
are banned in UTF-8, you cannot use `a?
chars in the code. It still compresses better if you try to avoid the bytes above 96 as much as possible.
Compressor in Python (output is the submission)