Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^4: Using hashes for set operations...

by LanX (Cardinal)
on May 23, 2011 at 20:15 UTC ( #906358=note: print w/replies, xml ) Need Help??

in reply to Re^3: Using hashes for set operations...
in thread Using hashes for set operations...

> No, I'm thinking that it is pointless to compare references since any two copies will test as unequal.

your thinking of nested structures I'm thinking of objects. If you have instances representing something like "Employees" you don't wanna identify twins.

> so do the same test as == against the reference itself (the address).

Actually it's eq, think about the way scalars are compared.

> The user wants to find the intersection of two lists, so he would be told to pass @set1 and @set2, and optionally a &func, which defaults to built-in stringification.

I was already meditating about this, I also like the Python approach (where sets are a built-in datatype) to make the hash function operate on the basis of an "equality" method of "hashable" objects. (but I don't know how this is efficiently implemented) IIRC it's possible in Perl to overload the way objects are stringified.

>If I want two ad-hoc uses of [qw/1 2 3/] to be considered the same, stringifying the reference won't do it.

IMHO sets of "deeply compared" nested structure are better done with nested hashes. (kind of a tree search for each level of nesting)

> And we suppose that this (the key function) is expensive, so only call it once per value in each input list.


BTW: interesting read

Cheers Rolf

update: fixed unescaped brackets

Replies are listed 'Best First'.
Re^5: Using hashes for set operations...
by John M. Dlugosz (Monsignor) on May 23, 2011 at 20:21 UTC
    I'm thinking X, you're thinking Y: more food for the FAQ. There are different problems to be solved.

    Stringification on an object: The overloaded stringify function might not be what you want for this specific call to intersection. Just like you can sort different ways, you want to identify matches some custom way for this operation alone.

    I'll read those links and reply more later. (Oh, that's not a link, it's an unescaped [).

      > The overloaded stringify function might not be what you want for this specific call to intersection.

      Sure but you can't solve all possible tasks at the same time. The stringification of an objectref is IMHO a reasonable default, which should be configurable of course.

      Cheers Rolf

        I agree. Default is built-in stringification, which gives object identity or any existing overloaded stringify for that object.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2021-03-01 07:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found