unifying hash and proc syntax

Discussion in 'Ruby' started by Joel VanderWerf, Mar 22, 2005.

  1. Hey, crazy idea. Why not use the same syntax for hashes and arrays? For
    example:

    {
    1 => 2,
    2 => 3,
    3 => 4,
    |x|
    x + 1
    }

    This is essentially a hash with a default_proc that adds 1 to its argument.

    Leave out the => pairs and you get something that acts like a proc.
    Leave out the |...|... construct and you get something that acts like a
    hash. With both, it's a hash with a default proc.

    It's backwards compatible.

    Are there any syntactic ambiguities?
     
    Joel VanderWerf, Mar 22, 2005
    #1
    1. Advertising

  2. Joel VanderWerf wrote:
    > Hey, crazy idea. Why not use the same syntax for hashes and arrays? For

    ^^^^^^
    erm, procs
     
    Joel VanderWerf, Mar 22, 2005
    #2
    1. Advertising

  3. Joel VanderWerf

    Hal Fulton Guest

    Joel VanderWerf wrote:
    > Hey, crazy idea. Why not use the same syntax for hashes and arrays? For
    > example:
    >
    > {
    > 1 => 2,
    > 2 => 3,
    > 3 => 4,
    > |x|
    > x + 1
    > }
    >
    > This is essentially a hash with a default_proc that adds 1 to its argument.
    >
    > Leave out the => pairs and you get something that acts like a proc.
    > Leave out the |...|... construct and you get something that acts like a
    > hash. With both, it's a hash with a default proc.


    It's a fllor wax AND a dessert topping!

    > It's backwards compatible.


    Delicious! And just look at that shine!

    > Are there any syntactic ambiguities?


    Hmm, not sure. But it certainly gives me a headache to think about it.


    Hal
     
    Hal Fulton, Mar 22, 2005
    #3
  4. Joel VanderWerf

    Csaba Henk Guest

    On 2005-03-22, Joel VanderWerf <> wrote:
    > example:
    >
    > {
    > 1 => 2,
    > 2 => 3,
    > 3 => 4,
    > |x|
    > x + 1
    > }
    >
    > This is essentially a hash with a default_proc that adds 1 to its argument.


    Witty! What's somewhat dubious in it is melting two things which are so
    different as objects. A hash is basicly transparent, a proc is basicly
    opaque. And an object which has a proc as an attribute in some sense
    won't just become a proc...

    But, hm, all-in-all, I like it. I always tend to think of hashes as
    mappings defined at finitely many places, abstracting from the data
    structure which implements it (and gives it its name).

    Csaba
     
    Csaba Henk, Mar 22, 2005
    #4
    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. David Lozzi
    Replies:
    3
    Views:
    1,949
    David Lozzi
    Jun 1, 2005
  2. Rim
    Replies:
    1
    Views:
    680
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Jun 30, 2003
  3. NevilleDNZ
    Replies:
    9
    Views:
    451
    NevilleDNZ
    Aug 16, 2006
  4. Eric S. Johansson

    unifying many packages under one name

    Eric S. Johansson, Aug 24, 2007, in forum: Python
    Replies:
    2
    Views:
    293
    James Stroud
    Aug 24, 2007
  5. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
Loading...

Share This Page