http://www.perlmonks.org?node_id=1025044


in reply to ChainMap of Hashes on CPAN?

Given that the chained hash will only allow access to keys found earlier in the chain -- ie. a key 'a' in a hash later in the chain will be hidden by a key 'a' in an earlier hash -- what is the advantage of a chained hash over composing a simple hash, from the chained hashes in reverse order?

I'm just wondering (but not dismissing) the use-case?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: ChainMap of Hashes on CPAN?
by LanX (Saint) on Mar 23, 2013 at 16:54 UTC
    > I'm just wondering (but not dismissing) the use-case?

    It's useful to avoid the overhead of concatenating very long hashes if you only need few lookups.

    A "lazy" approach if you want.

    There some use cases listed in the Py-docs and have a look into the talk for his justification (I deep-linked to 29m10s)

    But actually I'm also suspicious about the frequency of such valid use cases, in those rare cases I would personally simply reimplement the get() function as a wrapper like I showed.

    Now my intention was to show how easily a py-idiom can be adapted.

    But this talk is quite interesting, it often goes into length to describe how to solve problems we do not have in the realm of the onion.

    And this with a kind of "hurray" attitude which is somehow alien to me... ¹

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) but alas Europeans are bad in marketing! ;-)

      It's useful to avoid the overhead of concatenating very long hashes if you only need few lookups.

      Then I think I'd use:

      my $thing; exists $_->{ $key } and $thing = $_->{ $key } for \(%hash1, %hash2, %h +ash3);

      and save constructing a whole (class of) objects for something so trivial.

      But, I don't ever remember having such a requirement; nor can I think of an actual use-case.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Well your code has a logical flaw and a typo.

        And I already explained that I would probably do something similar.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        UPDATE:

        this works:

        exists $_->{ $key } and $thing = $_->{ $key },last for \(%H1, %H2);

        I there a typo?

        McA

      I had to laugh and just wanted to ask what vidoes you are watching... ;-)

      I have to cite from Monty Python’s Life of Brian:

      Are there any women(*) here today?

      McA

      (*) pythonistas

        Actually our local perlmongers group is "racially mixed" and we like comparing and learning from different approaches.

        I raised a discussion about how easy the counting idioms are implemeted in Perl because undef ++ == 1 and autovivification and was wondering how JS, Ruby and Python and so on are trying to solve this.

        (Our Haskell guy couldn't stop complaining how dirty Perl's behavior is)

        Now try this in these languages

        $count{$_}++ for @names

        or

        DB<135> @name=qw/rachel raymond matthew roger betty melissa judith c +harlie/ DB<136> push @{ $group{length $_} } , $_ for @name DB<137> \%group => { 5 => ["roger", "betty"], 6 => ["rachel", "judith"], 7 => ["raymond", "matthew", "melissa", "charlie"], }

        This video was mailed to me with the remark "this is clean idiomatic Python" and the speaker talks for minutes about strategies to count and group.

        There is indeed MOOOOOORE than one way to do it in Python where Perl has one simply way.

        --> http://stackoverflow.com/questions/3496518/python-using-a-dictionary-to-count-the-items-in-a-list

        We are quite fast in criticizing the downsides of some Perl features, it's worth showing where Perl profits from those features.

        "Only an idiot learns from his errors, the smart guy learns from other's errors"
        (attributed to Otto von Bismarck)

        And not everything I learned in this video was a bad idea!

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^2: ChainMap of Hashes on CPAN?
by LanX (Saint) on Mar 23, 2013 at 17:43 UTC
    Especially if at least one of hashes is a tie itself (like a DB wrapper or even more complicated) you will need a more complex container to add defaults and overrides.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^2: ChainMap of Hashes on CPAN?
by McA (Priest) on Mar 23, 2013 at 16:56 UTC

    As the Python guy says: No creation of a new hash based on the other hashes, no data copy, save resources.

    McA