6 ms·
f:<:`:index.txt / Open message log append-only h:("\n"/:0::`:index.txt),"\n" / Load message history from log .z.po:{(-:.z.w)h}
by ketralnis 12y ago
f:<:`:index.txt / Open message log append-only
h:("\n"/:0::`:index.txt),"\n" / Load message history from log
.z.po:{(-:.z.w)h} / On join, send full message history
.z.ws:{s:(" "/:($"du"$.z.Z))," ",x; / On message, add server timestamp
{(-:x)y}\:[!:.z.W;s]; / Fan out to other connections
h::(h,s,"\n"); / Update message history in memory
(-:f)s} / Append to message log file
.z.ph:{"\n"/:0::`:chat.htm} / Serve static client app
That is really quite inscrutable
- srpeck 12y agoIs q more readable to you? file:hopen`:index.txt / Open message log append-only history:("\n"sv read0`:index.txt),"\n" / Load message history from log .z.po:{neg[.z.w]history} / On join, send full message history .z.ws:{s:(" "sv(string"du"$.z.Z))," ",x; / On message, add server timestamp {neg[x]y}\:[key .z.W;s]; / Fan out to other connections history::(history,s,"\n"); / Update message history in memory (neg file)s} / Append to message log file .z.ph:{"\n"sv read0`:chat.htm} / Serve static client app
- RodgerTheGreat 12y agoHave you ever read any documentation, tutorials or explanations of K? It seems rather odd to expect to be able to understand a programming language without learning about it first.
- jnbiche 12y ago> It seems rather odd to expect to be able to understand a programming language without learning about it first. Really? You can't understand a new language that's in the same general paradigm as one you know already? Because I can, and I'm no PL genius. I've been able to very quickly understand code written in Swift, Ceylon, Pyret, Rust, Nimrod, and various other new-to-me languages in the past few years. But I can't make heads or tails of K. And that's clearly the way it's meant to be. It's the epitome of an esoteric language.
- RodgerTheGreat 12y agoThat's exactly it- K isn't in the same general paradigm as those languages. K resembles APL and other array programming languages. Syntax among those languages differs in some ways, but the most basic elements are the same. If you knew APL or J, K wouldn't look so alien.
- beagle3 12y agoThat's because Swift, Ceylon, Pyret, Rust, Nimrod make (relatively) small changes in syntax and semantics compared to Python, Java, C and friends - this is the Algol family of languages if you are old enough (and the "C" family if not). Can you read Prolog (assuming you're not fluent in it already)? It is a little farther along from Algol - it is generally verbose, but control flow is implicit (by failure, and negation IS failure), except where you make it explicit by saying '!' (read: "cut"), which means "never backtrack through this point". And there's no assignment, but unification plays a similar part - except it is much weirder (to the uninitiated). K is of the APL / J mold, which is much farther away from Algol and Prolog. It's mostly functional. It takes orthogonality beyond where Haskell does. Examples that might make you look again: "flatten" (convert recursively nested list/tree to one-level list with the same order as a DFS scan) is written ",//" "decode run length decoding" is written ",/#/'" (courtesy of https://github.com/JohnEarnest/ok/blob/gh-pages/examples/runlength.k https://github.com/JohnEarnest/ok/blob/gh-pages/examples/run... Now, these 3-char and 5-char examples are not predefined in any way; They emerge from composing the semantics of the individual characters that make them. Let that sink. Are you aware of any other language in which a straight concatenation of 3 primitives generates a "flatten" function? 5 primitives generates a "run length decoding" function? Something about K/APL/J is distinctly different. And if you believe that it looks different becuase it's "the way it's meant to be", you're missing out on a lot.
- klibertp 12y ago> any other language in which a straight concatenation of 3 primitives generates a "flatten" function? Does J count? ;) A quick look here: http://rosettacode.org/wiki/Flatten_a_list http://rosettacode.org/wiki/Flatten_a_list seems to suggest that in most languages you need at least ~5 primitives (I'm counting for example pattern matching, 'rec' qualifier in OCaml, named let in Scheme etc. as single language primitives, maybe that's too restricted).
- bkeroack 12y agoThat is equally inscrutable. http://code.kx.com/wsvn/code/kx/kdb%2B/c/c/k.h http://code.kx.com/wsvn/code/kx/kdb%2B/c/c/k.h
- RodgerTheGreat 12y agoPerhaps you should start with a manual instead of C headers: http://web.archive.org/web/20041022042401/http://www.kx.com/technical/documents/kusrlite.pdf http://web.archive.org/web/20041022042401/http://www.kx.com/...
- srpeck 12y agoThis might help you understand it: http://code.kx.com/wiki/Cookbook/InterfacingWithC http://code.kx.com/wiki/Cookbook/InterfacingWithC Once you understand the underlying data structure, a lot of k's design decisions make sense. I am using the k.h file from k4 as almost the sole guide to recreating a toy k5 (http://kparc.com/k.txt http://kparc.com/k.txt) interpreter in C.
- Avshalom 12y agoAll it's doing is opening a file, connecting to other clients and appending the messages between clients to the file. What you're missing is that K automatically sets up an environment with the necessary pieces in place. A server, the sockets, a type of reactive/eventloopy environment. Everything else is just function call/index/assignment and some mappy/foldy stuff In some ways its the same reason Mathematica seems so magic sometimes.