Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: map and grep (but for hashes)

by BrowserUk (Patriarch)
on Jan 31, 2009 at 00:30 UTC ( [id://740358]=note: print w/replies, xml ) Need Help??


in reply to Re^4: map and grep (but for hashes)
in thread map and grep (but for hashes)

develop a new school of Perl Fu around this new potent weapon!

I assumed that you already has uses for them, and I'm not at all convinced about their potency. By flattening the hash to a list, you've effectively thrown away most all the properties that make hashes so useful:

  1. O(1) lookup.
  2. exists

The most effective uses of hashes involve going directly to the items of interest and avoiding having to iterate the entire structure.

It's an interesting exercise to construct them, but I think that the uses will be fairly limited. If there were a raft of good uses, someone would have invented them before now.


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^6: map and grep (but for hashes)
by zerohero (Monk) on Feb 01, 2009 at 20:07 UTC

    I only realized later that the version flattened to list, which I think was an ambiguity in my spec, i.e. "to make this work like map and grep", only for hashes. The spec was imperfect, but a valuable place to start, naively, to zero in on the real question (it's often hard to ask a precise question at the beginning of a task).

    In fact, this school of programming already exists, and it's called "functional programming". That it's powerful is already demonstrated in many real world applications. map is borrowed from functional programming, and is the simplest, first order example of it. So at the heart of my question was is there any other functional programming support in Perl, besides map? For example transformations that take hashes to hashes, and can be composed? Let's try hmap.

    It's not that I couldn't write the code for this myself (once I figured out what the spec actually was). However, there was the sneaking feeling that this was already done, well understood, and there would be better techniques for it.

    Thanks for taking the time to answer my questions. I learn a lot with each round of discourse.

      As long as you treat a hash as a list of pairs, you can pass hashes around just like lists.

      If you're interested in how to do Functional Programming in Perl, I highly recommend Higher Order Perl, which nowadays even exists as a free, legal, complete downloadable ebook.

      Perl supports anonymous functions and closures, which are indispensable once you start passing code as parameters. Perl does not really support typing. Depending on who and how you ask, Perl is typed from very weak to very strong. It has implicit type conversions and no further type inference. It supports recursion, but it has no pattern matching, and is a strict language (even though strict is something completely different).

      If you're looking for composability and flexibility, this version will

      1. accept either a list of hashref;
      2. return a list or hashref in context;
      3. provides key and value as: $a & $b; and in @_; and the value as $_.

      Which allows your composed "query" to operate fairly naturally:

      my %new = hmap { exists $b->{ nick } && $b->{ nick } =~ m[^(?:rbush|fred)$] ? ( $a => scalar hgrep{ $a =~ m[^(nick|phone)$] } $b ) : () } \%old;

      Code and tests

      That done, the next thing you are likely to want, is to be able to process arrays nested in hashes; and hashes nested in arrays; and conditionally either. At which point you are moving into the realms of the provisions of Data::Rmap.


      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.
        Thanks for the pointers. I'm also about halfway through "Higher Order Perl" which seems to give a few nuggets as well (although the first chapters are a pretty long rehash of recursion). It's become clear that part of my struggle is figuring out the Perl syntax around lists and subroutines (e.g. used in closures). I'm good with hierarchical anonymous structures, but clearly some of my lack of precise syntactical knowledge is holding me back. Anyway, thanks for all the pointers!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://740358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found