Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Auto-created Array elements?

Reply
Thread Tools

Auto-created Array elements?

 
 
Brian Candler
Guest
Posts: n/a
 
      10-12-2004
a = Array.new { [] }
p a #=> []
p a[3] #=> nil

Just checking: you can't have an Array whose elements spring into life when
needed (as you can with a Hash)?

Maybe a good thing - you might not want a single access to a[10000] to
create that many objects - but it could be useful on occasions.

No need to post other solutions - I can subclass/delegate such that [], []=
are overridden appropriately, although usually I just end up writing

a[x] ||= []
a[x][y] = 'whatever'

Regards,

Brian.


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      10-12-2004
>>>>> "B" == Brian Candler <(E-Mail Removed)> writes:

B> a = Array.new { [] }

svg% ruby -we 'Array.new { [] }'
-e:1: warning: given block not used
svg%


Guy Decoux


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-12-2004

"Brian Candler" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> a = Array.new { [] }
> p a #=> []
> p a[3] #=> nil
>
> Just checking: you can't have an Array whose elements spring into life

when
> needed (as you can with a Hash)?
>
> Maybe a good thing - you might not want a single access to a[10000] to
> create that many objects - but it could be useful on occasions.
>
> No need to post other solutions - I can subclass/delegate such that [],

[]=
> are overridden appropriately, although usually I just end up writing


You only need to override [], do you?

Btw, this raises an interesting question about the desired semantics of
auto creation: should it kick in for all elements that are nil or just for
those beyond the array limits? Should it create just a single instance or
fill all newly created cells?

I'd opt for 1.2 and 2.1, i.e., only the element accessed is created if
they are nil. This feels the most close to Hash. This would be the impl
I'd choose:

class AutoArray < Array
NILLER = lambda { nil }

def initialize(*x, &b)
super(*x)
@missing = b || NILLER
end

def [](idx)
super || @missing.call(self, idx)
end
end

a = AutoArray.new {|a,idx| a[idx]=[]}

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments