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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|