Live data from Hacker News

Another Programming Idiom You've Never Heard Of

prog21.dadgum.com

11–20 of 35 posts

Re: Another Programming Idiom You've Never Heard Of

#11
I have heard of both this programming idioms: this and the original. In fact, both of these two programming idioms can be expressed in terms of in degree regular algebraic relations (e.g Common Lisp place forms). Indexes are places because you can retrieve the value at an index (nth coll i) and you can edit the value at the index (setf (nth coll i) val).

Slices of collections are themselves places, and they can be decomposed further into even finer places. In Common Lisp the subseq function describes such places: (setf (subseq coll start end) slice). Injective functions have an in degree regularity of one, which means that they are trivial place forms. As such, the original programming idiom can be described as a process that moves an object to a place, runs some functions, and then moves back to the starting place.

Re: Another Programming Idiom You've Never Heard Of

#12

Oh look, you just invented array slices! https://en.wikipedia.org/wiki/Array_slicing

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).

Re: Another Programming Idiom You've Never Heard Of

#13

Oh look, you just invented array slices! https://en.wikipedia.org/wiki/Array_slicing

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.)

Re: Another Programming Idiom You've Never Heard Of

#14
post #10

Oh look, you just invented array slices! https://en.wikipedia.org/wiki/Array_slicing

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?

Re: Another Programming Idiom You've Never Heard Of

#17
post #15

This seems cool but I have a question. Is this: > 6 5 4 3 2 1 0 { 10 5 9 6 20 17 1 Stored in an array? Because if it is, when I do this: > 2 3 4 5 6 { 10 5 9 6 20 17 1 Won't I have to rearrange the whole array? (deleting two of the index parts)

It doesn't rearrange the array, it constructs a new one. While I'm not 100% sure about the J implementation* , K and related languages usually modify the array in place if there's only a single reference to it (so that mutation is still referentially transparent), to avoid extra allocation & copying.

* It's now open-source at https://github.com/openj/core, but I have a hard time following their C style -- it's legal C, but written like J.

APLs have memory allocators that are designed with arrays in mind, making this less expensive than you might expect. See this post I wrote about the memory management for Kona (https://github.com/kevinlawler/kona/), an open-source implementation of K: https://groups.google.com/forum/#!topic/kona-dev/fs5GoSBtF3Y... .

Post reply on HN