XP is just a number | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
In addition to Zaxo's solution, consider implementing your "lists" as the keys of hashes to begin with (actually this is just packaging Zaxo's solution for easy reuse). Then all these membership questions become relatively easy to code. E.g., your "@A without @B" is basically a relative complement:
From here it's easy to code similar operations such as intersections: or the "symmetric difference" (in A but not B, or in B but not A; akin to xor): With the above:
Alternatively, you can roll out the big guns and use one of the implementations of sets from CPAN, such as Jarkko Hietaniemi's Set::Scalar. With the latter, the above reduces to:
Tangential mini-rant: As much as I like Set::Scalar, I have a major mathematical nit to pick with it: the standard OO $instance->method( @args ) interface is not the right one for set operations, because for too many of these operations, the arguments all have equal standing, something that is obscured by this interface (this is somewhat of a fixation of mine). It is just as stilted to say $A->union( $B ) as it is to say $A->sum( $B ). Moreover, this interface excludes the edge cases in which the operations are applied to no arguments (for operations, such as union, in which this is mathematically well-defined). When the operations on the instances of a class have these symmetry properties, I think it is better to implement them as class methods. the lowliest monk In reply to Re: most efficient way to implement "A without B"
by tlm
|
|