Basics
Printing
- Use
echoto 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.9echo,.2^i.9(creating and printing a 9×1 array) is not so useful because the result is right-aligned.
echocan optionally take a left argument.x echo yis equivalent to(echo^:x)y. This is particularly useful whenxis the result of some condition.echotruncates long lines (>256 characters). To avoid this, applystdoutto a string argument:stdout 257$'x'stdoutis defined as1!:2&4, see1!:docs- To print a rational number in decimal, to 1000 digits of precision:
stdout 0j1e3":1r7
- J formats negative numbers with a leading
_. If you need to print with a leading-instead, the8!:nforeign 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!
Input
ARGVis a list of boxed-string program arguments.2}.ARGVchops off the first two (/usr/bin/jand/tmp/code.ijs).f&>2}.ARGVappliesfto each unboxed argument.- Example: print each argument concatenated with its reverse (
|.){{echo y,|.y}}&>2}.ARGV(direct definition,yis the argument)echo&(,|.)&>2}.ARGV(tacit)
- Example: print each argument concatenated with its reverse (
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)yis the same as(u&n^:x)y,x(n&u)yis the same as(n&u^:x)yu@nis the same as(u n)"_. For example,(*i.@4)10is0 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 4is the same as|."2 i.2 _3 4 - If a train ends with a conjunction, i.e
(f g h^:)it is an adverb wherex(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/foldinstalled, 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.orgMatrix 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.