4 ms·
If someone produces smaller and faster code than me, then I should want to learn from it. I wonder why other people have the exact opposite reaction. Why do yo
by geocar 12y ago
If someone produces smaller and faster code than me, then I should want to learn from it. I wonder why other people have the exact opposite reaction.
Why do you think that is?
- beagle3 12y agoI don't understand it either. But it happens everywhere, not just in code. Most people are only interested in the "truth" and "facts" as long as it fits within their existing world view. And things like K rarely do.
- gcanyon 12y agoI'm not sure -- I know that after the fourth or fifth time solving a problem on projecteuler.net in 20 lines of code and seeing someone post a 1-line J/K solution, I went and downloaded J. I even managed to solve a few euler problems with it, which I regard as a large accomplishment for a novice. I like to tell people I've written a whole twenty or so lines of code in J!
- kelnos 12y agoI do enjoy learning about such things, but, for most of the work I do, performance is nowhere near at the top of the list of things I care about. Also in the past I've been burned by code that's small/fast but is otherwise utterly unmaintainable. I'm not saying that's the case here, but... past experience, and all that tends to color perceptions. I think with a language like k or q, which appears to be purpose-built for certain types of problems, people look at it and get easily confused and discouraged because it's so different from all the more mainstream general-purpose programming languages they're used to. And it's a lot easier to put down something you don't understand than to admit you don't get it, or to spend lots of time learning something that may not be of much use to you. Kinda sucks, but it's often human nature.
- geocar 12y agoOne benefit to a short program is that there's not much code to rewrite if you can't read something. This doesn't happen very often, but I find the thought comforting.
- beagle3 12y ago> I think with a language like k or q, which appears to be purpose-built for certain types of problems, The thing is, it's not purpose built, and it doesn't even appear to be if you suspend your disbelief. The only reason you'd think it is purpose built is because "well, it can't be this short if it wasn't purpose built". But if you go over the manual, and find special built operators, please tell us what they are. e.g., to compute an average, you can use the function avg:{(+/x)%#x} - with the exception of parentheses, every character has an orthogonal function. Similarly, the maximum subarray sum solution mss:|/0(0|+)\ ; and there are many others. And it's not just math stuff - http://nsl.com http://nsl.com has lots of other examples of many kinds -- and most importantly -- is an operating system + GUI not general enough?
- robryk 12y agoHow would you represent a graph and implement DFS in K?
- fidotron 12y agoNumbers in arrays can be treated as pointers, into that same array, giving a graph. It's just a question of context. If you were storing RDF triples in K you'd simply have an array for subjects, one for objects, one for predicates, and one of the URIs/text. Simply store the index of the URI/text item in each of the subject, object and predicate columns. An individual triple would be formed by the same index applied to the subject, object and predicate arrays. DFS is then a variation of the more familiar functional style of tackling the problem where you have your end condition (i.e. something that matches what you're looking for) and failing that do something else (typically recursion). I can't recall enough of the K syntax these days to actually implement that right now though, or if K has TCO.
- beagle3 12y agoIf you are really interested, http://nsl.com/ http://nsl.com/ is a treasure trove - quite a few of the examples are extremely well documented, some or not, but there's a wealth of information there. Specifically about graphs, you can look at: http://nsl.com/papers/order.htm http://nsl.com/papers/order.htm - topological sorting http://nsl.com/k/tarjan.q http://nsl.com/k/tarjan.q - strongly connected components http://nsl.com/k/loop.q http://nsl.com/k/loop.q - find loops in graphs I think in all of these the graph is represented either as a list of edges or a dictionary of node->(list of nodes that it has edges to)
- Dylan16807 12y agoIt all depends on how you define "small and fast". Comments obviously are not code, so it's reasonable to complain about lack of comments. You suggested wordcount, I think wordcount is good, so it's reasonable to complain about single letter words rather than descriptive words. uberalex's suggestion for reformatting wouldn't change the algorithm or speed. It would simply spread operations across more lines. That also seems like a reasonable thing to ask, to me. They can learn your method either way. Edit: I mean, I'm sure fitting more on the screen is valuable, but people already know how to fit many times as much code onto a screen. They avoid it on purpose for whatever reason.
- geocar 12y ago>I mean, I'm sure fitting more on the screen is valuable, but people already know how to fit many times as much code onto a screen. They avoid it on purpose for whatever reason. I think this reason (whatever it happens to be) is probably wrong.
- astazangasta 12y agoHey, just read bytecode, then. As small and fast as you can get.
- icsa 12y agoI think that "It's what I'm used to." is the main reason - intellectual comfort zone. Having learned BASIC, FORTRAN and Pascal, C seemed like line noise - at first. As did PERL. And then k. Btw, COBOL seemed "too verbose". Once I actually started writing many k programs and then reading even more of them, I was able to recalibrate for the abstraction/density. I moved my intellectual comfort zone. Ironically, I was already there with mathematics. However, programming languages were different :). Now, as a result, every time I have to read Java, I suffer from a kind of fatigue - having to read way too much code to glean the writer's intent. I just want them to get to the F'ing point. N.B. - Mathematical literature/writing went through this same transition during the Renaissance. Equations were described in natural language (not unlike COBOL). A simple polynomial could require a paragraph of text to describe.