Actually you can have more than one active iterator; however in that case, you should explicitly specify the iterating list when calling iter. I avoided the splice-based solution because I didn't want to add the overhead of copying the array. In truth, I'm not sure how much performance this buys me, considering all the splice-ish code which is now duplicated in perl. I also prefered not to take the OO aproach because I wanted a function that would feel more like a built-in (like each, for example).

That said, my solution is pretty ugly in comparison :)

update: After running some benchmarks, it's apparent that my solution is less efficient (by a factor of about 7x when iterating on each individual item), since the built-in splice is considerably faster than a re-implementation of a non-destructive splice in Perl. (though, my iter does outperform the OO iter when iterating and returning segments of length 100 or greater {really big grin})

So without further ado, -- this quote-unquote craft, and forget I ever wrote this piece of schnapz.

update2: Oh yeah, I forgot to mention, you wrote alot of code! :p

sub iter { my @a = @_; sub { splice @a, 0, shift || 1 }; }

In reply to Re: Re: iter by MeowChow
in thread iter by MeowChow

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.
  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.