(or emacs irrelevant)

Try J

Intro

I imagine that if you're reading this blog, you like to tinker with Emacs. And people who like to tinker with stuff probably also like to learn new programming languages, just for fun. In that case, if you ever want to learn a non-mainstream language, I highly recommend J.

From its homepage:

J is a modern, high-level, general-purpose, high-performance programming language. J is portable and runs on Windows, Unix, Mac, both as a GUI and in a console. J systems can be installed and distributed for free.

The Appetizer

Things J has going for it that are high on my list:

What attracted me to J in the first place is that it consistently has the shortest solutions (and fast-running) on Project Euler. What kept me going after the initial wow-effect, was the extremely elegant standard functions implementation dividing things into verbs, adverbs, and conjunctions.

Something impressive: a Sudoku solver

This code is taken from the ob-J page that I wrote some time ago (you can find a lot of additional info there):

#+begin_src J :exports both
i =: ,((,|:)i.9 9),,./,./i.4$3
c =: (#=[:#~.)@-.&0
t =: [:(([:*/_9:c\])"1#])i&{+"1 1(>:i.9)*/[:i&=i.&0
r =: [:,`$:@.(0:e.,)[:;(<@t)"1
s =: 9 9&$@r@,
]m =: 9 9 $"."0'200370009009200007001004002050000800008000900006000040900100500800007600400089001'
s m
#+end_src

#+RESULTS:
#+begin_example
2 0 0 3 7 0 0 0 9
0 0 9 2 0 0 0 0 7
0 0 1 0 0 4 0 0 2
0 5 0 0 0 0 8 0 0
0 0 8 0 0 0 9 0 0
0 0 6 0 0 0 0 4 0
9 0 0 1 0 0 5 0 0
8 0 0 0 0 7 6 0 0
4 0 0 0 8 9 0 0 1

2 8 4 3 7 5 1 6 9
6 3 9 2 1 8 4 5 7
5 7 1 9 6 4 3 8 2
1 5 2 4 9 6 8 7 3
3 4 8 7 5 2 9 1 6
7 9 6 8 3 1 2 4 5
9 6 7 1 4 3 5 2 8
8 1 3 5 2 7 6 9 4
4 2 5 6 8 9 7 3 1
#+end_example

It's pretty amazing that the whole implementation, not counting the example input matrix, takes only 169 characters. You can also see how functional the language is.

Something simpler: a factorial

To have a more simple example, here's how to write down incrementally the factorial of 20:

#+begin_src J
i.20
#+end_src

#+RESULTS:
: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

#+begin_src J
1 + i.20
#+end_src

#+RESULTS:
: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

#+begin_src J
*/ 1 + i.20
#+end_src

#+RESULTS:
: 2432902008176640000

The spaces are optional, I've only included them to make the code more clear.

Something visual: a sine plot

#+begin_src J
load 'plot'
plot 1 o. 0.1 * i.200
#+end_src

By simply pressing C-c C-c on this source block you get this image generated and opened in your browser:

ob-J-sine.png

The Emacs Tooling

As mentioned above, there's org-mode babel support for J, including session interaction. More importantly, there is j-mode. The same package also provides a REPL via jconsole.

Additionally, I wrote down a learning/assisting tool helm-j-cheatsheet. Here's one of the screenshots:

helm-j-cheatsheet

It allows to:

  • insert a function by English name
  • look up the English name of the function by symbol
  • open the J documentation for a function

Outro

J is a very cool language to try and I hope you give it a go. I haven't yet managed to find a nice use for it, but you could say that, just as learning LISP, learning J can make you better at other languages.