Saturday, July 16, 2016

J provokes self-talk (4)

OK, so back to the main game. I've managed to create something that looks vaguely like a hand of cards.
┌─────────────┐
│CSSCHSCHDSHHH   │
│8 6J 2 32T KJ T2 8 5   │

└─────────────┘
I'll sort the formatting out later. I mentioned earlier that Volker & Thomson clearly aren't card players; who ever played with an unsorted hand? Never mind, sorting (/:~ ) is one of my favourite things.

< /: unsrtd
┌─────────────┐
│8 6J 2 32T KJ T2 8 5   │
│CSSCHSCHDSHHH   │

└─────────────┘
This clearly is not much use. What seems to have happened is that we've sorted each 'column'. Well, maybe I can transpose (|:) and sort...

< |: \: ~ |: unsrtd
┌─────────────┐
│SSSSHHHHHDCCC   │
│TJ6 2K8 5 3 2 J T8 2   │

└─────────────┘
...and transpose back. Great! Well, more or less. Have to work out what's going on with that STJ in a moment. Meantime, what's happening with that sort statement? It's very interesting. /: is called 'grade', and what it outputs, when applied to one input, is a list, in sorted order, of the original positions - the indices - of the data to be sorted. Eg:

 n_suits
CSSCHSCHDSHHH
/: n_suits
0 3 6 8 4 7 10 11 12 1 2 5 9      

You can see the 0th, 3rd, 6th elements are the CCC of the sorted list. This list of indices can be applied to any other list, but for now at least, it's enough to apply it to itself:

n_suits /: n_suits
CCCDHHHHHSSSS                

Or, as above, use the shortcut ~ which says, in effect, use the y input additionally as x input. This also illustrates another feature of J - the fact that a verb (function, operator,...) can have two different  functions, depending on whether it has one input (a monad) or two (a dyad). (Note: /: = grade down \: = grade up)

It seems very fortunate to me that the suit values are so nicely sorted internal to the suits. I mean, in the original list the order of the C was 8,T,2. Why when relocating the C did J relocate them to positions 1,0,2? The order, in itself, of the S is easy to understand; since we're descending (to get SHDC), the T comes before the J - but it seems too clever by half to decide to partition sort without being asked. Is this always going to happen? Should I test it, ransack the documentation, or work out a partition sort? Plus, I need to have a custom sequence...

I'm going to work these two functions out, because I really can't believe the good fortune of that list sort. Maybe I'll find the documentation serendipitously.

No comments:

Post a Comment