Some notes about J from today

Charting

   chart =. {{ l {~ >. (<: #l =. u: 16b20, 16b2581 + i.8) * y % >./ y }}
   ] dat =. 10 ? 20 
13 18 9 0 1 4 16 19 3 7
   chart dat
▆█▄ ▁▂▇█▂▃

tsvector in J

A tsvector in Postgres is the tokens of the input and their locations. We can compute this in J by cutting the input and associating it with the number of words of the input. Here is the Postgres example:

SELECT to_tsvector('english', 'a fat  cat sat on a mat - it ate a fat rats');
                  to_tsvector
-----------------------------------------------------
 'ate':9 'cat':3 'fat':2,11 'mat':7 'rat':12 'sat':4

In J:

   txt =. 'a fat  cat sat on a mat - it ate a fat rats'
   w ([,<@])/.. i. #(w =. ;: txt)
┌────┬──────┐
│a   │0 5 10│
├────┼──────┤
│fat │1 11  │
├────┼──────┤
│cat │2     │
├────┼──────┤
│sat │3     │
├────┼──────┤
│on  │4     │
├────┼──────┤
│mat │6     │
├────┼──────┤
│-   │7     │
├────┼──────┤
│it  │8     │
├────┼──────┤
│ate │9     │
├────┼──────┤
│rats│12    │
└────┴──────┘

There are some differences due to cleanup that Postgres does on the input which I’m not doing. But the idea is pretty much the same.

Collecting Data using /..

Considering the following:

   ]data =: _2 ]\ 'Fred';90;'John';75;'Fred';95;'John';85;'Susan';100;'John';86
┌─────┬───┐
│Fred │90 │
├─────┼───┤
│John │75 │
├─────┼───┤
│Fred │95 │
├─────┼───┤
│John │85 │
├─────┼───┤
│Susan│100│
├─────┼───┤
│John │86 │
└─────┴───┘

The documentation provides the following solution:

   ]s =: ({."1 data) <@;/. ({:"1 data)   NB. Use first column as keys; collect second-column values
┌─────┬────────┬───┐
│90 95│75 85 86│100│
└─────┴────────┴───┘
   (~. {."1 data) ,. s                   NB. Reinstall the name
┌─────┬────────┐
│Fred │90 95   │
├─────┼────────┤
│John │75 85 86│
├─────┼────────┤
│Susan│100     │
└─────┴────────┘

I found my own solution:

   (}:"1 data) ([,<@;@])/.. ({:"1 data) 
┌─────┬────────┐ 
│Fred │90 95   │ 
├─────┼────────┤ 
│John │75 85 86│ 
├─────┼────────┤ 
│Susan│100     │ 
└─────┴────────┘

The channel found this:

   (,<@;)/../|:data
┌─────┬────────┐
│Fred │90 95   │
├─────┼────────┤
│John │75 85 86│
├─────┼────────┤
│Susan│100     │
└─────┴────────┘

This notices that we have one column on the left and one on the right and inserts the calculation between them, reducing the amount of tearing apart we have to do by hand.

   (}:"1 (,<@;)/.. {:"1)

This is a tacit definition from someone else on the channel, which reduces parens by using a tacit definition.


Tags
j

Date
April 22, 2025