Live data from Hacker News

Another Programming Idiom You've Never Heard Of

prog21.dadgum.com

1–10 of 35 posts

Re: Another Programming Idiom You've Never Heard Of

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

I use this kind of stuff in R all the time. Its actually wonderful how many typical data analytic tasks can be expressed in this format. Thats why, I suspect, both R and J have this operator.

Re: Another Programming Idiom You've Never Heard Of

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

I've used something like that:

   @state[1,2,3,5,6,7,9,10,11,13,14,15] =
     @state[5,10,15,9,14,3,13,2,7,1,6,11];
That's the row shift transformation in AES encryption.

Re: Another Programming Idiom You've Never Heard Of

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

Re: Another Programming Idiom You've Never Heard Of

#8

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

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

Re: Another Programming Idiom You've Never Heard Of

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

I've used perl's array slices as

  @array[@indices]
in the past. Can't remember why, but I have. It also works for hashes:

  %hash = (foo => "bar", one => "two", alpha => "beta");
  @keys = ('alpha', 'one');
  @hash{@keys} == ('beta', 'two');

Re: Another Programming Idiom You've Never Heard Of

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

Post reply on HN