In truth, I like using logical operators for this sort of thing, and personally find them very readable. Benefit of a C background.
The thing that bothers me with reading your code is your API. It is emergent from the logic and not very intuitive. You are implementing a class with a single closure which takes zero or more arguments.
- $fifo->() returns the next value from the queue, adjusting the state of $fifo.
- $fifo->(undef) falls all the way through, returning a defined false and not altering state.
- $fifo->($foo,@bar), with $foo defined, enqueues the elements of the arg list, returning the total length of the queue including those elements which have already been removed. This is unexpected, to me.
- $fifo->(undef,'cmd'), for 'cmd' one of (reset|all|ordered|dump) either rewinds the queue (returning 0), or returns a peek at all its values.
- $fifo->(undef,undef,@bar) triggers an undefined value warning.
Note that each instance of a uFIFO carries an interpreter around with it.
How about an implementation as a module? Make Uniqueue.pm define a data class with a constructor, a $fifo->enqueue( @foo ) acting like push, and a $fifo->next() which acts like either shift or a nondestructive iterator, depending on what behavior you want. Other methods like 'all' or 'reset' then can be defined as methods also.
I've coded a Uniqueue.pm in terms of shift. If there's interest, I'll send it to the catacombs.
After Compline,
Zaxo
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.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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?
See Writeup Formatting Tips and other pages linked from there for more info.