Skip Menu |

This queue is for tickets about the Data-Stream-Bulk CPAN distribution.

Report information
The Basics
Id: 58168
Status: open
Priority: 0/
Queue: Data-Stream-Bulk

People
Owner: Nobody in particular
Requestors: hanenkamp [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: A straight up iterator would be nice
The double loop is tedious in most cases. However, if I want to collapse my iteration to a single loop, I either have to write a custom iterator like: sub iterator { my $stream = shift; my $block = []; return sub { $block = $s->next unless @$block; shift @$block }; } my $iter = iterator($stream); while (my $obj = $iter->()) { # do something... } Or consume the whole list using all(). I'd prefer one at a time as an option. Seems like something like this iterator sub should be part of the interface. This wouldn't be a problem except that Data::Bulk::Stream is the de facto iterator of KiokuDB, which I'm experimenting with at the moment. It seems silly for me to write the utility subroutine in addition to the iterator class, when this is really a function of the iterator class.
Subject: Re: [rt.cpan.org #58168] A straight up iterator would be nice
Date: Sun, 6 Jun 2010 15:50:48 +0300
To: bug-Data-Stream-Bulk [...] rt.cpan.org
From: Yuval Kogman <nuffin [...] cpan.org>
Would you be willing to provide a patch with tests? http://github.com/nothingmuch/data-stream-bulk FWIW, it looks like it's missing a condition for the end of the stream (i.e. it might dereference an undef) Putting in the Data::Stream::Bulk role seems appropriate, but I'd call it 'to_iterator_sub' or something more explicit, since iterators come in so many forms on the CPAN. Also, while (my $obj = $iter->() ) would not work for undef, so at the very least that should be documented as a caveat (i.e. if the stream is known to potentially contain 'undef' values, there's no reliable way to discern the end of the stream).
I was also thinking about a one-at-a-time wrapper for this, so here's my two cents... Make a class that does MooseX::Iterator::Role (next, has_next, peek) that wraps any ::Bulk object. I like this interface better than a simple sub{...} since it does allow you to iterate over values that are false. You do need something like ->has_next or ->is_done (note, those do mean slightly different things). This class would not do the Data::Stream::Bulk role since ->next would have a different meaning -- this should be explained in the docs. Then have an export in Data::Stream::Bulk::Util that takes a ::Bulk object and returns one of these wrapper objects. Not sure what to call the class or export. Data::Stream::Bulk::OneAtATime? Technically this could be in a separate dist, but would be convenient to not be.