Filename | /homes/dcw/lib/perl5/DCW/List.pm |
Statements | Executed 10 statements in 749µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 17µs | 20µs | BEGIN@10 | List::
1 | 1 | 1 | 12µs | 38µs | BEGIN@13 | List::
1 | 1 | 1 | 10µs | 16µs | BEGIN@11 | List::
1 | 1 | 1 | 9µs | 15µs | BEGIN@15 | List::
0 | 0 | 0 | 0s | 0s | as_str | List::
0 | 0 | 0 | 0s | 0s | cons | List::
0 | 0 | 0 | 0s | 0s | deepclone | List::
0 | 0 | 0 | 0s | 0s | foreach | List::
0 | 0 | 0 | 0s | 0s | headtail | List::
0 | 0 | 0 | 0s | 0s | isempty | List::
0 | 0 | 0 | 0s | 0s | items | List::
0 | 0 | 0 | 0s | 0s | list | List::
0 | 0 | 0 | 0s | 0s | new | List::
0 | 0 | 0 | 0s | 0s | nillist | List::
0 | 0 | 0 | 0s | 0s | push | List::
0 | 0 | 0 | 0s | 0s | randomitem | List::
0 | 0 | 0 | 0s | 0s | reverse | List::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package List; | ||||
2 | |||||
3 | # | ||||
4 | # simple OO wrapper around perl arrays, giving | ||||
5 | # self-printing lists of anything. | ||||
6 | # | ||||
7 | # usage: use List; (mainly OO) | ||||
8 | # or: use List qw(list nillist); (convenience functions too) | ||||
9 | |||||
10 | 2 | 30µs | 2 | 24µs | # spent 20µs (17+3) within List::BEGIN@10 which was called:
# once (17µs+3µs) by NewBoard::BEGIN@25 at line 10 # spent 20µs making 1 call to List::BEGIN@10
# spent 3µs making 1 call to strict::import |
11 | 2 | 34µs | 2 | 22µs | # spent 16µs (10+6) within List::BEGIN@11 which was called:
# once (10µs+6µs) by NewBoard::BEGIN@25 at line 11 # spent 16µs making 1 call to List::BEGIN@11
# spent 6µs making 1 call to warnings::import |
12 | |||||
13 | 2 | 53µs | 2 | 64µs | # spent 38µs (12+26) within List::BEGIN@13 which was called:
# once (12µs+26µs) by NewBoard::BEGIN@25 at line 13 # spent 38µs making 1 call to List::BEGIN@13
# spent 26µs making 1 call to overload::import |
14 | |||||
15 | 2 | 626µs | 2 | 21µs | # spent 15µs (9+6) within List::BEGIN@15 which was called:
# once (9µs+6µs) by NewBoard::BEGIN@25 at line 15 # spent 15µs making 1 call to List::BEGIN@15
# spent 6µs making 1 call to Exporter::import |
16 | 1 | 2µs | our @EXPORT_OK = qw(list nillist); | ||
17 | |||||
18 | |||||
19 | # exported convenience functions | ||||
20 | sub list (@) { return List->new(@_); } | ||||
21 | sub nillist { return List->new( () ); } | ||||
22 | |||||
23 | |||||
24 | # | ||||
25 | # my $l = List->new( @item ); | ||||
26 | # Create a new List of items @item | ||||
27 | # | ||||
28 | sub new ($@) | ||||
29 | { | ||||
30 | my( $class, @item ) = @_; | ||||
31 | return bless [ @item ], $class; | ||||
32 | } | ||||
33 | |||||
34 | |||||
35 | # | ||||
36 | # my $l->push( @item ); | ||||
37 | # Push @items on the end of $l | ||||
38 | # (destructively modifying $l) | ||||
39 | # | ||||
40 | sub push ($@) | ||||
41 | { | ||||
42 | my( $l, @item ) = @_; | ||||
43 | push @$l, @item; | ||||
44 | } | ||||
45 | |||||
46 | |||||
47 | # | ||||
48 | # my $l->cons( $item ); | ||||
49 | # cons $item on the front of $l | ||||
50 | # (destructively modifying $l) | ||||
51 | # OR | ||||
52 | # my $newlist = List->cons( $head, $tail ) | ||||
53 | # functional cons: create a new List | ||||
54 | # containing the $head followed by all | ||||
55 | # elements in the $tail | ||||
56 | # | ||||
57 | sub cons ($$;$) | ||||
58 | { | ||||
59 | my( $thing, $head, $tail ) = @_; | ||||
60 | unless( defined $tail ) | ||||
61 | { | ||||
62 | die "List->cons: $thing not a list\n" unless | ||||
63 | ref($thing) eq "List"; | ||||
64 | unshift @$thing, $head; | ||||
65 | } else | ||||
66 | { | ||||
67 | return List->new( $head, @$tail ); | ||||
68 | } | ||||
69 | } | ||||
70 | |||||
71 | |||||
72 | # | ||||
73 | # my @item = $l->items; | ||||
74 | # Extract all the items from list $l | ||||
75 | # | ||||
76 | sub items ($) | ||||
77 | { | ||||
78 | my( $self ) = @_; | ||||
79 | return @$self; | ||||
80 | } | ||||
81 | |||||
82 | |||||
83 | # | ||||
84 | # my $isempty = $l->isempty; | ||||
85 | # Is list $l empty? | ||||
86 | # | ||||
87 | sub isempty ($) | ||||
88 | { | ||||
89 | my( $self ) = @_; | ||||
90 | return @$self == 0; | ||||
91 | } | ||||
92 | |||||
93 | |||||
94 | # | ||||
95 | # my( $head, $tail ) = $l->headtail; | ||||
96 | # Without altering $l, break apart | ||||
97 | # the head and tail (creating a new | ||||
98 | # List object for the tail) | ||||
99 | # | ||||
100 | sub headtail ($) | ||||
101 | { | ||||
102 | my( $self ) = @_; | ||||
103 | my @x = @$self; | ||||
104 | die "list->headtail: empty list\n" if @x == 0; | ||||
105 | my $head = shift @x; | ||||
106 | return ( $head, List->new(@x) ); | ||||
107 | } | ||||
108 | |||||
109 | |||||
110 | # | ||||
111 | # my $revlist = $l->reverse; | ||||
112 | # Create a new list containing the same elements | ||||
113 | # as in $l but in reverse order | ||||
114 | # | ||||
115 | sub reverse ($) | ||||
116 | { | ||||
117 | my( $self ) = @_; | ||||
118 | return List->new( reverse @$self ); | ||||
119 | } | ||||
120 | |||||
121 | |||||
122 | # | ||||
123 | # my $item = $l->randomitem; | ||||
124 | # Extract a random item from list $l | ||||
125 | # | ||||
126 | sub randomitem ($) | ||||
127 | { | ||||
128 | my( $self ) = @_; | ||||
129 | return $self->[int(rand(@$self))]; | ||||
130 | } | ||||
131 | |||||
132 | |||||
133 | # | ||||
134 | # my $l->foreach( $cb ); | ||||
135 | # Foreach item in list $l, call $cb(item) | ||||
136 | # | ||||
137 | sub foreach ($$) | ||||
138 | { | ||||
139 | my( $l, $cb ) = @_; | ||||
140 | foreach (@$l) | ||||
141 | { | ||||
142 | $cb->($_); | ||||
143 | } | ||||
144 | } | ||||
145 | |||||
146 | |||||
147 | # | ||||
148 | # my $s = $l->as_str; | ||||
149 | # Stringify a list nicely | ||||
150 | # | ||||
151 | sub as_str ($) | ||||
152 | { | ||||
153 | my( $self ) = @_; | ||||
154 | my @item = @$self; | ||||
155 | my $str = join(",", map { "$_" } @item ); | ||||
156 | return "[$str]"; | ||||
157 | } | ||||
158 | |||||
159 | |||||
160 | # | ||||
161 | # my $newlist = $list->deepclone; | ||||
162 | # deepclone the given list $list, calling each item's deepclone | ||||
163 | # method and building and returning a new identical list with | ||||
164 | # no shared items.. | ||||
165 | # ONLY CALL THIS IF ALL ITEMS ON $list "can" deepclone! | ||||
166 | # | ||||
167 | sub deepclone ($) | ||||
168 | { | ||||
169 | my( $self ) = @_; | ||||
170 | return List->new( map { $_->deepclone } @$self ); | ||||
171 | } | ||||
172 | |||||
173 | |||||
174 | 1 | 4µs | 1; |