YA Perl Advent Calendar 2005-12-11

There's at least one thing to look forward to in Perl 6, junctions. Junctions provide a simple interface for set-like operations, typically with the functions all, any, none, and one. For some time now we've had this functionality available to us in Perl 5 thanks to the Mad Aussie's Quantum::Superpositions; in fact it was featured on the advent calendar way back in 2001. Alas, this wonderful tool came with a very hefty performance hit. In my first use of the module, I ended up switching to a grep and gaining a ten+fold increase in speed.

Enter Perl6::Junction, note the lack of plural—I doubt I'm the only one who'll find themselves repeatedly typing Perl6::Junctions. Junction might be thought of as Superpositions Lite: it lacks several features but is significantly faster 1. In a recent thread on Boston.PM we determined that Junctions is about 50x faster than Quantum 2

ANY:  
      s/iter    QS    P6       <= Note this is s/iter instead of Rate (iter/s)
QS      1.20    --  -98%          Rate ~ 0.83/s
P6 2.10e-002 5611%    --          Rate ~ 47.6/s

ALL:  
     Rate    QS    P6  
QS 1.46/s    --  -98%  
P6 79.2/s 5337%    --  
One of the notably missing features of Perl::Junction 1.1 is that it neither exports by default nor does it provides an :all tag. It also lacks stringification, or the ability to view eigenstates.

1. The performance difference between Junction and Superpositions are due to implementation details rather than feature set e.g; Junction uses short-circuiting; internally Superpositions is object oriented, uses multimethods and an occasional eval.

2. Activestate 813 on XP.

See Also

For more generic set operations see Set::Array, Set::Bag, Set::IntRange, Set::IntSpan, Set::Scalar