Live data from Hacker News

Another Programming Idiom You've Never Heard Of

prog21.dadgum.com

21–30 of 35 posts

Re: Another Programming Idiom You've Never Heard Of

#21
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)

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.

Re: Another Programming Idiom You've Never Heard Of

#22

How can it be an idiom if we haven't heard of it?

There are plenty of idioms in Hebrew that I've never heard of.

This is an idiom in a subculture you aren't a part of, in languages you do not speak. There are a lot of them. The author assumed that because J is not really a "mainstream language" that the people reading his blog would probably not be familiar with its idioms.

Re: Another Programming Idiom You've Never Heard Of

#24
post #7
post #2

Perl can do this as well: @a = (10, 5, 9, 6, 20, 17, 1); @slice = @a[0, 1, 3, 6]; Though I've never seen this used for anything other than simple sublists (like @a[0..3]).

So can MATLAB. a = [10 5 9 6 20 7]; slice = a([1 2 4 7]); I guess it's not called matrix laboratory for nothing.

plus, MATLAB has "find", which returns a list of all the positions of a matrix matching some pattern. The combination of the two makes things that are very ugly to express in other languages quite pretty.

Too bad it's wicked expensive, proprietary and slow. I quite enjoy the paradigm.

Re: Another Programming Idiom You've Never Heard Of

#26

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 wha…

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.

Re: Another Programming Idiom You've Never Heard Of

#27
post #16

Clojure can do this as well without requiring any special syntax or function because all sequence are funcallable: (map [1 2 3 4 5 6 7 8 9 10] [0 3 4]) => (1 4 5)

Does it do the slicing in parallel?

If not (I assume no), would you accept

(pmap [1 2 3 4 5 6 7 8 9 10] [0 3 4])

instead? pmap being defined as "Like map, except f is applied in parallel" [1]

That said, I'm too ignorant to understand the deeper benefits of the parallel approach. It seems it enables nifty things according to some other comments in this thread, yet I've been unable to understand quite why so far.

1: http://clojure.github.com/clojure/clojure.core-api.html#cloj...

Re: Another Programming Idiom You've Never Heard Of

#30
post #7

Earlier quoted context omitted.

So can MATLAB. a = [10 5 9 6 20 7]; slice = a([1 2 4 7]); I guess it's not called matrix laboratory for nothing.

plus, MATLAB has "find", which returns a list of all the positions of a matrix matching some pattern. The combination of the two makes things that are very ugly to express in other languages quite pretty. Too bad it's wicked expensive, proprietary and slow. I quite enjoy the paradigm.

The obscure matrix language IDL has this too via WHERE. It's really nice. This syntax is essential for image processing unless you want to write a bunch of unnecessary for loops.

img[WHERE(img LT 5)] = 0

I built a bunch of sparse image processing algorithms using index lists. Pass in the indices and the values at those locations. Everything else is assumed zero. Sort the index array and you can use binary search to look up neighbors. On my domain images which were less than 2% foreground it made a big difference.

Post reply on HN