4 ms·
Will the vector solution thing still be elegant if parentheses that are escaped with a backslash don't count? Oh, and backslashes can also be escaped, so the p
by clarry 6y ago
Will the vector solution thing still be elegant if parentheses that are escaped with a backslash don't count? Oh, and backslashes can also be escaped, so the paren in "\\(" still counts. Oh and you can have a quoted segment in the string where parens don't count. As expected, quotes can be escaped..
I bet there are elegant solutions, and perhaps they are obvious to those who are well versed in array programming. What I think the world needs is a book/tutorial/whatever that demonstrates elegant solutions to things that seem ugly and not amenable to vector processing (at least in the obvious and naive way). I think we have enough (too many) tutorials showing how to calculate means and such :(
- beagle3 6y agoElegance is in the eye of the beholder, of course - a state machine is easier to code in K than in almost any other language, and is likely the most elegant overall solution. A common kludgy solution you’d see would probably be to delete or zero out characters that shouldn’t be counted, and then do the original thing; some would consider that elegant and others won’t. There are many examples if you look for them, many well explained, e.g. https://nsl.com https://nsl.com ; the problem is APL/K is almost as different from C/Java as Japanese is from English ; there’s no amount of chewing anyone will do for you that will save you from going deep, and if you’ve decided to go deep, the material is already available.
- clarry 6y agoSounds good, I love state machines and I often write them in C where other people would just go with ad-hoc loops and branches and temporaries. I'll keep an eye out for examples..
- avmich 6y agoRecently there was a link on simdjson, a C++ parser for JSON which aims to be record fast. It uses vector instructions of modern processes, but if you'll watch the video presentation, Daniel Lemire will also explain how they handle finding string boundaries - with all those escapes needed - without branching, purely logical operations. Could be a good example for how APL approaches things.
- tluyben2 6y agoThe one thing that makes you think different when working with these languages for a bit, is that your mind has to move differently from when you are doing 'regular' programming. > Will the vector solution thing still be elegant if parentheses that are escaped with a backslash don't count? Oh, and backslashes can also be escaped, so the paren in "\\(" still counts. Oh and you can have a quoted segment in the string where parens don't count. As expected, quotes can be escaped.. You name 4 additional 'features' to the issue; in a c-like language, for example, you would amend the original problem with extra code to add these in. And, usually, you can recognise the original problem solution somewhere inside the final code. In k you might find that the final solution is the same size as the original but has no (seeming) overlap at all. You might find, that when you bring the problems as managers often do; after you implemented 1 thing, they would in with 'oh, it does this as well, amirite?', that you could end up with 5 solutions that are completely different but the last one delivers the features you required. Good or bad, that's a matter of taste, situation etc.
- icen 6y agoYou build vectors of the other conditions, and subtract them before the running sum.