Live data from Hacker News

Another Programming Idiom You've Never Heard Of

prog21.dadgum.com

31–35 of 35 posts

Re: Another Programming Idiom You've Never Heard Of

#31

Earlier quoted context omitted.

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/cloj…

Yes, that works.

The deeper benefits come from having a language design where almost everything expects to work with arrays and array indices, so that idiom can be combined with many other operators and combinators.

Quick example in K, J's cousin, which I know far better: consider "{[d;p]d@&p d}". That's a 3-argument lambda which takes a data set (d) and a predicate function (p), applies the predicate function to the data set's values (p d) returning a boolean vector of the predicate's results, filters the result set to just the indices of the true/1 values (&), then indexes the data set by those indexes. The predicate function can be applied to the data set in parallel, and it can be slices by the indices in parallel. D can be a small data set in memory, or a terabyte of memory-mapped data on disk; it doesn't matter. (Really, it could just be "{x@&y x}" or "{x[&y[x]]}", but I named the parameters.) 'where' ("&") is just one of many operators that combines with array slicing like this.

In pseudo-Lisp it might look something like "(lambda (d p) (pmap d (where (pmap f d))))". 'where' is a function that converts e.g. [1 2 3 4 5] to [0 1 1 2 2 2 3 3 3 3 4 4 4 4 4], and [0 1 0 1 0] to [1 3]. This works for both boolean results and reshaping data sets, but the underlying implementation is the same.

Re: Another Programming Idiom You've Never Heard Of

#32
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?

No, but it does it lazily.

pmap mentioned above probably has too much overhead for what you are thinking as it is intended for more coarse distribution of workload.

Re: Another Programming Idiom You've Never Heard Of

#33
post #10

Earlier quoted context omitted.

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

Re: Another Programming Idiom You've Never Heard Of

#34
post #32

Earlier quoted context omitted.

Does it do the slicing in parallel?

No, but it does it lazily. pmap mentioned above probably has too much overhead for what you are thinking as it is intended for more coarse distribution of workload.

Okay. I think I've figured out how to integrate clojure-style lazy streams with an array-oriented language, but it's a work in progress, and currently shelved until I get my Strage Loop presentation done.

Re: Another Programming Idiom You've Never Heard Of

#35
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]).

An array slice can also be used as an lvalue in an assignment:

  @a = (10, 5, 9, 6, 17, 1);
  @a[3,5,1] = qw(yes no maybe);
  print "@a";

  >> "10 maybe 9 yes 17 no"
In fact, because the slice can be both an lvalue and an rvalue, this is an easy way to transpose elements in-place.

  @a = (10, 5, 9, 6, 17, 1);
  @a[0,4] = @a[4,0];
  print "@a";

  >> "17, 5, 9, 6, 10, 1";
Post reply on HN