J is my calculator
April 30, 2020.I used to use node
(javascript) as my CLI calculator. (I know, I know.) After realizing how silly I was for using such an inexpressive language, I tried to learn calc
and bc
. The next few I went through were:
python
ghci
(Haskell)R
octave
(matlab equivalent)- and finally, J
I don’t have a good reason to settle with J. Maybe I’ll change it soon. But here’s my review of J, collected slowly over a few months of usage:
Pros:
- Hassle-free rationals: write
1%2r127+3r160
instead of1/((2/127)+(3/160))
, and get20320r701
() as a result. - No need to repeat an operator. Write
*/12 52 65 13 42
rather than12*52*65*13*42
- Built-in polynomial evaluation at multiple points. Write
_4 2 1 1 p. 1 2 3
to evaluate x³+x²+2x-4 at x={1,2,3}. - The idiom for convolution (polynomial multiplication) is
+//. @: (*/)
(take outer product*/
and sum+/
the antidiagonals/.
) - Any user defined function is an operator just like
+
- Hooks and forks make some expressions incredibly easy to write. Examples:
(+/%#) 2.252 2.284
evaluates to the mean of the list:2.268
2.268 (-,+) 0.016
evaluates to the interval2.252 2.284
(f,g,h) x
evaluates multiple functions on the same datax
(say, mean median range)- recent example for me:
(13.342-7.666)%%:+/18.140 14.378(*:@[*%)578 379
evaluates
Cons:
- You can only share with other J users (not like you’re going to share one-off calculations though)
- Need to get used to how every operator associates right without exception
- Have to understand how J evaluates things in order to refactor expressions
- Configuration is not obvious: e.g.
(9!:11)4
sets displayed floating point precision to 4. - Jagged or non-homogenous arrays requires you to learn the messy art of value boxing/unboxing
- Working in hex (or any base) is a hassle if you want hex output. You can input hex with
16bdeadbeef
for example, but all output is in decimal (unless you pass it through the hex-printing(4+#@$) }. 2&(3!:3)
or use the printf addon). - It’s hard to find the right operator for the job if you don’t know it by heart. It’s just like in mathematics. Languages like J suffer from lack of discoverability.
OCR at your fingertips (part 3)