Auto-created Array elements?

Discussion in 'Ruby' started by Brian Candler, Oct 12, 2004.

  1. 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.
     
    Brian Candler, Oct 12, 2004
    #1
    1. Advertising

  2. Brian Candler

    ts Guest

    >>>>> "B" == Brian Candler <> writes:

    B> a = Array.new { [] }

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


    Guy Decoux
     
    ts, Oct 12, 2004
    #2
    1. Advertising

  3. "Brian Candler" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Robert Klemme, Oct 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2FyYW4=?=

    Auto-Suggested Textbox like google auto suggest

    =?Utf-8?B?V2FyYW4=?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,538
    inrakeshworld
    Jul 27, 2007
  2. Ulf Heyder
    Replies:
    0
    Views:
    580
    Ulf Heyder
    Oct 16, 2003
  3. P
    Replies:
    1
    Views:
    1,181
    Joe Kesselman
    Jul 7, 2006
  4. linkswanted
    Replies:
    1
    Views:
    948
  5. [rob desbois]
    Replies:
    2
    Views:
    288
    [rob desbois]
    Apr 15, 2008
Loading...

Share This Page