On with sorting...
I was totally stunned when the |:/:~|: worked yesterday - I would have sworn that I'd tried that (and 100 frustrated variations of it) unsuccessfully before I started writing, and I was only planning to introduce it as a segue to partition sorting. I'll postpone that while I explore this unexpected success.
I haven't found the documentation yet, but it seems irrefutably true that, when sorting a 'matrix' (less typing) by column, J performs tiebreaking via adjacent right values. By controlling the sort order, I can sort my hand suitably. The verb index i. manages the sort order, when used as a dyad, by converting the values into numeric indices, which can then be sorted in the usual numeric sequence. (Is it my imagination, or is that pretty cute?)
'AKQJT98765432SHDC' i. |:unsrtd
16 6
13 8
13 3
16 12
14 11
13 12
16 4
14 1
15 3
13 4
14 12
14 6
14 9
Grade that, and apply to the original, and transpose:
|: ( |: unsrtd) /: 'AKQJT98765432SHDC' i. |: unsrtd
SSSSHHHHHDCCC
JT62 K8 5 3 2 J T8 2
The parentheses are required.
In terms of my main task, which is dealing hands for analysis, that's all I need. I just have to iterate the process across the four hands, which I don't think will be too hard, and format the output into a file somewhere for now. Later I'll look at calling a DLL and handling the results, as well.
Rather than press on, I'm going to pause to write up my partition sort discovery. Otherwise I'll forget. Originally, I was sorting the suits and the values separately - I don't, in retrospect, know why - so I had:
\:~ 0 {"2 unsrtd
SSSSHHHHHDCCC
and
d2 =. (1 {"2 unsrtd ) \: 0 {"2 unsrtd
6J2T3K285J82T
so, with laminate:
SSSSHHHHHDCCC
6J 2T3 K2 8 5 J 8 2T
It took a lot of pain to find the key verb for this process, which turned out to be 'cut' ;. 'Cut' is not a verb, it's an adverb, in point of fact, which says: apply the associated verb to y over partitions defined by a boolean list x. If I could create a boolean list 1000100001100 to represent the suit lengths and apply a sort verb to the partitioned card values, I'd be right.
Thanks to Alvord & Thomson, I had an idea about how to do this. There is a verb 'rotate' |. (they use it to test for duplicates) which gives:
_1 |. SSSSHHHHHDCCC
CSSSSHHHHHDCC
rotating 1 to the right via the _1 index. By testing for the Boolean not-equal ~: I derive my boolean list of distributions.
SSSSHHHHHDCCC
CSSSSHHHHHDCC
1 000 10 0 0 0 11 0 0
Using box < to demonstrate:
1 0 0 0 1 0 0 0 0 1 1 0 0 <;.1 d2
┌────┬─────┬─┬───┐
│6J2T │ 3K285│ J│82T │
└────┴─────┴─┴───┘The 1 in the cut says to start the partition with the 1's in the list.
I want the suits to be sorted in car order, so I'm going to write a verb (otherwise the sentence gets too hard to read) called ssrt.
1 0 0 0 1 0 0 0 0 1 1 0 0 ssrt ;.1 d2
JT62
K8532
J
T82
Woot!
1 0 0 0 1 0 0 0 0 1 1 0 0 < @ ssrt ;.1 d2
┌────┬─────┬─┬───┐
│JT62 │ K8532│ J│ T82│
└────┴─────┴─┴───┘
Introducing the conjunction @ which allows J to execute 2 verbs more or less simultaneously (there are degrees of simultaneity; another time)
Finishing off for now, the verb ssrt:
ssrt =: /: 'AKQJT98765432' & i.
This is using a style of programming J calls 'tacit', due to the absence of apparent variables. Of course the variables are y and x, as always, and the challenge is to manage them. Inside a 'subroutine' the order of operations isn't quit the same as in open code; in fact it's the same as inside parentheses in open code. This matters quite a lot, because of the strictures of data handling.
Take ssrt. It contains 2 verbs, grade and index. We want the x of grade to be the y of index and J provides this via the 'hook' facility, which is just a name for this setup. The y of index will be the y of ssrt. (If ssrt had an x, this would be the x of grade, but in its absence, ssrt's y gets re-used. Can I stop that from happening? I don't know.)
Now, index also requires an x , which in this case is a constant, our suit rank order, and in this case the constant has to be bonded & with the verb.
At this moment of my life, nothing can persuade me that this isn't a mess. So far, a navigable mess, but one that seems...inelegant.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment