burninate documentation for DOM::Tiny
[catagits/DOM-Tiny.git] / lib / DOM / Tiny / _Collection.pm
CommitLineData
78ba4051 1package DOM::Tiny::_Collection;
2
3use strict;
4use warnings;
5use Carp 'croak';
6use List::Util;
7use Scalar::Util 'blessed';
8
ba909048 9use constant REDUCE => ($] >= 5.008009 ? \&List::Util::reduce : \&_reduce);
10
22cfa6c8 11our $VERSION = '0.004';
78ba4051 12
78ba4051 13sub new {
14 my $class = shift;
15 return bless [@_], ref $class || $class;
16}
17
18sub TO_JSON { [@{shift()}] }
19
20sub compact {
21 my $self = shift;
22 return $self->new(grep { defined && (ref || length) } @$self);
23}
24
25sub each {
26 my ($self, $cb) = @_;
27 return @$self unless $cb;
28 my $i = 1;
29 $_->$cb($i++) for @$self;
30 return $self;
31}
32
33sub first {
34 my ($self, $cb) = (shift, shift);
35 return $self->[0] unless $cb;
36 return List::Util::first { $_ =~ $cb } @$self if ref $cb eq 'Regexp';
37 return List::Util::first { $_->$cb(@_) } @$self;
38}
39
40sub flatten { $_[0]->new(_flatten(@{$_[0]})) }
41
42sub grep {
43 my ($self, $cb) = (shift, shift);
44 return $self->new(grep { $_ =~ $cb } @$self) if ref $cb eq 'Regexp';
45 return $self->new(grep { $_->$cb(@_) } @$self);
46}
47
48sub join {
2d9f5165 49 join +(defined($_[1]) ? $_[1] : ''), map {"$_"} @{$_[0]};
78ba4051 50}
51
52sub last { shift->[-1] }
53
54sub map {
55 my ($self, $cb) = (shift, shift);
56 return $self->new(map { $_->$cb(@_) } @$self);
57}
58
59sub reduce {
60 my $self = shift;
61 @_ = (@_, @$self);
ba909048 62 goto &{REDUCE()};
78ba4051 63}
64
65sub reverse { $_[0]->new(reverse @{$_[0]}) }
66
67sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }
68
69sub size { scalar @{$_[0]} }
70
71sub slice {
72 my $self = shift;
73 return $self->new(@$self[@_]);
74}
75
76sub sort {
77 my ($self, $cb) = @_;
78
79 return $self->new(sort @$self) unless $cb;
80
81 my $caller = caller;
82 no strict 'refs';
83 my @sorted = sort {
84 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\$a, \$b);
85 $a->$cb($b);
86 } @$self;
87 return $self->new(@sorted);
88}
89
90sub tap {
91 my ($self, $cb) = (shift, shift);
92 $_->$cb(@_) for $self;
93 return $self;
94}
95
96sub to_array { [@{shift()}] }
97
98sub uniq {
99 my ($self, $cb) = (shift, shift);
100 my %seen;
101 return $self->new(grep { !$seen{$_->$cb(@_)}++ } @$self) if $cb;
102 return $self->new(grep { !$seen{$_}++ } @$self);
103}
104
105sub _flatten {
106 map { _ref($_) ? _flatten(@$_) : $_ } @_;
107}
108
ba909048 109# For perl < 5.8.9
110sub _reduce (&@) {
111 my $code = shift;
112
113 return shift unless @_ > 1;
114
115 my $caller = caller;
116
117 no strict 'refs';
118
86ed756d 119 local (*{"${caller}::a"}, *{"${caller}::b"}) = (\my $x, \my $y);
ba909048 120
aeae9512 121 $x = shift;
86ed756d 122 foreach my $e (@_) {
123 $y = $e;
aeae9512 124 $x = $code->();
ba909048 125 }
126
aeae9512 127 $x;
ba909048 128}
129
78ba4051 130sub _ref { ref $_[0] eq 'ARRAY' || blessed $_[0] && $_[0]->isa(__PACKAGE__) }
131
1321;
9a5f1e3f 133
134=for Pod::Coverage *EVERYTHING*
135
136=cut