in reply to How to distinct the call position of a sub?

Sorry, but to me that's design smell.

If your iterator cares about how and where it's being used, you basically prohibit all uses that you didn't think of, thus defeating the idea of code reuse.

The usual approach that you consider to be too verbose has the advantages that it

I think that ease of use on the one hand and ease of use on the other hand are not contradictory; one approach would be to use a tied array, so that you can iterate over it with a simple for loop.

Other ideas are to look on CPAN for lazy list implementations. (I've written Perl6::GatherTake as a proof of concept; it goes a bit in that direction, though I'm sure that you'll find much better suited modules).

I also think that if you ask broader, like "how do I write an iterator that's easy to use?" you might get even more clever ideas from the assembled monks.

Replies are listed 'Best First'.
Re^2: How to distinct the call position of a sub?
by LanX (Saint) on Nov 24, 2008 at 23:28 UTC
    Moritz, why should a position depending use-case be passed around??? Do you pass around the head of foreach or the flip-flop-operator?

    Sorry your completely missing the point, to find a solution for a one-way and one-place iterator+generator.

    Evidently if the iterator has to be passed around, you definetely need a initial construction in a generator. CPAN has this already.

    If you can't imagine, better think of literals:
    nobody initializes a variable and use it just once in the next line, when he can just use a literal string...

    TIMTOWTDI

    Cheers Rolf