### Comment on

 Need Help??

Over in Ultra compact set searching, I had a little function called "and_maker" which inspired two comments, so I figured I'd generalize it and give it its own node.

The general problem is: I have an operation I would like to perform on each element in a list.

It's very easy to state. It's very easy to think about. "I want to add up these 5 items." "I want to multiply these 7 things." "I want to xor these 83 elements." Like so many things, the devil's in the details. While you can hardwire your function to add up exactly 5 things, you may need to add up 6 next time. And maybe 4 after that.

The standard approach is to use an iterator. I'll show you a different way. Not necessarily better, but different.

A standard way to do this might be something like the following. Say you want to add up a list of elements:

```
my \$sum = 0;
\$sum += \$_ foreach @_;
return \$sum;
}

What we're gonna do instead is dynamically generate a subroutine that only operates on the number of elements you have for this call. We'll be slick and cache the function so that subsequent calls give us back the same code snippet. Call the arbitrary_operator_maker with two arguments - the operator and the number of elements.

```
{
my %dispatch = ();

sub arbitrary_operator_maker {
my \$operation    = shift;
my \$num_elements  = shift;

my \$dispatch_key = "\$operation\$num_elements";

return \$dispatch{\$dispatch_key} if \$dispatch{\$dispatch_key};

my \$built = join \$operation, map {'\$_[' . \$_ . ']'} (0..\$num_eleme
+nts - 1);

return \$dispatch{\$dispatch_key} = eval "sub { \$built }";
}
}

You then create a new coderef for what you want to do:

```

print "\$result\n";

print "\$other_result\n";

#etc.

The function generated is straightforward. If you looked at what it actually does (which you can see by dumping out the \$built variable inside the arbitrary_operator_maker, it builds up your list of elements once for you, for repeated use.

```

sub {
\$_[0] + \$_[1] + \$_[2]
}

my \$xor_5_things = arbitrary_operator_maker('^', 5);

\$xor_5_things internally looks like:
sub {
\$_[0] ^ \$_[1] ^ \$_[2] ^ \$_[3] ^ \$_[4]
}

Okay, so what? Well, this newly created function doesn't need to iterate, it just performs the operation as many times as you need. Performance can be impressive, as I'd written up over here. But, in other testing I'd done, its scaling is twitchy.

There also is overhead with doing the lookup and dispatching a pre-created function. If you can cache yourself in advance, the benefits are impressive. But my caching routine may just suck.

```
my \$op = '+';
my \$and_maker_5 = arbitrary_operator_maker(\$op, 5);

my @list_5 = (1..5);

timethese(1000000, {
'and_maker 5 elements cached' => sub {
\$and_maker_5->(@list_5);
},
'and_maker 5 elements dispatched' => sub {
my \$and_maker_5 = arbitrary_operator_maker(\$op, 5);
\$and_maker_5->(@list_5);
},
'multi_add 5 elements' => sub {
},
});

Benchmark: timing 1000000 iterations of and_maker 5 elements cached, a
+nd_maker 5 elements dispatched, multi_add 5 elements...
and_maker 5 elements cached:  2 wallclock secs ( 0.98 usr +  0.00 sys
+=  0.98 CPU) @ 1020408.16/s (n=1000000)
and_maker 5 elements dispatched:  4 wallclock secs ( 2.95 usr +  0.01
+sys =  2.96 CPU) @ 337837.84/s (n=1000000)
multi_add 5 elements:  3 wallclock secs ( 2.53 usr +  0.01 sys =  2.54
+ CPU) @ 393700.79/s (n=1000000)

Basically, a technique like this would be useful if you're going to do the same operation many many times over the same number of arguments. If not, the overhead of building the function or even looking up a pre-cached function may cause performance to fall below that of an iterator. This version of the operator maker also does no bounds checking. So you can call \$add_5_things with 3 elements or 97 of them. That may be a bad thing. YMMV.

In reply to The arbitrary operator maker by jimt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-12-17 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What programming language do you hate the most?

Results (464 votes). Check out past polls.

Notices?