[RCR] #inject, #partition expand array if arity > 2

Discussion in 'Ruby' started by Simon Strandgaard, Oct 29, 2003.

  1. It would be awesome, if #inject could do splitting when arity > 2.
    For instance converting an array of pairs into a hash (arity==3):

    x=[["name", "john"], ["age", 20]]
    p x.inject({}){|h,k,v|h[k]=v;h}
    #=> {"name"=>"john", "age"=>20}

    Also #partition cannot deal with arity > 2..
    All other iterator methods seems to deal ok with arity>2.
    Have I forgotten any iterator methods ?


    I have submitted the RCR here:
    http://www.rubygarden.org/article.php?sid=333

    Any thoughts ?

    --
    Simon Strandgaard
    Simon Strandgaard, Oct 29, 2003
    #1
    1. Advertising

  2. Moin!

    Simon Strandgaard wrote:

    > It would be awesome, if #inject could do splitting when arity > 2.
    > For instance converting an array of pairs into a hash (arity==3):


    > x=[["name", "john"], ["age", 20]]
    > p x.inject({}){|h,k,v|h[k]=v;h}
    > #=> {"name"=>"john", "age"=>20}


    This is already possible with a built-in ruby feature:

    x=[["name", "john"], ["age", 20]]
    x.inject({}) { |h, (k,v)| h[k]=v; h } # => {"name"=>"john", "age"=>20}

    Regards,
    Florian Gross
    Florian Gross, Oct 29, 2003
    #2
    1. Advertising

  3. On Thu, 30 Oct 2003 04:59:57 +0900, Florian Gross wrote:
    [snip]
    > x=[["name", "john"], ["age", 20]]
    > x.inject({}) { |h, (k,v)| h[k]=v; h } # => {"name"=>"john", "age"=>20}

    ^^^^^
    Thats nice.. I wasn't aware of this feature.
    I will start from today using parantesis this way.

    --
    Simon Strandgaard
    Simon Strandgaard, Oct 29, 2003
    #3
  4. Simon Strandgaard

    Harry Ohlsen Guest

    Florian Gross wrote:

    > Moin!
    > This is already possible with a built-in ruby feature:
    >
    > x=[["name", "john"], ["age", 20]]
    > x.inject({}) { |h, (k,v)| h[k]=v; h } # => {"name"=>"john", "age"=>20}


    I've not seen that syntax/semantics before. Is the |(k,v)| syntax documented somewhere?

    I'm guessing it's not in Pickaxe, since inject didn't appear until later ... or is this kind of syntax generally available when iterating over hashes (or anything with key/value pairs)?

    It seems incredibly useful!

    Cheers,

    Harry O.
    Harry Ohlsen, Oct 29, 2003
    #4
  5. Simon Strandgaard

    Dan Doel Guest

    I believe it's just assignment semantics.

    Block parameters are set in the same way that an assignment statement is
    evaluated, so essentially, it's the same as something like:

    x = [["name", "john"], ["age", 20]]

    h, (k,v) = {}, x[0]

    which does:
    h = {}
    (k,v) = ["name", "john"] # (k = "name", v = "john")

    When assignments involve commas, they're implicitly converted to arrays, so
    the above is the same as:

    [h, [k, v]] = [{}, x[0]]

    which explains why things happen the way they do.

    Cheers,

    - Dan
    Dan Doel, Oct 29, 2003
    #5
    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. bolega
    Replies:
    1
    Views:
    670
    Stan Bischof
    Mar 28, 2011
  2. Simon Strandgaard

    [RCR] array or with non-array

    Simon Strandgaard, Aug 2, 2005, in forum: Ruby
    Replies:
    27
    Views:
    231
    Nikolai Weibull
    Sep 1, 2005
  3. Peña, Botp

    inject does not inject last value

    Peña, Botp, Aug 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    169
    Peña, Botp
    Aug 7, 2006
  4. Anthony Martinez
    Replies:
    4
    Views:
    258
    Robert Klemme
    Jun 11, 2007
  5. Michal Suchanek
    Replies:
    6
    Views:
    214
    Nobuyoshi Nakada
    Jun 13, 2007
Loading...

Share This Page