5 ms·
what is `<<` ("ordinal") supposed to be doing? i understand how `<` works, but how is it useful to apply it a second time? the closest i got to finding some pa
by uryga 7y ago
what is `<<` ("ordinal") supposed to be doing? i understand how `<` works, but how is it useful to apply it a second time?
the closest i got to finding some pattern is this:
>>> xs
'34210'
>>> grade(xs)
[4, 3, 2, 0, 1]
>>> grade(grade(xs))
[3, 4, 2, 1, 0] # :O
where
grade = lambda xs: (
[i for (i,_) in sorted(enumerate(xs), key=swap)]
)
swap = lambda p: (p[1], p[0])
no clue what it means though.
(i guess "ordinal" really is too ambiguous...)
EDIT
alright, i see it now - `<<xs` is "for each item x of xs, where does x land when you sort xs?
- icen 7y ago< grades upwards: for each element, the element of the returned array is its position if the array is sorted. x: 10?A:"abcdefghijklmnopqrstuvwxyz" x "ysyselacwl" +`x`y`z!(x;<x;<<x) x y z --- - - "y" 6 8 "s" 7 5 "y" 4 9 "s" 5 6 "e" 9 2 "l" 1 3 "a" 3 0 "c" 8 1 "w" 0 7 "l" 2 4 So <x gives a sort index, <<x tells you how far each element is from the minimum in that sort index
- uryga 7y agoyeah, i figured it out eventually. since you seem knowledgeable about K, would you mind taking a look at my second comment in this thread (about inverting permutations) and seeing if it makes sense? thanks!
- i_don_t_know 7y agoI still don’t understand what the numbers in column y mean. I understand column z and that’s also what grade up is returning in John Earnest’s oK: x:“baced“ <x 1 0 2 4 3 <<x 1 0 2 4 3 That is, for example, when sorting, the item at position 1 in the original string (“a”) moves to position 0 (the beginning of the sorted string). So in the end you get “abcde”. What does column y mean? If I sort the original with it, I get “wllaysysce”. Has this changed between versions of K?
- i_don_t_know 7y agoNevermind. I've figured it out with the help of the reference manual at http://www.nsl.com/k/k2/k295/kreflite.pdf http://www.nsl.com/k/k2/k295/kreflite.pdf. I knew it was a permutation but I applied it incorrectly.
- chrispsn 7y agoThis may help - I found it confusing at first too: https://twitter.com/kcodetweets/status/1236904643684270080?s=21 https://twitter.com/kcodetweets/status/1236904643684270080?s...
- uryga 7y agoone cool explanation i found is that grading a permutation list effectively gives you its inverse: xs = "cab" <xs = [1,2,0] = { 0 → 1 1 → 2 2 → 0 } <<xs = [2,0,1] = { 0 → 2 1 → 0 2 → 1 } which is exactly what "ordinal" is supposed to do! it kinda makes sense - grading the indexes given by `<xs` gives a permutation list that maps each index back to its actual position (i.e. [1,2,0]==>[0,1,2]). or at least it seems that way if you stare long enough, would be good if someone could confirm!