Setting the bar high with an array of unique gifts

by Adam Russell

Not every elf studies the theory and craft of toymaking at elf college. Some elves study more abstract but less employable subjects and come to toy making after making the sad realization that very few people care to, say, discuss that shiny new Rubik's Cube that Santa dropped off in terms of Galois Theory.1 Of course, there are some happy intersections between the abstract and the practical. Set::Array is an example of one such useful application of the abstract—Set Theory—towards cleaner and more maintainable code. Set::Array essentially wraps arrays in a class that defines higher level functions on the internal data. Of course you can still do the basics. The code below shows some of the classic array functions anyone would expect.

Running this:

my$sa0=Set::Array->new(qw/b a yams tartes/);$sa0->pop();#methods that that throw pies aren't chainable$sa0->unshift('cider')->delete('yams')->reverse();

Results in a $sa0Set::Array object with elements (a, b, cider)

But such functionality is simply the new pair of underwear under the tree when what you really want is a train set! Let us direct our attention to the well hidden and expertly wrapped gifts in the corner under the stairs.

Set::Array comes with a variety of parent-approved2 Nerf weaponry to attack our array-like objects with ammunition ranging from the canonical union() on line 36 for our sample script, to the sugary join() on 38 and esoteric bag() on lines 35 and 53.

bag() is a bit of a departure from "real" sets in that it does not exclude duplicates. To ensure uniqueness to elements the module comes with a very necessary unique() function which is included presumably to, you know, actually allow us to convincingly use the word "set". ho ho ho.

Given another array…

my$sa1=Set::Array->new(0,1,'cider','eggnog');

…we can test some of the more set-like operations amongst the thirty-odd methods available:

In case you've forgotten your math, that last one returns the elements that occur in either set but do not also occur in the intersection.

Finally, it should be mentioned that the proper set operations implemented by Set::Array also come with overloaded operator synonyms i.e; +, &, -, *, and % They were left out of the examples since overloaded operators bring back ghosts of C++ courses past.