Matthew Moss wrote:
>> A 2 dimensional array as an array of arrays is a lowest common
>> denominator.  It assumes that I want only to use it as a simple storage
>> mechanism.
> 
> Aside from a 1-dimensional array using flattened indices, an array of
> arrays is the most basic and common mechanism, because memory is
> 1-dimensional. Show me a computer with 2-dimensional memory and I'll
> write you a better structure.
> 
That is really besides the point.  There are many different types of 
data structures that are intended for a variety of purposes.  They 
abstract away from the physical layer to make things happen more easily 
and intuitively in different contexts.  The table is a perfect example 
since that provides the type of power I want; however I am only looking 
for only fraction of that power on a very dynamic and very small set of 
data so creating a database would be overkill.  And if you think that 
having one dimensional memory limits us from having fast 2 dimensional 
data structures then you really need to take some time to better 
understand how some of these data structures trade memory usage for 
efficiency.  Try taking a look out the chunk file format, Lua's core 
table data structure, or how tables typically work in databases.

>> What I want is a 2 dimensional array data structure that has
>> powerful operating mechanisms such as sorting.  Additionally the way an
>> array of arrays is treated in Ruby goes against what is intuitive or
>> expected; and it was my understanding that Ruby was supposed to be
>> designed to do what "least surprises".
> 
> I think most people find the array of arrays to be fairly intuitive
> (i.e. first index selects a subarray, second index selects an item
> from the subarray), and Ruby holds to that intuition. I would
> nonoffensively suggest that what you are expecting is non-intuitive.
> 
I agree that it is intuitive when you are selecting a single array with 
the first index. a[0] returns [7,8,9] and a[0][0] returns 7.  That is 
not surprising.  What is less intuitive is a[0..1][x].  I'm sure it can 
be argued either way and I agree that you can't satisfy everyone.
> 
>>
>> So this allows a sort across the entire array and it should be noted
>> that it does an index step through comparison so [1,1,2] would come
>> after [1,1,1].  However this means I am limited to sorting only via this
>> mechanism.
> 
> You are limited to that sorting mechanism only if you choose to be;
> try using sort_by and use your own code to determine sort order.
> 
>> Based on Ruby syntax I would find this to be intuitive.
>>
>> a[0..1][0].sort
>>
>> The results I would then expect would be
>> [[1],[9]]
>>
>> The results I actually get from this are
>> [7,8,9]
> 
> a[0..1] grabs the 0th and 1st item of a.  However, in order to return
> multiple values, Ruby uses arrays, so those go right back into an
> array.  Then you use [0], and get back the 0th item [7,8,9]. Pretty
> straightforward.
> 
> To get the results you want is not terribly difficult:   a.collect {
> |x| [x[0]] }
> 
> Yes, I've also heard that Ruby is intended to be a language of "least
> surprises", but you can satisfy everyone at the same time. However, it
> is also a language of easy extendability, so if an array of arrays
> does not serve your purposes, or you have a different interpretation
> of the [] operator, it's trivial to start fleshing out your own
> classes to make it a language of "least surprises for you".
>

It is not "trivial" to implement all classes.  Some classes are actually 
quite complex and time consuming to implement.  The point in my post was 
to see if there was currently a "standard" class already in use today. 
For more mature languages these types of structures already exist.  I 
enjoy using the Ruby language and find that it certain situations it is 
better than other languages, but I am loathe to reinvent the wheel or 
roll my own for something that I know other people use as well so it may 
well exist already.

> class Array2D
>    def [] (i)
>       # ...
>    end
>    def []= (i, x)
>       # ...
>    end
> end


-- 
Posted via http://www.ruby-forum.com/.