11 ms·
Ode to J
- 7thaccount 7y agoI've done a little J in the past and have been playing with APL a lot lately. It is definitely a different way to think. My programs are taking a long time to write as I train my brain to think in this domain, but the code is very concise and generally not hard to follow afterwards. Everything is so interactive it's a joy. In particular, I enjoy how the poster broke the code down as I can only understand some of C code. It's cool to think I could create my own little J interpreter this quick.
- jim-jim-jim 7y agoIt seems like this blurb of code has appeared on HN quite a bit lately, and it's cool to see a breakdown of it. The next time I write C, I'm tempted to do it Whitney style like this. There really is something to be said about seeing everything at once.
- empath75 7y agoAbout halfway through reading that, I was thinking -- it would be fun to try and write this in Rust, and then i got to the bottom, and he had aleady done it. Much easier to read in rust.
- RodgerTheGreat 7y agoThe C version is 41 lines, and 1749 bytes. The Rust version is 295 lines, and 8289 bytes. The point of the Incunabulum is not really what it does (it's a REPL with a handful of half-implemented J verbs and no error checking), but the style it demonstrates. I think that rewriting a similar program in another language without even attempting to reproduce the style is rather missing the point. If you're interested in Rust, why not figure out what a semantically-compressed style looks like for it?
- tomp 7y agoHow much of that is due to the C preprocessor though? In theory, you could run the same preprocessor for Rust code (if you figure out some sensible macro shortcuts).
- clarry 7y agoNot much at all. It's a completely apples to oranges comparison, with the rust version having some error handling, support for longer identifiers, tests, relatively idiomatic style with line breaks and indentation where you expect it, variables and type names longer than one or two characters, etcetra. You could compress it massively before going for the preprocessor. In fact, a few of the preprocessor macros just make the C version longer (when measured in lines) than it would be without those macros if lines were allowed to be as long as in the rust version (<70 chars vs 100 chars). The printf macro (used only three times) actually makes the C code longer both in bytes and lines (or equal length in lines if you retain the 70 column limit). There's really only one macro (DO) that expands enough to save lines, but just barely. The shorthand for return saves quite a few bytes (but not so many lines) given that it's used everywhere.
- empath75 7y agoBut why though. It’s a terrible way to write code.
- dang 7y agoPlease don't post shallow dismissals: https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html This topic in particular tends to get flooded with a few shallow clichés as soon as it arises. This is to be avoided, because they have the effect of turning a thread into the same generic argument as the last N times it came up, which is tedious. Better to respond to the specifics of a topic. Alternatively, if you want to learn, ask curious and neutral questions. But please don't pick one of the low-hanging bombs and toss it; the results are predictably boring. Not picking on you personally—we all have this reflex, and it's actually fine in in-person conversation, but bad for internet forums.
- antigirl 7y agoWe did J in university. I hated it. We also did assembly and I much ordered that instead
- 7thaccount 7y agoI think I would also hate J, APL, Prolog, Haskell, Forth, or Lisp in University. The problem is that those languages require a completely different shift in software from the common imperative/OO style to something more declarative or in Lisp's case, just a bit different. In a typical university course, there isn't enough time to focus on learning those things and you stay so shallow with the material that the language seems useless. It seems like 1/2 of the Prolog subreddit is about homework assignments on things like list reversal. If you can already do that in Python/Java with a single method call, Prolog just seems like a really bizarre and inefficient method. Now once you take another step and see how it can figure out how to solve Sudoku without explicit instructions...that is cool. It's really the same thing with electrical engineering when the professor is trying to teach us microprocessors and assembly at the same time and it all seems like a waste when C is much easier. The assembly method probably teaches better, but I need more than just a few weeks to synthesize that information. Especially when you're getting slammed by other hard classes like differential equations at the same time. So perhaps you do hate J, then again, maybe you just need to learn it on your own schedule and not to answer arbitrary test questions about something you can already do in Java.
- aidenn0 7y agoI don't know how it is in J, but having fielded Lisp questions from students learning Lisp (usually 1/3 or 1/2 of a semester class on "Programming Languages" or "Functional Programming"), the assignments tend towards terrible questions, to the point where I don't think I could come up with questions that were better suited to making someone hate the language. People come out of a class that used Common Lisp thinking that Common Lisp lacks any looping construct (it has several). It looks like the professor took a SICP (which uses scheme, not common lisp) based class as an undergrad, never looked at any language in the lisp family again, and then 20 years later made up a syllabus using the half-remembered information using Common Lisp because they remember SICP had something to do with Lisp and taught it without actually trying to do any of the exercises.
- eggy 7y agoI love J, APL and the array languages in general. I am always looking for exercises to practice my J skills. I found this article by Tyler Limkemann called "Modeling the COVID-19 Outbreak with J"[1]. I also follow tangentstorm and took some of this J-talks gui code[2], and mashed it up with Tyler's as a J learning exercise. I just put this up on my github. [3] I highly recommend tangentstorm's YouTube videos too![4] The concise J code, and the way it makes me approach a problem brings me joy! Given all of my coding projects in J fit on one page, I can revisit them later, and get right back in without worrying about having too many comments or documents to understand my code. I like refining the code over time for fun and learning even after it works, or somebody much smarter than myself shows me a different way to look at it. [1] https://datakinds.github.io/2020/03/15/modeling-the-coronavirus-outbreak-with-j https://datakinds.github.io/2020/03/15/modeling-the-coronavi... [2] https://github.com/tangentstorm/j-talks https://github.com/tangentstorm/j-talks [3] https://github.com/rpherman/JLang/tree/master/COVID-19 https://github.com/rpherman/JLang/tree/master/COVID-19 [4] https://www.youtube.com/user/tangentstorm https://www.youtube.com/user/tangentstorm
- kinleyd 7y agoRecently I've been dabbling in q and kdb+. Would there be any benefit to dabbling in J instead?
- 7thaccount 7y agoSame concepts for the most part. I think K is a bit easier to understand and if you're lucky enough to work with a kdb+ system, I'd focus on k & q to be honest.
- kinleyd 7y agoThanks. I downloaded the 64bit "on demand" version of q, kdb+ and have been toying around.
- anonu 7y agoShakti seems to be the new flavor
- 7thaccount 7y agoThe creator of kdb+, the k language, and the q-sql DSL sold his share of Kxsystems (company that makes kdb+) to start a new venture called "Shakti" with a new version of his "k" language. Kdb+ is still around and developing, so I'd say they're forked at this point.
- kinleyd 7y agoI've taken a look at that as well. It seems to be heavily in development at the moment, though I'm keeping my eye on it.
- smabie 7y agoWhen a q like language is implemented for Shakti, I'll definitely switch. I personally find q a lot more comprehensible than k.
- kick 7y agoJ is probably the best language in the world to craft native GUIs (something k could never do very well, and now doesn't do at all). It also has a wonderful amount of books on it, most of them Creative Commons-licensed now (as all/most of Iverson's books are, I believe). It's significantly easier to learn for someone new to array languages yet who doesn't have the sort of hands-on training you can get with APL & k; you could go into the woods with nothing but the J interpreter tarball for a week and come out pretty having internalized the language, and it's even easier with some of the other books available. It's, of course, free software.
- scythe 7y agoSince this flurry of APL/J/K/A+/Q articles started, I've been wondering about the relationship between APLs and literate programming. They say that APL was developed to mimic mathematical notation, and I see the resemblance. But real works of mathematics consist of one or two lines of expressions separated by sentences and paragraphs explaining the goal of those expressions. So far in K "advertising" I only see the expressions and not the explanations. The explanation is there, but it's not a comment in the codebase; it's in a blog post that explains it. The code has many lines of expressions on top of each other, and nobody writes math that way. I feel like I'm missing something, basically. It's clear how to write K, but what are the best practices for making a large codebase readable? I shouldn't have to invent that myself, right?
- 7thaccount 7y agoAssuming you write your code for a certain domain, I think most of your colleagues could follow along after learning a little k. I wrote a toy simulator for the domain I work in using something like 6 lines of APL code and sent it to someone good with APL. They replied back to the email pretty quickly with some tips on how to improve it and could generally get the gist of what I was trying to do while knowing nothing of the problem domain. It was really an interesting experience. I think with zero doc, a colleague of mine would immediately understand what I was doing after going through a short APL tutorial. It isn't magic, but not nearly as cryptic as people make it out to be. Edit: people do comment their APL code. I think a million loc project would be pretty difficult in APL, but the beauty is that you should be able to do some pretty powerful work in a few pages I would imagine.
- eggy 7y agoI think you're right. Aaron Hsu's work in APL is inspiring and awesome. The source code to his data parallel compiler hosted on the GPU is 17 lines of APL! Read the abstract to his paper here: https://scholarworks.iu.edu/dspace/handle/2022/24749 https://scholarworks.iu.edu/dspace/handle/2022/24749
- deleted 7y ago[deleted]
- haolez 7y agoI like to play with array languages in my free time. I have a doubt regarding readability: For me, it's easier to read APL with the symbols than to read J/K. Does it get better if you work a lot with J/K?
- kick 7y agoAbsolutely yes for J and for k, but I wouldn't necessarily deem it necessary to use one or the other "a lot" just to get an intuitive grasp readability-wise.
- ThomasBHickey 7y agoLong ago I was doing some analysis on a Sigma 5/CP-V machine. One of the few high level languages available was Xerox APL. For those without an APL keyboard they provided ASCII replacements (I don't rember what they were, but looked like $NDX, $ASG). After learning APL with the awkward mnemonics I was never able to read it with the special characters! J does a much better standard-character APL.
- dunefox 7y agoWhy don't these languages support latex-style symbols in the way Julia does? Even in the Repl: type \alpha<TAB> and you get α. Even Σᵢ is possible. This makes symbols accessible without a keyboard and the code is quite close to mathematical notation.
- clarry 7y agoLatex style is way too verbose, I would not want to type like that. And I think the input method should be a problem for your editor/OS to handle, not the language. In any case, symbols were abandoned back in the day when everyone was still using 8-bit encodings. [1][2] Nowadays you have e.g. Dyalog APL which does use APL symbols and you have short ascii-based sequences (easier to type than latex) for inputting them: `u`y gives you ↓↑ (You can try this online at https://tryapl.org/ https://tryapl.org/) [1] Remembering Ken Iverson (http://keiapl.org/rhui/ http://keiapl.org/rhui/) > For the first few months, the special APL characters and the ASCII spelling co-existed in the system. It was Ken who first suggested that I should kill off the special APL characters. I myself resisted for a few weeks longer, until the situation became too confusing, for reasons described in J for the APL Programmer. [2] J for the APL Programmer (https://www.jsoftware.com/papers/j4apl.htm https://www.jsoftware.com/papers/j4apl.htm) > J uses the 7-bit ASCII alphabet. It also makes non-essential use of the box-drawing characters in the 8-bit ASCII alphabet for display. Using ASCII avoids the many problems associated with using APL symbols. It allows J to be used on a variety of machines without special hardware or software, and permits easy communication between J and other systems.
- klipt 7y agoAre there any open source compilers for J (and similar languages)?
- arabelladonna 7y agoJ. J is open-source. Also, for K: https://github.com/kevinlawler/kona https://github.com/kevinlawler/kona https://bitbucket.org/ngn/k/src/master/ https://bitbucket.org/ngn/k/src/master/
- moonchild 7y agoJ[1], ngn/k[2], oK[3], kona[4], and GNU APL[5] are the big oss interpreters. The main closed-source ones are Dyalog APL, Kx k, and Shakti k (which are still free for non-commercial use). 1: https://github.com/jsoftware/ https://github.com/jsoftware/ 2: https://bitbucket.org/ngn/k/src/master/ https://bitbucket.org/ngn/k/src/master/ 3: https://github.com/JohnEarnest/ok https://github.com/JohnEarnest/ok 4: https://github.com/kevinlawler/kona https://github.com/kevinlawler/kona 5: https://www.gnu.org/software/apl/ https://www.gnu.org/software/apl/
- vchak1 7y agoI'd also suggest looking at Pike's ivy as a simple example of an apl like system in go. https://github.com/robpike/ivy https://github.com/robpike/ivy
- moonchild 7y agoI would argue that, being stylistically inconsistent with the way most apl is implemented, that's actually not such a great resource. I found 'whitney c' surprisingly approachable, with just a little patience; despite no prior experience with it, I was able to make an apl-ish interpreter in 7kb and a few hours. If you're curious about implementation details, that might be a more worthwhile exercise. Be sure to check out the parsing section[1] of the J dictionary. 1: https://www.jsoftware.com/help/dictionary/dicte.htm https://www.jsoftware.com/help/dictionary/dicte.htm
- okareaman 7y agoAm I weird because I have no interest in terseness that you have to tease apart. It feels counter-productive. The programming golf (Code golf) contests and obfuscated C contests just seem like a waste of time to me. The whole point of programming languages is to allow humans to produce machine code using a more natural language closer human language that we are all already fluent in.
- smabie 7y agoThat's not the point of programming languages. The point of programming languages is to be more productive, it has nothing to do with natural language. In fact, natural languages are a poor fit for programming languages (see Cobol).
- ben509 7y agoCounterpoint: SQL is incredibly successful and its syntax uses natural language structures quite heavily. The problem Cobol has is that uses some natural structures, but then becomes very verbose because it only uses a few, and they often can't be combined, so it is very repetitive. It's not natural to say, I will buy eggs at the grocery store. And I will buy ham at the grocery store. And I will buy cheese at the grocery store. > The point of programming languages is to be more productive... But you should be productive not only in initially writing the code, but also in reading it later when you do maintenance. And what you see with most languages is extensive commentary to help future authors understand what's going on, and that's partly because terse languages tend to be cryptic.
- strbean 7y agoI get anonymously downvoted to hell whenever I post this, but SQL is an absolutely awful language. Fundamental aspects like consistent representation of the same semantics in different places are missing in the very core of the language. The ubiquity of SQL generators is a sign of how little most people want to work with it, and a major reason we haven't jumped ship.
- ken 7y ago
- smabie 7y agoIt seems like array languages are really picking up steam on HN lately; It's a positive development. Every developer should learn at least one array language. They sound absolutely crazy before you start, but then you start writing some code and you realize there's a lot of wisdom in the coding style. It's not something you can appreciate only from afar, though.
- Zhyl 7y agoSimilar logic as to why array languages are good can also be applied to why Linear Algebra is a good thing to learn, especially when you're in a calculation-heavy field. A lot of stuff can be abstracted away in a robust way that makes repetition unnecessary whilst also being able to reason about your equations/code.
- dunefox 7y agoWhich of these languages has good materials and is suitable as an introductory language?
- smabie 7y agoMy first one, which I learned for Advent of Code 2019 was kdb+/q. There's a good book for it too: https://code.kx.com/q4m3/ https://code.kx.com/q4m3/
- warthogschool 7y agoDoes anyone know what the dyadic verb 'find' (dyadic '~') is supposed to do? Its implementation seems to be empty in the little interpreter.
- chrispsn 7y agoIt's probably like k's find (`?`), which gets the indices of the found elements, or else the length of the list (or null, depending on the dialect) if not found: "abc"?"caz" 2 0 3
- anthk 7y agogets() wont compile today.