J syntax for canonical sorting?

Viewed 100

I am sorting through some J code, and happened upon this line of code:

] { /:@~.

I am trying to understand its use and function. Could someone help me read it, or use it? There is a label on it that states it is a canonical function.

1 Answers

To be honest, I am not exactly sure what your section of code is trying to do. Is it possible that it is part of a larger tacit verb?

In the meantime, there are parts that certainly could be useful in sorting the unique items of a vector.

   ~. 3 4 5 2 3 4     NB. returns the unique items of a vector
3 4 5 2
   /:@~. 3 4 5 2      NB. returns the indices that will order that vector
3 0 1 2
     3 0 1 2  { 3 4 5 2 NB. using { to select indices 3 0 1 and 2 from the vector.
2 3 4 5
    (] {~ /:@~.) 3 4 5 2 3 4 NB. perhaps this is what your code should have been?
2 3 4 5

The parenthesis around ] {~ /:@~. are important as they make the three verbs, ],{~ and /:@~. into a fork which takes ] and /:@~. and makes them into the left and right arguments to {~ The adverb ~ reverses the left and the right arguments of the verb {. The right argument we know is /:@~. which are the unique indices 3 0 1 2 and the left argument is ] which is the original list 3 4 5 2 3 4. Reversing the two would be equivalent to

     3 0 1 2  { 3 4 5 2 3 4
2 3 4 5

So, perhaps you meant (] {~ /:@~.) which would sort the unique items of a vector?

Hope this helps.

Related