[Dbix-class] Improved resultset iterators
Matt S Trout
mst at shadowcat.co.uk
Thu May 6 20:31:10 GMT 2010
On Tue, May 04, 2010 at 01:08:52PM +1000, Toby Corkindale wrote:
> On 22/04/10 19:00, Peter Rabbitson wrote:
> >Toby Corkindale wrote:
> >>Hey all,
> >>Wouldn't it be nice if the ResultSet iterators were a bit more advanced?
> >>
> >>I would love it if the iterator built into DBIx::Class supported some
> >>more functional programming style methods.
> >>
> >>ie. So that I could do:
> >>
> >>$schema->resultset('Users')->search(
> >> { first_name => 'Bob' }
> >>)->foreach(
> >> sub { email_user($_) }
> >>);
> >>
> >>Or to "map" to get the results I want in one hit:
> >>
> >>my @uppercase_names = $schema->resultset('Users')->search(
> >> { first_name => 'Bob' }
> >>)->map(
> >> sub { uc($_->last_name . $_->firstname) }
> >>);
> >>
> >>Or do a more complex check, grep style, to filter the list:
> >>
> >>my @y_users = $schema->resultset('Users')->search(
> >> { first_name => 'Bob' }
> >>)->grep(
> >> sub { return complex_check($_) }
> >>);
> >>
> >
> >The implicit iterators built into DBIC resultsets were a blatant design
> >mistake, and therefore they will not be extended any longer (at least not
> >in core). You are however welcome to discuss a design for a ResultSet
> >component, or even contribute to the DBIx::Class::Helpers family of
> >modules.
>
> Hi,
> I attach a ResultSet Component that adds the features I was discussing.
>
> Would this be appropriate to release as a mini CPAN module or would you
> like to incorporate it into something else?
I would like you to never release that, and erase all copies from your hard
disk.
As noted, they are a design mistake. One you are perpetuating.
If you want to do something useful, write a resultset component that
extracts the iterator work out into a separate object and proxies next,
first and reset to a built-in iterator.
Then you cold implement things like foreach as
$self->iter->each(...
or similar. Which would be far cleaner.
> Also, I wondered at which point it is best to call $resultset->reset..
> At the start, or end, or both, of all the methods?
Any design that requires ->reset to be called is perpetuating the same
mistake as perl's each() builtin makes and should be taken out and shot.
--
Matt S Trout - Shadowcat Systems - Perl consulting with a commit bit and a clue
http://shadowcat.co.uk/blog/matt-s-trout/ http://twitter.com/shadowcat_mst/
Email me now on mst (at) shadowcat.co.uk and let's chat about how our Catalyst
commercial support, training and consultancy packages could help your team.
More information about the DBIx-Class
mailing list