| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Mojo::Collection::Role::GroupBy; | ||
| 5 | |||
| 6 | use Role::Tiny; | ||
| 7 | use List::Util qw/uniq/; | ||
| 8 | use Hash::Util::FieldHash qw/fieldhash/; | ||
| 9 | use Carp; | ||
| 10 | use Exporter 'import'; | ||
| 11 | use Mojo::Collection::Role::GroupBy::Util qw/pack_array unpack_array/; | ||
| 12 | |||
| 13 | # ABSTRACT: adds group_by function to Mojo::Collection | ||
| 14 | |||
| 15 | our $VERSION = '0.003'; | ||
| 16 | |||
| 17 | requires 'each'; | ||
| 18 | |||
| 19 | fieldhash my %is_grouped; | ||
| 20 | fieldhash my %is_composite; | ||
| 21 | |||
| 22 | sub _is_grouped { | ||
| 23 | my $self = shift; | ||
| 24 | if (@_){ $is_grouped{$self} = shift } | ||
| 25 | return $is_grouped{$self} | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _is_composite { | ||
| 29 | my $self = shift; | ||
| 30 | if (@_){ $is_composite{$self} = shift } | ||
| 31 | return $is_composite{$self} | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _group { | ||
| 35 | my ($self, $group_sub) = @_; | ||
| 36 | |||
| 37 | my ($composite, $sub) = $self->_make_group_sub($group_sub); | ||
| 38 | my %h; | ||
| 39 | $self->each(sub { | ||
| 40 | my $v = shift; | ||
| 41 | my $k = $sub->($v); | ||
| 42 | push @{$h{$k}}, $v; | ||
| 43 | }); | ||
| 44 | return ($composite, \%h); | ||
| 45 | # return %h; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _type { | ||
| 49 | my $self = shift; | ||
| 50 | my %seen; | ||
| 51 | for ($self->@*) { | ||
| 52 | my $t = ref $_ eq 'ARRAY' ? 'ARRAY' | ||
| 53 | : ref $_ eq 'HASH' ? 'HASH' | ||
| 54 | : blessed $_ ? 'OBJECT' | ||
| 55 | : 'SCALAR'; | ||
| 56 | $seen{$t}++; | ||
| 57 | } | ||
| 58 | die "Mixed collection: contains " . join(", ", keys %seen) . "\n" if keys %seen > 1; | ||
| 59 | return (keys %seen)[0]; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub _make_group_sub { | ||
| 64 | my ($self, @args) = @_; | ||
| 65 | return (0, $args[0]) if ref $args[0] eq 'CODE'; | ||
| 66 | |||
| 67 | my $type = $self->_type(); | ||
| 68 | |||
| 69 | my $arg; | ||
| 70 | if (@args == 1 && !ref $args[0]) { | ||
| 71 | $arg = $args[0] | ||
| 72 | } | ||
| 73 | elsif (ref $args[0] eq "ARRAY" && scalar $args[0]->@* == 1) { | ||
| 74 | $arg = $args[0]->[0] | ||
| 75 | } | ||
| 76 | elsif (ref $args[0] eq "ARRAY") { | ||
| 77 | @args = $args[0]->@* | ||
| 78 | } | ||
| 79 | elsif (@args > 1) { | ||
| 80 | # already fine, @args is set | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | croak "Arguments must be scalars or an arrayref" | ||
| 84 | } | ||
| 85 | |||
| 86 | if (defined $arg) { | ||
| 87 | return (0, sub { ${$_[0]}[$arg] }) if $type eq 'ARRAY'; | ||
| 88 | return (0, sub { ${$_[0]}{$arg} }) if $type eq 'HASH'; | ||
| 89 | return (0, sub { $_[0]->$arg }) if $type eq 'OBJECT'; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | if ($type eq 'ARRAY') { return (1, sub { pack_array( map { $_[0]->[$_] } @args ) }); } | ||
| 93 | if ($type eq 'HASH') { return (1, sub { pack_array( map { $_[0]->{$_} } @args ) }); } | ||
| 94 | return (1, sub { pack_array( map { $_[0]->$_ } @args ) }); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub group_by { | ||
| 99 | my ($self, $group_sub) = @_; | ||
| 100 | |||
| 101 | my ($composite, $h) = $self->_group($group_sub); | ||
| 102 | my %h = $h->%*; | ||
| 103 | |||
| 104 | $self = $self->new(map { [ $_, $self->new($h{$_}->@*) ] } sort keys %h); | ||
| 105 | |||
| 106 | $self->_is_grouped(1); | ||
| 107 | $self->_is_composite($composite); | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub to_hash { | ||
| 113 | my ($self, $group_sub) = @_; | ||
| 114 | my $composite; | ||
| 115 | my %h; | ||
| 116 | unless ($group_sub) { | ||
| 117 | die "This does not look like a grouped Mojo::Collection\n" unless $self->_is_grouped; | ||
| 118 | $self->each(sub { $h{$_->[0]} = $_->[1] }); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | %h = $self->_group($group_sub)->%*; | ||
| 122 | } | ||
| 123 | $_ = Mojo::Collection->new($_->@*) for values %h; | ||
| 124 | return \%h; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub to_grouped_array { | ||
| 128 | my ($self, $group_sub) = @_; | ||
| 129 | my $h = $self->to_hash($group_sub); | ||
| 130 | return [ map { $h->{$_} } sort keys $h->%* ] | ||
| 131 | } | ||
| 132 | |||
| 133 | sub expand { | ||
| 134 | my $self = shift; | ||
| 135 | my $composite = $self->_is_composite; | ||
| 136 | my $h = $self->to_hash; | ||
| 137 | |||
| 138 | return Mojo::Collection->new( | ||
| 139 | $self->_is_composite ? | ||
| 140 | map { | ||
| 141 | [ unpack_array($_) , $h->{$_} ] | ||
| 142 | } sort keys $h->%* | ||
| 143 | : | ||
| 144 | map { | ||
| 145 | [ $_, $h->{$_} ] | ||
| 146 | } sort keys $h->%* | ||
| 147 | ) | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | |||
| 153 | =encoding utf8 | ||
| 154 | |||
| 155 | =head1 NAME | ||
| 156 | |||
| 157 | Mojo::Collection::Role::GroupBy - Group-by operations for Mojo::Collection | ||
| 158 | |||
| 159 | =head1 SYNOPSIS | ||
| 160 | |||
| 161 | use Mojo::Collection; | ||
| 162 | |||
| 163 | my $c = Mojo::Collection->new( | ||
| 164 | [0, "a"], [1, "b"], [2, "a"] | ||
| 165 | )->with_roles('+GroupBy'); | ||
| 166 | |||
| 167 | # Group by index | ||
| 168 | my $grouped = $c->group_by(1); | ||
| 169 | |||
| 170 | # Group by coderef | ||
| 171 | my $grouped = $c->group_by(sub { $_[0]->[0] % 2 }); | ||
| 172 | |||
| 173 | # Group by multiple keys (composite) | ||
| 174 | my $grouped = $c->group_by([0, 1]); | ||
| 175 | |||
| 176 | # Convert to hash | ||
| 177 | my $hash = $grouped->to_hash; | ||
| 178 | |||
| 179 | # Expand to collection of key/value pairs | ||
| 180 | my $expanded = $grouped->expand; | ||
| 181 | |||
| 182 | =head1 DESCRIPTION | ||
| 183 | |||
| 184 | L<Mojo::Collection::Role::GroupBy> is a role for L<Mojo::Collection> that adds | ||
| 185 | grouping operations. It can be applied to any L<Mojo::Collection> instance using | ||
| 186 | L<Mojo::Base/"with_roles">. | ||
| 187 | |||
| 188 | my $c = Mojo::Collection->new(...)->with_roles('+GroupBy'); | ||
| 189 | |||
| 190 | Collection elements can be arrayrefs, hashrefs, or objects. Mixed collections | ||
| 191 | will raise an error. | ||
| 192 | |||
| 193 | =head1 METHODS | ||
| 194 | |||
| 195 | L<Mojo::Collection::Role::GroupBy> implements the following methods. | ||
| 196 | |||
| 197 | =head2 group_by | ||
| 198 | |||
| 199 | my $grouped = $c->group_by(sub { ... }); | ||
| 200 | my $grouped = $c->group_by($index); | ||
| 201 | my $grouped = $c->group_by($key); | ||
| 202 | my $grouped = $c->group_by([$key1, $key2]); | ||
| 203 | my $grouped = $c->group_by($key1, $key2); | ||
| 204 | |||
| 205 | Group collection elements by the return value of a grouping function. Returns a | ||
| 206 | new L<Mojo::Collection> of C<[$key, $collection]> pairs sorted by key. | ||
| 207 | |||
| 208 | The grouping argument can be a coderef, a scalar index or key, or an arrayref | ||
| 209 | of indices or keys for composite grouping. | ||
| 210 | |||
| 211 | # Group arrayrefs by element at index 1 | ||
| 212 | my $grouped = $c->group_by(1); | ||
| 213 | |||
| 214 | # Group hashrefs by key | ||
| 215 | my $grouped = $c->group_by('category'); | ||
| 216 | |||
| 217 | # Group objects by method | ||
| 218 | my $grouped = $c->group_by('name'); | ||
| 219 | |||
| 220 | # Group by multiple keys | ||
| 221 | my $grouped = $c->group_by([0, 1]); | ||
| 222 | |||
| 223 | # Group with a coderef | ||
| 224 | my $grouped = $c->group_by(sub { $_[0]->[0] % 2 }); | ||
| 225 | |||
| 226 | =head2 to_hash | ||
| 227 | |||
| 228 | my $hash = $grouped->to_hash; | ||
| 229 | my $hash = $c->to_hash(sub { ... }); | ||
| 230 | my $hash = $c->to_hash($key); | ||
| 231 | |||
| 232 | Convert a grouped collection to a hashref, or group and convert in one step by | ||
| 233 | passing a grouping argument. Values are L<Mojo::Collection> objects. | ||
| 234 | |||
| 235 | # Group first, then convert | ||
| 236 | my $hash = $c->group_by('category')->to_hash; | ||
| 237 | |||
| 238 | # Group and convert in one step | ||
| 239 | my $hash = $c->to_hash('category'); | ||
| 240 | |||
| 241 | =head2 to_grouped_array | ||
| 242 | |||
| 243 | my $array = $grouped->to_grouped_array; | ||
| 244 | my $array = $c->to_grouped_array(sub { ... }); | ||
| 245 | |||
| 246 | Convert a grouped collection to an arrayref of collections, sorted by key. | ||
| 247 | |||
| 248 | =head2 expand | ||
| 249 | |||
| 250 | my $expanded = $grouped->expand; | ||
| 251 | |||
| 252 | Expand a grouped collection into a new L<Mojo::Collection> of C<[$key, $collection]> | ||
| 253 | pairs, where C<$key> is the plain grouping key. For composite keys, C<$key> is | ||
| 254 | an arrayref of the component values. | ||
| 255 | |||
| 256 | my $expanded = $c->group_by(1)->expand; | ||
| 257 | $expanded->each(sub { | ||
| 258 | my ($key, $values) = $_->@*; | ||
| 259 | say "Group $key has " . $values->size . " elements"; | ||
| 260 | }); | ||
| 261 | |||
| 262 | # Composite keys come back as arrayrefs | ||
| 263 | my $expanded = $c->group_by([0, 1])->expand; | ||
| 264 | $expanded->each(sub { | ||
| 265 | my ($keys, $values) = $_->@*; | ||
| 266 | say join(", ", @$keys); | ||
| 267 | }); | ||
| 268 | |||
| 269 | =head1 ERRORS | ||
| 270 | |||
| 271 | L<Mojo::Collection::Role::GroupBy> will raise an error if the collection | ||
| 272 | contains mixed element types (arrayrefs, hashrefs, and objects cannot be | ||
| 273 | mixed), or if L</"to_hash"> or L</"expand"> are called on an ungrouped | ||
| 274 | collection without a grouping argument. | ||
| 275 | |||
| 276 | =head1 SEE ALSO | ||
| 277 | |||
| 278 | L<Mojo::Collection>, L<Mojo::Base>, L<Mojo::Collection::Role::GroupBy::Util> | ||
| 279 | |||
| 280 | =head1 AUTHOR | ||
| 281 | |||
| 282 | Simone Cesano <scesano@cpan.org> | ||
| 283 | |||
| 284 | =head1 COPYRIGHT AND LICENSE | ||
| 285 | |||
| 286 | Copyright (C) Simone Cesano. | ||
| 287 | |||
| 288 | This library is free software; you can redistribute it and/or modify | ||
| 289 | it under the same terms as Perl itself. | ||
| 290 | |||
| 291 | =cut |