Hacker News new | ask | show | jobs
by onlyup 5123 days ago
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)

2 comments

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

I don't quite understand the question. Are you asking if the latter example needs to modify the whole array, because you 'cut off' the first two elements?

I don't know J at all, but I'd assume that the result of the '{' is a _new_ array. All I've seen about J is very much rooted in mathematics and therefor I'd expect a functional/immutable language. Nothing would be 'deleted', no array changed/rearranged and a new array without the first two elements created/returned.

Note the fat 'no idea about J' disclaimer, of course.