3 ms·
i keep hoping that one day i'll understand j or k well enough that it won't take me hours to decipher a few lines of it; but today i am less optimistic about th
by kragen 3y ago
i keep hoping that one day i'll understand j or k well enough that it won't take me hours to decipher a few lines of it; but today i am less optimistic about this, because earlier tonight, i had a hard time figuring out what these array-oriented lines of code did in order to explain them to someone else
textb = 'What hath the Flying Spaghetti Monster wrought?'
bits = (right_shift.outer(array([ord(c) for c in textb]),
arange(8))).ravel() & 1
and i wrote them myself three months ago, with reasonably descriptive variable names, in a language i know well, with a library i've been using in some form for over 20 years, and their output was displayed immediately below, in https://nbviewer.org/url/canonical.org/~kragen/sw/dev3/random-truncated-correlation.ipynb https://nbviewer.org/url/canonical.org/~kragen/sw/dev3/rando...
i had every advantage you could conceivably have! but i still guessed wrong at first and had to correct myself after several seconds of examination
i suspect that in j or k this would be something like (,(@textb)*.$i.8)&1 though i don't know the actual symbols. perhaps that additional brevity would have helped. but i suspect that, if anything, it would have made it worse
by contrast, i suspect that i would have not had the same trouble with this
bits = [(ord(c) >> i) & 1 for c in textb for i in range(8)]
however, as with rpn, i suspect that j or k syntax is superior for typing when what you're immediately evaluating expressions rather than writing a program to maintain later, because the amount of finger typing is so much less. but maybe i just have a hard time with point-free style? or maybe, like you say, it's different types of people. or maybe i just haven't spent nearly enough time writing array code during those years
- vidarh 3y agoI think the K would likely be both simpler and harder than your first example by reading very straightforwardly in a single direction but with operators reading like line noise. In your case, my Numpy is rusty, but I think this is the Ruby equivalent of what you were doing? textb = 'What hath the Flying Spaghetti Monster wrought?' p textb.bytes.product((0...8).to_a).map{_1>>_2}.map{_1 & 1} Or with some abominable monkey patching: class Array def outer(r) = product(r.to_a) def right_shift = map{_1>>_2} end p textb.bytes.outer(0...8).right_shift.map{_1 & 1} I think this latter is likely to be a closer match to what you'd expect in an array language in terms of being able to read in a single direction and having a richer set of operations. We could take it one step further and break the built in Array#&: class Array def &(r) = map{_1 & r} end p textb.bytes.outer(0...8).right_shift & 1 Which is to say that I don't think the operator-style line-noise nature of K is what gives it its power. Rather that it has a standard library that is fashioned around this specific set of array operations. With Ruby at least, I think you can bend it towards the same Array nature-ish. E.g. a step up from the above that at least contains the operator overloading and instead coerces into a custom class: textb = 'What hath the Flying Spaghetti Monster wrought?' class Object def k = KArray[*self.to_a] end class String def k = bytes.k end class KArray < Array def outer(r) = product(r.to_a).k def right_shift = map{_1>>_2}.k def &(r) = map{_1 & r}.k end p textb.k.outer(0...8).right_shift & 1 With some care, I think you could probably replicate a fair amount of K's "verbs" and "adverbs" (I so hate their naming) in a way that'd still be very concise but not line-noise concise.
- kragen 3y agothat all seems correct; the issue i had was not that python is less flexible than ruby (though it is!) but that it required a lot of mental effort to map back from the set of point-free array operations to my original intent. this makes me think that my trouble with j and k is not the syntax at all. but conceivably if i study the apl idiom list or something i could get better at that kind of thinking?
- vidarh 3y agoI think you could twist Python into getting something similarly concise one way or other ;) It might not be the Python way, though. I agree it often is painful to map. I think in particular the issue for me is visualizing the effects once you're working with a multi-dimensional set of arrays. E.g. I know what outer/product does logically, but I have to think through the effects in a way I don't need to do with a straightforward linear map(). I think I'd have been more likely to have ended up with something like this if I started from scratch even if it's not as elegant. p textb.bytes.map{|b| (0...8).map{|i| (b>>i) & 1} }.flatten EDIT: This is kind of embarrassing, but we can of course do just this: textb.bytes.flat_map{_1.digits(2)} But I think the general discussion still applies, and it's quite interesting how many twists and turns it took to arrive at that
- rak1507 3y agoThe k solution is ,/+|(8#2)\textb k doesn't have a right shift operator, but you don't need that, you can use the base encoding operator instead Personally I think this is clearer than both the array-ish python and the list comp. https://ngn.codeberg.page/k/#eJwrSa0oSbJSCs9ILFEA4gyFkoxUBbecysy8dIXggsT0jNSSkkwF3/y84pLUIoXyovzS9IwSeyUuHX3tGg0LZSPNmBKQCQCRmBiW https://ngn.codeberg.page/k/#eJwrSa0oSbJSCs9ILFEA4gyFkoxUBbe...
- kragen 3y agothank you very much! which k implementation does this page use? oh, apparently a new one called ngn/k: https://codeberg.org/ngn/k https://codeberg.org/ngn/k
- vidarh 3y agoAs much as it's a struggle to read, you got to love the people implementing this are dedicated enough to write their C as if it was K.
- vidarh 3y agoI'm sitting here with the K reference manual, and I still can't decode this. I'm wildly guessing that your approach somehow ends doing something closer to this: textb.bytes.flat_map{_1.digits(2)} Which I must admit it took me embarrassingly long to think of.
- kragen 3y agoi can't decode it either but i think you're right. note that this probably gives the bits in big-endian order rather than little-endian order like my original, but for my purposes in that notebook either one would be fine as long as i'm consistent encoding and decoding
- rak1507 3y agoIt would do if it the bits weren't reversed, which is done with |