Live data from Hacker News

Arrays of Arrays (2009)

ericlippert.com

21–30 of 30 posts

Re: Arrays of Arrays (2009)

#21
post #18
post #5

Earlier quoted context omitted.

Imo foo[x][y] is better for many reasons: - compiler can break it apart so foo[x] is evaluated once for many ys just like any other function - you can make types that are unaware of their rank eg vector doesn’t know if it’s a 1D vector of int or a 2D vector of vectors - “seamlessly” handles jagged arrays

They'd probably be lowered to exactly the same code, so the compiler argument isn't very good.

If the user has access to both indices, they can make partially hosting harder/impossible

Re: Arrays of Arrays (2009)

#22
post #5

Earlier quoted context omitted.

Imo foo[x][y] is better for many reasons: - compiler can break it apart so foo[x] is evaluated once for many ys just like any other function - you can make types that are unaware of their rank eg vector doesn’t know if it’s a 1D vector of int or a 2D vector of vectors - “seamlessly” handles jagged arrays

Which reminds me of an insightful observation that the array indexing operator [] and the function call operator () are basically doing the same thing: evaluating a function and returning a value. So what you've just described is basically Currying, or partial function evaluation. It's a bit like defining "al(i)" as array lookup for an index 'i' instead of "[i]". Then the following are equivalent: something[i][j] som…

[deleted]

Re: Arrays of Arrays (2009)

#23
post #3

Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload "operator[]". But it can't take more than one argument. Why? Because of the C "comma operator". In C, the comma operator evaluates both operands and discards the first result. It's an ancient hack used mostly with preprocessor defines. In C and C++, foo(…

> In C, the comma operator evaluates both operands and discards the first result. Sounds like how the semicolon operator works in modern languages, like Rust.

It does not, because this code does not compile:

  fn main() {
      println!("ok"; "Hello, world!");
  }

Re: Arrays of Arrays (2009)

#24
post #3

Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload "operator[]". But it can't take more than one argument. Why? Because of the C "comma operator". In C, the comma operator evaluates both operands and discards the first result. It's an ancient hack used mostly with preprocessor defines. In C and C++, foo(…

I just learned this one while teaching an intro to C++ class... the student had no idea what they wrote and it took me a minute to realize why their code even compiled.

Re: Arrays of Arrays (2009)

#25
post #9
post #3

Over a decade ago I was trying to get the C++ committee to support multidimensional arrays via overloading, so we could have 2D collections. In C++, you can overload "operator[]". But it can't take more than one argument. Why? Because of the C "comma operator". In C, the comma operator evaluates both operands and discards the first result. It's an ancient hack used mostly with preprocessor defines. In C and C++, foo(…

With C++20, use of comma expressions as second argument of a subscript operator got deprecated.

Finally!

Multidimensional array discussions tended to dissolve into bikeshedding. Some people wanted the ability to store an array as either transpose, that is, by row or by column. Then that had to be extended to N dimensions. That added a lot of complexity to support a rare use case. Then the proposal got so complex it was shelved. All this is in old USENET comp.lang.c++, if that hasn't been lost yet.

Re: Arrays of Arrays (2009)

#26
post #5

Earlier quoted context omitted.

Imo foo[x][y] is better for many reasons: - compiler can break it apart so foo[x] is evaluated once for many ys just like any other function - you can make types that are unaware of their rank eg vector doesn’t know if it’s a 1D vector of int or a 2D vector of vectors - “seamlessly” handles jagged arrays

Which reminds me of an insightful observation that the array indexing operator [] and the function call operator () are basically doing the same thing: evaluating a function and returning a value. So what you've just described is basically Currying, or partial function evaluation. It's a bit like defining "al(i)" as array lookup for an index 'i' instead of "[i]". Then the following are equivalent: something[i][j] som…

In C++ you can create a simple generic reusable currying wrapper. Boost as a few. The reality is that the vast majority of C++ programmers don't see the need.

Re: Arrays of Arrays (2009)

#27

Earlier quoted context omitted.

Which reminds me of an insightful observation that the array indexing operator [] and the function call operator () are basically doing the same thing: evaluating a function and returning a value. So what you've just described is basically Currying, or partial function evaluation. It's a bit like defining "al(i)" as array lookup for an index 'i' instead of "[i]". Then the following are equivalent: something[i][j] som…

In C++ you can create a simple generic reusable currying wrapper. Boost as a few. The reality is that the vast majority of C++ programmers don't see the need.

Trying to do this this through Currying turns a simple 2D array access into a quite complicated operation. One that a compiler has to be really smart to turn into a gheap 2D array indexing operation again. This really matters in number-crunching code, where it seems that almost everything is about multiplying arrays.

Re: Arrays of Arrays (2009)

#28
Take a look at D language library Mir for N-Dimensional array and its comparison to Numpy [1].

Its native performance inside GLAS implementation is something to shout about and even comparable to OpenBLAS that is currently being used by Julia and Matlab [2][3].

[1]http://docs.algorithm.dlang.io/latest/mir_ndslice.html

[2]http://blog.mir.dlang.io/glas/benchmark/openblas/2016/09/23/...

[3]http://docs.mir.dlang.io/latest/mir_glas.html

Re: Arrays of Arrays (2009)

#29
post #27

Earlier quoted context omitted.

In C++ you can create a simple generic reusable currying wrapper. Boost as a few. The reality is that the vast majority of C++ programmers don't see the need.

Trying to do this this through Currying turns a simple 2D array access into a quite complicated operation. One that a compiler has to be really smart to turn into a gheap 2D array indexing operation again. This really matters in number-crunching code, where it seems that almost everything is about multiplying arrays.

Of course it wouldn't make much sense to do it for indexing. I was discussing currying as a generally useful tool.

Re: Arrays of Arrays (2009)

#30
post #20
post #12

Earlier quoted context omitted.

Take a look at yalsat: http://fmv.jku.at/yalsat/ Unpack the "v" version, look at yals.c. You will see there the following: #define PUSH(S,E) \ do { \ if (FULL(S)) ENLARGE (S); \ *(S).top++ = (E); \ } while (0) #define POP(S) \ (assert (!EMPTY (S)), *--(S).top) #define TOP(S) \ (assert (!EMPTY (S)), (S).top[-1]) #define PEEK(S,P) \ (assert ((P) These are definitions for typed dynamic arrays using C macros. Very useful…

> I hope I proved you wrong. I don't see where you did - Animats said he couldn't find examples of the comma operator inside square brackets, not that he couldn't see it being used at all.

I think he has failed precisely because he wanted not to succeed.
Post reply on HN