Proposed new data structure: Pile
We're all used to working with arrays to implement queues and stacks using shift
, unshift
, push
, and pop
. They allow you to keep your data nice and tidy, well-ordered and predictable.
But life isn't nice and tidy, well-ordered or predictable.
We need something in our programming language that reflects the mess of the real world.
The Pile
Enter the concept of the Pile. A pile, like a stack, allows you to add data to it, as well as retrieving it. But, rather than using the neat pop
and push
, you use mush
and mop
.
Adding data is done using the mush
operator. You can mush arbitrary collections of data into a pile.
Retrieving data is done with the mop
operator. When mopping data from a pile, you can specify how many items to mop (i.e. the "capacity").
Syntax
The first problem with this new data type is to find a proper sigil for piles. Since Perl supports unicode, I would suggest U+1F4A9. This would give us:
my 💩pile; # empty pile
my 💩pile = ('some data', $some_other_data, %ENV); # Pile up some initial data.
Adding ("mushing") data
Adding data is simple:
mush 💩pile, $item1;
mush 💩pile, ($some_data, $some_more_data), %ENV;
When mushing data on the pile, the order and even the structure of what you add becomes unpredictable: items may be concatenated with others, lists will be scrambled, numbers can become bit strings, etc.
Note that this implies that if you add 10 items, the pile will not necessarily contain 10 items. Some items may be mushed together with others, code references may get called with adjacent items in the pile, and be replace by the result of the function call. There's no telling.
Retrieving ("mopping") data
The mop
operator takes an optional "mop capacity" parameter:
my $data = mop 💩pile; # always one item
my @data = mop 💩pile, 3; # mop up three items
my @data = mop 💩pile; # mop up the whole pile
The mop
operator removes items from the pile. Again, there's no telling what you'll get back. Strings, numbers, undef
, references to hashes, arrays, functions, binary blobs, anything goes.
Copying
You can assign a pile to another pile:
💩pile2 = 💩pile1;
Note, however, that those two piles will not be the same. No two piles are ever the same. Hence, the Perl compiler can statically optimise 💩pile1 == 💩pile2
conditions by replacing them with a false value.
Application
I believe the Pile fits best in AI research and real world process modelling.
yes
U+1F4A9.
The most popular emoji in Canada
First I thought this was silly. Then I saw it was a joke, but I thought it was a stupid one. Then I got to the end. Hah! 😊