Hacker News new | ask | show | jobs
by peteretep 5123 days ago
Oh look, you just invented array slices!

https://en.wikipedia.org/wiki/Array_slicing

4 comments

J has it via APL, which is heavily based on linear algebra. (Not surprisingly, R, MATLAB, and other matrix-math-centric languages also have it.)

It's one thing to say, "yeah, I can kinda do this in [language]", it's quite another when your whole language is based on it, as the APL family are. If you can do array slicing in parallel, you can work with permutation vectors rather than sorts (as he notes), and guess what? The same idiom works for relational joins, among many other things. (The killer app for Q, kdb+, is an in-memory, column oriented relational database. NoSQL, before it was cool.)

This pattern is incredibly useful in Q, and one of the things I miss about using it daily. Of course, now I mainly use java, so the pendulum has truly swung.
Yeah, ok, array slicing, woohoo. But what's interesting here is that this doesn't come from any special/privileged syntax or from special handling by the { operator. The { operator just expects a scalar and a list and accesses an element of the list according to the scalar. This slicing capability simply falls out of the automatic lifting J does when you pass an array of higher rank than the operator expects, the same rule that gives you (2 5 3)+7 = (9 12 10).
"The concept of slicing was surely known even before the invention of compilers. Slicing as a language feature probably started with FORTRAN (1957)"

Maybe slightly late for that.

Guy who doesn't understand general statements and statistics: "Your title is wrong, _I have_ heard of that idiom".

A different guy with the same deficiency: "Wrong, it's a common idiom in (list of obscure languages)".

Plus: array slicing, in 95% of the languages mentioned (and 99% of common languages) is NOT what he talks about. It just returns a sub-array and it only takes an uper/lower bound.

Do you really think of Perl as an obscure language?
Are 95% and 99% difficult to parse?

Plus, does "I've seen it in Perl" nullify the "an idiom you haven't seen" for the rest of millions of programmers that don't use Perl, or J, or ...?