Hmm, I'm undecided about this. I've had a look over Quantum::Superpositions now, and it's got a lot of cool stuff in it. However, I see my approach as mostly syntactic sugar to replace things like:
if ($param eq 'this' || $param eq 'that'
|| $param eq 'the other') {
...
}
with
if ($param eq any('this', 'that', 'the other') {
...
}
which Quantum::Superpositions seems a bit overkill for.
Taking a hint from hardburn above, I did some benchmarks (below) and there is a significant runtime difference, between using my code and Quantum::Superpositions'.
One feature from Quantum::Superpositions that I think would be worth adding, is
$result = any(1,2,3) * 2;
if ($result == 4) {...}
which would be trivial to add.
Tanktalus asked above about if (all(@x) eq any(@y) {...}. I can see how useful that would be, but would have to experiment to find a way to implement it without getting as big as Q::S, while not making the code too ugly.
Would Junction::Simple be a more fitting name? (I'm very hesitant to claim a top-name space)
Benchmark timings...
$ /usr/bin/perl ./benchmark.pl
all: numeric (few)
Rate Quantum-Superpositions Ju
+nction
Quantum-Superpositions 1842/s --
+ -94%
Junction 30303/s 1545%
+ --
all: numeric (lots)
Rate Quantum-Superpositions Jun
+ction
Quantum-Superpositions 1364/s --
+ -44%
Junction 2433/s 78%
+ --
all: string
Rate Quantum-Superpositions Jun
+ction
Quantum-Superpositions 1689/s --
+ -79%
Junction 8000/s 374%
+ --
any: numeric (few)
Rate Quantum-Superpositions Ju
+nction
Quantum-Superpositions 747/s --
+ -97%
Junction 28571/s 3725%
+ --
any: numeric (lots)
Rate Quantum-Superpositions Jun
+ction
Quantum-Superpositions 37.9/s --
+ -97%
Junction 1429/s 3672%
+ --
any: string
Rate Quantum-Superpositions Jun
+ction
Quantum-Superpositions 317/s --
+ -95%
Junction 6422/s 1928%
+ --
And here's the test code:
#!/usr/bin/perl
use strict;
use warnings;
use Junction qw/ xall xany /;
use Quantum::Superpositions qw/ all any /;
use Benchmark ':all';
print "all: numeric (few)\n";
cmpthese(30_000,{
Junction => sub {
1 if xall(1..5) == 2;
},
'Quantum-Superpositions' => sub {
1 if all(1..5) == 2;
},
});
print "\nall: numeric (lots)\n";
cmpthese(30_000,{
Junction => sub {
1 if xall(1..300) == 250;
},
'Quantum-Superpositions' => sub {
1 if all(1..300) == 250;
},
});
print "\nall: string\n";
cmpthese(10_000,{
Junction => sub {
1 if xall('a'..'z') eq 'z';
},
'Quantum-Superpositions' => sub {
1 if all('a'..'z') eq 'z';
},
});
print "\nany: numeric (few)\n";
cmpthese(30_000,{
Junction => sub {
1 if xany(1..5) == 2;
},
'Quantum-Superpositions' => sub {
1 if any(1..5) == 2;
},
});
print "\nany: numeric (lots)\n";
cmpthese(2_000,{
Junction => sub {
1 if xany(1..300) == 250;
},
'Quantum-Superpositions' => sub {
1 if any(1..300) == 250;
},
});
print "\nany: string\n";
cmpthese(7_000,{
Junction => sub {
1 if xany('a'..'z') eq 'z';
},
'Quantum-Superpositions' => sub {
1 if any('a'..'z') eq 'z';
},
});