12 ms·
K – A simple, fast vector programming language
- 7thaccount 4y agoArray languages are very cool. I'm more of an APL guy myself though.
- magicalhippo 4y agoDo they all read like noise[1]? [1]: https://codeberg.org/ngn/k/src/branch/master/a19/20.k https://codeberg.org/ngn/k/src/branch/master/a19/20.k
- t-3 4y agoIf you want to understand the thinking, read Iverson's Notation as a Tool of Thought. These languages are optimized for the user rather than the learner, so the curve is steep but rewarding.
- anthk 4y agoEver used dc(1) in Unix?
- magicalhippo 4y agoNever heard of it till now. Interesting, reminded me of my mom's HP-15C calculator I played with as a kid.
- 7thaccount 4y agoSwiss micros sells high quality remakes of many of the HP calculators, with a couple of high end new creations of their own. Works of art and engineering for sure. Not as good as my top end TI for just getting stuff done though. My dad has a 15c as well. Fond memories there.
- jasomill 4y agoSpeaking of works of art and engineering, the three HP Voyager series calculators on my desk (12C, 15C, 16C) still work as well as they did several decades ago when they were new. As does the other 15C that I regularly carry around and occasionally drop.
- 7thaccount 4y agoMy father only had a problem with the battery leaking in a bigger HP calculator that had these expansion modules. His 15c is also still working since the 80s.
- rpz 4y agoReads like music to me
- magicalhippo 4y agoAt least music sheets have a clear structure, I don't see any in the examples like the one I linked to. Of course it could be that the examples I've seen just happen to be written like IOCCC entries.
- yjftsjthsd-h 4y agoYou can't see structure in a language that you don't know? I mean, sure, one possibility is that there just isn't a structure, but that's really not the obvious option here.
- magicalhippo 4y agoI can see structure in most new programming languages I'm exposed to. I might not immediately understand what the code does, but usually there's some sense of rhyme and reason. Though most languages are not as terse as this.
- tlack 4y agoThese languages are from a different branch of the computer science language tree. The verbs are different. The same verbs usually work similarly for all data types. Each of those symbols (verbs) are jam-packed with meaning, and most looping is implicit or very natural, and overall code constructions are very terse. It's like reading a nice short poem rather than a 700 page book. It can go too far, of course; there is some middle ground to be found. If you have time, reading into the language a little more deeply may be illuminating, because it changes the way you think about the very nature of the task at hand.
- lokedhs 4y agoIn some of the example programs written in KAP (my APL derivative), I tried to write it in a style that makes people unfamiliar with the array style more comfortable. This code could of course have been written in a style similar to some of the more extreme examples, and they would have been significantly shorter in that case. https://github.com/lokedhs/array/blob/master/demo/advent-of-code-2021/ https://github.com/lokedhs/array/blob/master/demo/advent-of-...
- razetime 4y agoI hope you have taken some time to understand K's philosophy and goals before making this comment. A tutorial exists here: https://github.com/razetime/ngn-k-tutorial https://github.com/razetime/ngn-k-tutorial if you'd like to understand why this solution is good.
- magicalhippo 4y agoIt was just an honest question. I've just peeked at APL, J and K due to posts here on HN, and the sample code I've seen mostly reads as noise to me, so I was just curious if there were any array languages that would be approachable to me. The tutorial does help a fair bit, though glossing through it, it doesn't really explain why it has to be so exceptionally terse. I mean the matmul[1] sure is impressively short in the end, and with a background in computer graphics and simulations I can absolutely appreciate working on arrays rather than singular values. But I'm sure one could get the same computational result with a bit more approachable syntax. [1]: https://github.com/razetime/ngn-k-tutorial/blob/main/c-thinking-in-k.md https://github.com/razetime/ngn-k-tutorial/blob/main/c-think...
- gitonthescene 4y agoOr, or, and here me out here, you could learn the syntax in order to make a more informed assessment. Do also seek out geocar’s posts. They are well written and to my mind convincing. https://news.ycombinator.com/item?id=27393682 https://news.ycombinator.com/item?id=27393682
- magicalhippo 4y ago> you could learn the syntax in order to make a more informed assessment I'm fairly sure my assessment wouldn't change even if I did learn the syntax. It would still read like noise. The only difference would be that I could understand the noise. I've read and worked on enough super-terse code in other languages to feel confident in that. > Do also seek out geocar’s posts. They are well written and to my mind convincing. I guess it comes down to how ones brain is wired then. For example, I have the exact opposite reaction to multiple pages of code vs terse one-liners compared to geocar[1]. I don't have trouble remembering what range() does because it says so right on the tin, unlike for example + in k (addition or, surprise, transpose). [1]: https://news.ycombinator.com/item?id=27223086 https://news.ycombinator.com/item?id=27223086
- flocke 4y ago[dead]
- andreypopp 4y agoIt's very cool to see ngn/k on the HN front page. After the recent AoC which I've attempted to solve with BQN[1] (another array language) I've got interested in array languages and now exploring K too. K seems like a nice blend between an array language and a lisp: list is a primary data structure in K, no multidimensional data. K is good at symbolic workloads as well — visit http://nsl.com http://nsl.com for some examples. Also, make sure to check out the C code style ngn/k is written with :-) another K implementation Kona (uses similar C code style) has a wiki page with explanation[2]. [1]: https://mlochbaum.github.io/BQN/index.html https://mlochbaum.github.io/BQN/index.html [2]: https://github.com/kevinlawler/kona/wiki/Coding-Guidelines https://github.com/kevinlawler/kona/wiki/Coding-Guidelines
- mdaniel 4y ago> I've got interested in array languages and now exploring K too. https://www.arraycast.com/ https://www.arraycast.com/ announced last year (https://news.ycombinator.com/item?id=27209093 https://news.ycombinator.com/item?id=27209093) may interest a similar audience as this announcement
- mlochbaum 4y agoSince other array languages are being brought up already, here are two things K does that you won't find anywhere else in the APL family as far as I know: - Dicts (same as hashmaps, associative arrays, etc.) are a native datatype, with `ab`cd!0 1 indicating a dict mapping symbols `ab and `cd to 0 and 1, for example. In K4 and later (ngn/k implements K6), array primitives work on them to do all sorts of fancy things. For example prefix &, "where", returns the indices of any 1s in an array. If given a dict, it gives you the keys corresponding to 1s. - Partial application. If you have a function f of two arguments, f[;x] binds the second argument x to f, but leaves the first one unbound. So f[;x][y] is f[x;y]. But that's not nearly all: arrays, dicts, and functions are unified, so if fs is a list of one-argument functions, then fs[;x] applies each function to x. This is because fs[;x][i] is fs[i;x] or fs[i][x]! ngn/k is the best implementation to use now in my opinion. It's short on documentation, but the K Wiki is a new and very nice resource for K. Maybe start with the list of all primitives at https://k.miraheze.org/wiki/Primitives https://k.miraheze.org/wiki/Primitives .
- moonchild 4y ago> ngn/k is the best implementation to use now in my opinion why?
- memorable 4y agoThe biggest win for me to use ngn/k is the built-in documentation. K is primarily taught through refcards, and most K refcards that you find in implementations are quite compact, like this: https://kparc.com/k.txt https://kparc.com/k.txt While refcards like this are perfectly fine, they can confuse beginners at specific parts in the text. This is where ngn/k shines. It has a robust documentation system, containing several help commands for multiple subjects (types, verbs, adverbs, etc.). They also contain examples, have clear separation between subjects, and are way less ambiguous.
- bakul 4y ago> So f[;x][y] is f[x;y]. f[y;x]
- WaxedChewbacca 4y ago
- jiggawatts 4y agohttps://codeberg.org/ngn/k/src/branch/master/v.c https://codeberg.org/ngn/k/src/branch/master/v.c Where does this madness come from, and why does it infect proponents of array-based languages to the point where they start writing C code in this style!?
- mlochbaum 4y agoWell, for what it's worth, the strings with curly braces are K code.
- RodgerTheGreat 4y agoIt's just writing C in the style of the target language. If you can read K, reading that C isn't especially hard. Some writings on this style: https://code.jsoftware.com/wiki/Essays/Incunabulum https://code.jsoftware.com/wiki/Essays/Incunabulum https://github.com/kevinlawler/kona/wiki/Coding-Guidelines https://github.com/kevinlawler/kona/wiki/Coding-Guidelines
- thayne 4y agoThat does not make me want to learn k.
- memorable 4y agoLearning how to write K is way less painful than learning how to write C in the style of K.
- avmich 4y agoYou may want to learn J if you read "J for C programmers" - https://www.jsoftware.com/docs/help807/jforc/contents.htm https://www.jsoftware.com/docs/help807/jforc/contents.htm . A great book.
- regularhuman 4y agoI've been fascinated by this style ever since I saw the "J Incunabulum." Unfortunately, it seems to come from idol worship. Arthur Whitney, the author of the K programming language(s) writes in this style, and so those who seek to emulate his achievements sometimes do as well. Some of my complaints: * true adherence to the source material requires one to dispense entirely with all but the tersest comments * it over-aggressively tunes away whitespace (if you code like this, it's quite beautiful when you align comparable values on the same column between adjacent rows) * many of the idioms rely on undefined behavior, which makes it very frustrating to maintain, port, and extend--to a lesser degree, it makes the code hard to follow, but usually (the most frustrating part of all!) is that the intent is _clear_, it's just that it doesn't work well with the C standard * it emphasizes the placement of as many statements as possible on a single line, which is jarring when those statements don't flow together; on the other hand, the style is quite nice when you allow yourself to place complete and related concepts together, since it's clear that you're "doing something else" on subsequent lines I said this is "unfortunate" not because I have any problem with Arthur, but because I actually wish this was part of a richer tradition of C (which I've never been able to confirm and thus came to my conclusion about idol worship). Although I happily abide by them in my professional life, I am thoroughly frustrated with coding standards that dictate variable names be complete sentence and code limited to 80 lines. I would love to adopt a rigorous style _based on_ Arthur's, although I will concede that this will probably never happen because the aesthetic of this practice conflicts so completely with the contemporary zeitgeist. Anyway--nobody has to like it, but some people really actually do like it and accomplish work with it.
- technion 4y agoThese style of languages often come up here. Could a proponent give some examples of practical applications written in these array languages? I feel like that's missing in these discussions. People say "finance", but let's say I have a finance team that have never heard of it - why might they be interested?
- deleted 4y ago[deleted]
- flocke 4y agohttps://www.dyalog.com/case-studies/financial.htm https://www.dyalog.com/case-studies/financial.htm An issue in writing a thoroughly helpful response, is that successful APL and K implementations are usually applied in a largely corporate and proprietary context. I lack a list of applications to link which you could look at. However I'd still say APL/J/K applications require vastly less resource, and initial investment than most tools whilst providing on-par or greater computational power.
- snicker7 4y agoThe main product is is “kdb+”, a simple, fast, proprietary, embedded, time series database. Especially good for tick data. I work in finance, and literally all of my employers used it. Not much secret sauce, just simple algorithms implemented very well. No real open source competitor. Well, maybe DuckDB if they support as-of joins.
- geocar 4y ago> why might they be interested? Because you get done faster. I have no doubt any experienced C programmer could beat a k solution in run-time performance, but the k programmer will always be done faster. > proponent give some examples of practical applications written in these array languages? Databases are the most common: Building an API library to quickly answer questions about data is probably about the easiest thing to do in an array language, but isn't almost anything a database with a definition like that? E-commerce and storefronts, CRMs, flight trackers, and so on. In this way, I often suggest to people that the closest analogue is maybe something like SQL stored-procedures, but with a less-shit language. But this isn't exactly right. Games are an interesting problem that aren't databases (for example), and a lot of games have vector-problems that have nice solutions in k. So let's play hangman. If you don't know this game, we will only be playing with lowercase letters: A:`c$"a"+!26; // A gets the characters cast [from] "a" + til 26 First, let's agree on a dictionary to use: W:0:"/usr/share/dict/words" and I will only choose a word with at least 7 letters in it and make it lowercase so you have a chance: w:_*1?W@&7<#'W; // w gets lowercase [the] first [of] 1 random-take [from the] Words [that/at] where 7< count each Words Now when you make a guess: G:();g:{G::G,x} I'll now tell you what you know, but you're only allowed N guesses. N:5 k:{@[w;&|/w=/:A^N#G;:;"."]}; // k gets a lambda, [that] amends w where the max-over [when the] word equal [to] each of-the Alphabet without [the first] N [characters] taken from the Guess That's it. You keep guessing until you guess the word, or you run out of guesses. g"e";k[] "...e..e..'." g"s";k[] "..se..e..'s" It's a silly game. I wrote comments above (with // characters so they should be easy to spot), and I now want you to take a look at them. The comment is what I "read" when I am reading the k stanza. The square-brackets cover words (prepositions mostly) that help make it sound more like English, but look at the other words too: x=/:y is x equals each-of y. That's all it is. I am reading it and writing it that quickly. Yes it takes longer to write and read the English, and of course, any non-array language will be a lot more code to read and write as well. That's what I think the value is in an array language: It's just so clear once you know how to read and write it, that you are simply faster. And being clear means the computer does what you expect (usually on the first try!) and so the programmer gets on to the next problem sooner. I've written ad servers and presentation tools in array languages; I don't think there's anything I can't write (after all, almost everything is a database or a game if you squint hard enough), but there's a lot I wouldn't: I did an MP4 decoder in JavaScript once (to autoplay ads on iphones back when they blocked video playback in iframes) but I would never say JavaScript is the right language to write MP4 decoders. Array languages are tools in your toolbox -- like JavaScript or any other -- and you should try to put as many tools in your toolbox so that you have the best chance of using the right tool for the job.
- ur-whale 4y agoWhat does debugging code written in this style look like? Has anyone run code like this under gdb?
- gitonthescene 4y agoI think this style of coding is decidedly less amenable to using a debugger on. With so much on a single line stepping becomes unclear. Heavy use of macros fights against the use of a debugger as well. I think proponents would argue that the extreme rigidity of the style means the code is more likely to fit expectations but you can decide how convincing that is. As for what people actually do, the catch phrase seems to be “use printfs”.
- ur-whale 4y agoI wonder what a diff looks like on code like this? Is it even a good idea to use a diff on something that dense rather than just re-sending the entire code?
- gitonthescene 4y agoI think a diff is just as valuable with this code. Of course understanding a diff also requires understanding the code but if you’re interested the repository is linked at the root of this thread so you could poke around.
- icsa 4y agoYes. The diffs tend to be quite small & obvious.
- LfLxfxxLxfxx 4y agoBy default git diff works on entire lines, but try this: git diff --word-diff=color --word-diff-regex=. It highlights only the parts that have actually been changed/added/deleted.
- jhbadger 4y agoWhat makes this implementation different from kona? https://github.com/kevinlawler/kona https://github.com/kevinlawler/kona
- gitonthescene 4y agoThe K family of languages hew very closely to versions written by Arthur Whitney who famously rewrites the entire codebase from scratch with each major revision. Kona is largely modeled on K2 (the second major revision) and ngn/k is modeled on K6. K6 introduced a few more types, more flexible dictionaries and added/changed some primitives from earlier versions but the overall structure of the language is very similar. Someone here has to post ngn’s survey of K languages: https://ngn.codeberg.page/k.html https://ngn.codeberg.page/k.html oK is also modeled on K6 and its manual comes highly recommended.
- skruger 4y agoIf anyone is looking for some additional docs for ngn/k, I’ve written https://xpqz.github.io/kbook https://xpqz.github.io/kbook
- pxeger1 4y agongn has a comparison page for various implementations of K: https://ngn.codeberg.page/k.html https://ngn.codeberg.page/k.html Note that ktye/i is actually public domain, not proprietary as that page says.
- ithkuil 4y agoI also found https://t3x.org/klong https://t3x.org/klong very interesting
- wklm 4y agoCool to see another open source flavor of q, but is there an open source alternative to kdb+?
- gitonthescene 4y agoThe author of Kona has written kerf which I believe is intended for this same space. I’ve never used it though. https://github.com/kevinlawler/kerf1 https://github.com/kevinlawler/kerf1
- moonchild 4y agokerf is not open source
- gitonthescene 4y agoYou’re right. I don’t see any license mentioned in the source at all.
- deleted 4y ago[deleted]
- ur-whale 4y agoThis particular way of coding to me exemplifies one very important problem with people who work in technical professions: their utter inability to "read the room". In this specific instance, I suspect the folks who enjoy writing (and assuming they actually can - reading) code like this, completely assume that everyone has a brain with the same kind of weird quirks theirs has, namely the ability to recall almost instantly infernally long minutiae of details and thereby decipher what most people perceive as pure noise. Or, more generally speaking, they assume that their potential audience essentially are as smart as they are. News at 11: this is almost never the case. And in the case of: this type of language, APL, LISP, functional programming, quantum physics, etc ... your favorite topic/programming language/hobby ends up bound to be confined to a tiny bubble of folks that are like you instead of gaining popularity.
- avmich 4y agoIt's interesting that in the days APL was rather popular with non-programmers.
- gitonthescene 4y agoIf you haven’t yet, you should check out posts by geocar referred to elsewhere in these comments. Odds are you are selling yourself short if you think the difference is smarts.
- kragen 4y agoIf you don't understand the code, probably you don't know what kinds of mental processes are needed to understand it. Maybe "recalling almost instantly infernally long minutiae of details" isn't actually among them. Maybe great intelligence isn't, either. There's a whole genre on YouTube of videos of people doing day-to-day work with skills that seem utterly superhuman, with names like "Fast Workers #230" and "People are AWESOME 2020", like https://youtu.be/YmkPoiEcgJM https://youtu.be/YmkPoiEcgJM. Actually, though, if you practice almost any skill every day for an hour or more, working diligently to improve, after 20–40 years your ability at it will seem superhuman, even if you're average intelligence or slightly below. (Unless it's a professional sport, in which case people will be mentally comparing you to Lionel Messi or Tiger Woods, and you won't get that good. Or a popular hobby like knitting.) It definitely does not require 20–40 years of effort to understand quantum physics or code written in K. — ⁂ — Now, suppose that it were true that reading poetry written in Volapuk (or whatever) were a niche interest because of some essential feature of Volapuk that inherently limits it to a small group, rather than because learning Volapuk takes time that most people haven't put in, for the standard diffusion-of-innovations reasons. Would that be bad? Would it mean that people writing poetry in Volapuk had an "utter inability to 'read the room'"? I don't think so—to attribute their choice to do so to such a cognitive handicap, you are presupposing that you understand their motivations, and that those motivations include maximizing the popularity of their creation. But it should be obvious upon a moment's thought that plenty of poetry, and code, is written with no thought for participating in such popularity contests; it's written for an audience of one, or two, or forty-eight. Perhaps the majority of poetry and code. So writing it in Volapuk, or K, may actually be a rational strategy if it appeals to your intended audience—not an error resulting from a mental disability, as you claim. More generally, quoting Chip Salzenberg: > It's usually a big mistake to assume that a number of experts on a given subject disagree with you because they don't grasp the true situation. (cf. http://canonical.org/~kragen/quotes.txt http://canonical.org/~kragen/quotes.txt)
- chrispsn 4y agoIn case you've jumped straight to the comments, here are some intro links. Many of these also appear in ngn/k's readme. First, direct links to ngn/k in the browser: - REPL: https://ngn.bitbucket.io/k/#r https://ngn.bitbucket.io/k/#r - Editor: https://ngn.bitbucket.io/k/ https://ngn.bitbucket.io/k/ Second, the best one-stop shop for an overview of k6's primitives (both ngn/k and oK are based on k6). https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.... The best intro examples are in John Earnest's k editor iKe - there's a dropdown at the bottom right. http://johnearnest.github.io/ok/ike/ike.html http://johnearnest.github.io/ok/ike/ike.html ngn/k's editor also has an 'examples' dropdown in its menu. For an illustration of k's strengths, Razetime's matmul refactoring is brilliant: https://github.com/razetime/ngn-k-tutorial/blob/main/c-thinking-in-k.md https://github.com/razetime/ngn-k-tutorial/blob/main/c-think... Finally, some nice examples of concise k: {$[2>#?x;x;,/o'x@&'~:\x<*1?x]} / Quicksort (+':,)\~!10 / Pascal's triangle {x x}{x{x[x]y}y} / Y-combinator Executable versions of above: - Quicksort (via Kelas's KCC guide): https://ngn.bitbucket.io/k/#eJwrLLaqVok2slO2r7CusNbRz1evcFBTr7OKqbDRMrSviK3l4iosVspIzcnJVyjPL8pJUQIAgGcPkw== https://ngn.bitbucket.io/k/#eJwrLLaqVok2slO2r7CusNbRz1evcFBT... - Pascal (via Richie/Attila): https://ngn.bitbucket.io/k/#eJzT0Fa30tGMqVM0NAAADYMCZg== https://ngn.bitbucket.io/k/#eJzT0Fa30tGMqVM0NAAADYMCZg== - Y-combinator (via ngn): https://ngn.bitbucket.io/k/#eJyLtKquUKiora6oroiuiK2srax14IqMro5Os66IVVCJrrCu0EpTqNA1tDaMrY015eLS0jfUVjQFABmvES8= https://ngn.bitbucket.io/k/#eJyLtKquUKiora6oroiuiK2srax14IqM...