8 ms·
Here's the text editor they're talking about: http://www.kparc.com/edit.k http://www.kparc.com/edit.k The code is, well, not the easiest to understand.
by bshimmin 12y ago
Here's the text editor they're talking about: http://www.kparc.com/edit.k http://www.kparc.com/edit.k
The code is, well, not the easiest to understand.
- outside1234 12y agoThat's an understatement. If we are allowed code like this, I can write an editor in ONE line of JavaScript. :)
- deleted 12y ago[deleted]
- TheOsiris 12y agoI think he does a better job than uglifyjs can
- beagle3 12y agoNote that his lines are < 60 chars. Can you write this editor in one line of less than 6000 chars of javascript (without a "textarea")?
- calinet6 12y agoIt does not matter. This is not a programming language; languages are designed for human comprehension. Human comprehension is an irreducible part of the whole paradigm of programming, being necessary to understand the nearly always complex logic required to describe anything of value. This is gibberish. It fails at the one task of a programming language: to translate something a human can understand into something a computer can understand, with as little confusion as possible.
- dang 12y agoAny language you don't know is gibberish—hence the proverb, "it's all Greek to me." Please don't dismiss the unfamiliar just because it's strange.
- beagle3 12y agoNo, it is just not a language for the masses who have gotten used to all programming language syntax being minor variations on the Algol/Pascal/C theme. Much like math and physics, this is an extremely terse notation that is immensely useful to those who practice it, and looks like gibberish to those who have not attempted to study it. You could give the same comment about an article in any area of math whose notation you are unfamiliar with -- and you would be just as equally wrong.
- deleted 12y ago[deleted]
- serf 12y agoWell, he's right in a code-commenting way. If you have a sudoku solver that's entirely commented by the phrase 'sudoku', that's generally an indicator of code that is optimized for the computer rather than the reader--regardless of the language used.
- avmich 12y agoYou owe to yourself to read the Iverson's Turing lecture, "Notation as a tool of thought". You can find it. Apparently APL family can claim relationship to math notation - which is not a small claim. Lisp family does it too - in different way.
- sxp 12y agoSomeone skilled in writing compact JS probably can. People write amazing programs in 1k & 4k of JS: http://www.google.com/webhp?#q=javascript+4k http://www.google.com/webhp?#q=javascript+4k
- beagle3 12y agoYes. There is, however, a fundamental difference between codegolfed K and codegolf in essentially any other language: Codegolfed k uses 1-letter variable names, and drops white spaces (which, unfortunately, is kind of idiomatic). But everything else is idiomatic even for the non-golfers. Codegolfed JS or C much more often than not is non-idiomatic in order to save all that space. e.g., compare the K Sudoku solution from http://kparc.com/z/game.k http://kparc.com/z/game.k to any other language, and you'll see that the K code is almost a generic solver (the 3, 9 and 10 stand for box size, board size and number of options respectively while setting up the constraints for the solver), whereas every other short solution (that usually come about twice as long to 10 times as long) is extremely specific to the standard sudoku table. That's not random - idiomatic K solutions tend to be more generic than other languages. The syntax and semantics gently push you towards more general and more efficient solutions compared to most languages, in that the more general (and/or efficient) solution is usually shorter and simpler to code than a specific case. I know it sounds unbelievable. But you might believe it when you see example after example and realize that it's not just a "one trick pony". And still, most people surround it with a SEP field. I recommend spending the time needed to grok it instead. [Or APL or J; my personal preference is K, but to each his own]
- sz4kerto 12y agoWhy do you exclude textarea? It's a high-level functionality available in JS. That q code uses the high-level functionality of q. What's the difference?
- beagle3 12y agoNo, it's part of webkit/blink/gecko, it's not part of JS. But that's not why I exclude textarea: I exclude textarea for the same reason dictionaries define something without using the same word and base form. And the argument is generally meaningless: You may just as well call emacs and say you are using "the operating system's high level functionality".
- kemayo 12y agoDoes that k code implement cursor-movement / scrolling / etc itself, or is it relying on some sort of system functionality for it?
- Jupe 12y agoOpen a new tab and enter this (sans quotes) into the address bar: data:text/html, <html contenteditable> Done :)
- zokier 12y agoHere is the four line version: http://www.kparc.com/$/edit.k http://www.kparc.com/$/edit.k (lines beginning with / are comments)
- xdissent 12y agoI think the first line imports view.k: http://www.kparc.com/$/view.k http://www.kparc.com/$/view.k
- kemayo 12y agoSince I don't know how to read K, and it's not really very friendly to amateurs who're just scanning a program without a reference open... is this equivalent to a bare C program implementing an editor with no libraries, or is it relying on stuff the system provides?
- nitrogen 12y agoI see cz cx and cv in the four-line version, which seem like they are referring to Ctrl-Z, Ctrl-X, etc. If that is the case, then there is a lot of behind the scenes support stuff to get from those four lines of code to a window on the screen or a buffer in a terminal.
- RickHull 12y agoObviously bug free! Regarding intelligibility / comprehensibility: it's somewhere between brainfuck and Perl's worst examples of line noise
- tlrobinson 12y agoCould someone who can parse (reverse engineer?) this write up an explanation? (if you exist)
- geocar 12y agoI'll give this a shot. I'll try to explain what's in my mind as I read it as well. First, get out the reference manual: http://kparc.com/k.txt http://kparc.com/k.txt and we'll do the first couple lines. The sequence that goes f x applies x to f. this f is unary. The sequence that goes x f y applies x and y to f. this f is binary (and just labelled verb). Some things (adverbs) go f a x and apply f in some special way to x. Last hint: You read this code from left to right (like english). Do not scan it. Now let's dive in: c::a$"\n" This says: c is a view of where a is a newline. That is, if "a" contains a file, then c is the offsets of each newline. A view is a concept in k that is unusual in other languages: When "a" gets updated, then "c" will automatically contain the new values. This is similar to how a cell in Excel can refer to other cells and reflect whatever the value of those cells happens to be. b::0,1+c This says: b is a view of zero join one plus c. That is, where "c" contains the offsets of each newline, 1+c would contain the beginning of each new line. We joint zero to the beginning because of course, the beginning of the file is also the beginning of a line. d::(#c),|/-':b This says: d is the view of the (count of c) joined with the max reduce, of each pairs difference of b. That sounds like a lot, but "each pairs difference of b" (where b is the position of all the new lines) is also the length of each line, and the max reduce of that is the longest line. You might say that "d" is the dimensions of the file. i::x,j-b x:b'j This says: i is a view of x (?) joined with j (?) minus b (the offset of the beginning of each line) applied to each x which is defined as the bin of j in b. j hasn't been defined yet, but you can see that x is defined later in the definition, and used to the left. This is because K (like APL) executes from right to left just like other programming languages. you write a(b(c(d()))) in C and you're executing the rightmost code first (d) then applying that leftwards. We can do this with anything including definitions. The other string thing is that we know that b is the offset of the beginning of each line, and yet we're applying something to it. This is because k does not distinguish between function application and array indexing. Think of all the times you write in JavaScript x.map(function(a) {return a[whatever] }) when you'd really just like to write x[whatever] -- k let's you do this. It's a very powerful concept. On that subject of binning: b'j is going to find the index of the value of b that is smaller or equal to j. Since we remember that b is the offset of the beginning of each line, then if j is an offset in the file, then this will tell us which line it is on(!) But we don't understand what j is yet; it's the next definition: j::*|k This says: j is a view of the last (first reverse) of k. We don't know what k is yet. f:"" This says: f is defined as an empty string. g::a$f This says: g is a view of the offsets of f (an empty string) in a. Initially this will be null, but other code will assign to f later making g a value. Next line. s::i&s|i-w-2 This is very straightforward; & is and, and | is or. While excel doesn't let us use a cell in it's own definition, k does: It means the old value of s. So this is literally: the new view of s is i and the old view of s or i minus w (?) minus 2. We don't know what w is yet. S:{s::0|x&d-w} This is a function (lambda). x is the first argument. If we called S[5] it would be the same as setting s to zero or 5 and d (dimensions) minus w. Double-colon changes meaning here; it no longer means view, but set-global. px:{S s+w*x} This requires some knowledge of g/z: http://kparc.com/z.txt http://kparc.com/z.txt Note that px is defining a callback for when the pageup/pagedown routines are called. x will be 1 if we page down and -1 if we page up. It may now become clear that S is a setter of s that checks it somehow. When we understand what w is (later in the program) it will be absolutely clear, but pageup/pagedown are changing s by negative w when pageup, and positive w when pagedown. wx:{S s+4*x} Consulting the g/z documentation, we can see this has to do with wheels. Note we modify s again relative to 4 times x; x is -1 when wheelup and 1 when wheeldown. It becomes clear that s is modified by the pageup/pagedown and the mouse wheel. cc:{9'*k_a} Again: in the documentation, 9' stashes something in the clipboard. cc is a function that takes the first slice of offsets k (we still don't understand) in a (the file). The g/z documentation says that cc is the callback for control-C. This is expected as control-C traditionally copies things to the clipboard. Since the slice of offsets k in a are being saved in the clipboard, we may guess at this point that k will contain the current selection. This process is time consuming, but it is to be expected: Learning english took a while at first, and often required consulting various dictionaries. Eventually you got better at it and could read somewhat quickly. I don't know if you want to try the next few lines yourself to see if you can get a feel for it, or if you want to try one of the less dense examples: * http://www.kparc.com/$/view.k http://www.kparc.com/$/view.k * http://www.kparc.com/$/edit.k http://www.kparc.com/$/edit.k ... or if you want me to keep going like this, or if you want to ask a few questions. What are your thoughts?