4 ms·
Since 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: - Di
by mlochbaum 4y ago
Since 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]
- avmich 4y agoYes. And J has & to bind left or right argument to a dyad making it a monad - e.g. -&5 allows (-&5) 11 which evaluates to 6, while (5&-) 11 evaluates to _6 .
- vanderZwan 4y agoI was going to say "wouldn't BQN have dicts since it's trying to be a more accessible, modern, from-the-ground-up designed APL and dictionaries are pretty damn useful?" but then I noticed your username so I guess that answers that. So now I'm curious: why didn't you include some version these features in BQN? They seem pretty nice to have! EDIT: will you ever write a "let's learn BQN by figuring out how the self-hosted compiler is implemented!" article? I doubt I'll use BQN soon but I find it a fascinating, extremely interesting language, and I'm just super-curious how you implemented the self-hosting compiler.
- mlochbaum 4y agoLanguages with multidimensional arrays (APL, BQN, J, but not K) have trouble with dicts because an index into an array is a list of numbers, and an index into a dict is an arbitrary value. Many primitives, and especially selection, are designed around lists of numbers and don't transfer to dicts. In K, where the index into a list is one number, there's still a requirement that the keys in a dict all have the same level of nesting, but this isn't bad in practice. BQN will eventually have hashmaps implemented as in a more mainstream/conventional way, as objects. There's a model at https://github.com/mlochbaum/bqn-libs/blob/master/hashmap.bqn https://github.com/mlochbaum/bqn-libs/blob/master/hashmap.bq... . I don't think studying the compiler is a very good way to learn BQN, but I would like to write up parts of it (limited by time and motivation of course). I did some chat sessions on this sort of compilation during early development; see the links at the bottom of https://mlochbaum.github.io/BQN/implementation/ https://mlochbaum.github.io/BQN/implementation/ .