| 1 | package Language::Functional; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS $INFINITE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 1 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 12 | 1 | $INFINITE = 8192; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | my @methods = qw(show inc double square cons max min even odd | |||||||||||||||||||||||||||
| 15 | rem quot gcd lcm Until | ||||||||||||||||||||||||||||
| 16 | id const flip fst snd head Last tail init | ||||||||||||||||||||||||||||
| 17 | null Map filter Length concat | ||||||||||||||||||||||||||||
| 18 | foldl foldl1 scanl scanl1 | ||||||||||||||||||||||||||||
| 19 | foldr foldr1 scanr scanr1 | ||||||||||||||||||||||||||||
| 20 | iterate repeat replicate | ||||||||||||||||||||||||||||
| 21 | take drop splitAt takeWhile dropWhile span break | ||||||||||||||||||||||||||||
| 22 | lines words unlines unwords Reverse | ||||||||||||||||||||||||||||
| 23 | And Or any all elem notElem lookup maximum minimum | ||||||||||||||||||||||||||||
| 24 | sum product zip zip3 unzip unzip3 | ||||||||||||||||||||||||||||
| 25 | integers factors prime | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | @EXPORT_OK = @methods; | |||||||||||||||||||||||||||
| 29 | 1 | %EXPORT_TAGS = ('all', => \@methods); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 NAME | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Language::Functional - a module which makes Perl slightly more functional | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | use Language::Functional ':all'; | ||||||||||||||||||||||||||||
| 39 | print 'The first ten primes are: ', | ||||||||||||||||||||||||||||
| 40 | show(take(10, filter { prime(shift) } integers)), "\n"; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Perl already contains some functional-like functions, such as | ||||||||||||||||||||||||||||
| 45 | C<map> and C<grep>. The purpose of this module is to add other | ||||||||||||||||||||||||||||
| 46 | functional-like functions to Perl, such as foldl and foldr, as | ||||||||||||||||||||||||||||
| 47 | well as the use of infinite lists. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Think as to how you would express the first ten prime | ||||||||||||||||||||||||||||
| 50 | numbers in a simple way in your favourite programming | ||||||||||||||||||||||||||||
| 51 | language? So the example in the synopsis is a killer app, | ||||||||||||||||||||||||||||
| 52 | if you will (until I think up a better one ;-). | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The idea is mostly based on Haskell, from which most of the | ||||||||||||||||||||||||||||
| 55 | functions are taken. There are a couple of major omissions: | ||||||||||||||||||||||||||||
| 56 | currying and types. Lists (and tuples) are simply Perl list | ||||||||||||||||||||||||||||
| 57 | references, none of this 'cons' business, and strings are | ||||||||||||||||||||||||||||
| 58 | simple strings, not lists of characters. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | The idea is to make Perl slightly more functional, rather | ||||||||||||||||||||||||||||
| 61 | than completely replace it. Hence, this slots in very well | ||||||||||||||||||||||||||||
| 62 | with whatever else your program may be doing, and is very | ||||||||||||||||||||||||||||
| 63 | Perl-ish. Other modules are expected to try a much more | ||||||||||||||||||||||||||||
| 64 | functional approach. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | The following functions are available. (Note: these should not be | ||||||||||||||||||||||||||||
| 69 | called as methods). | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | In each description, I shall give the Haskell definition | ||||||||||||||||||||||||||||
| 72 | (if I think it would help) as well as a useful example. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =over 4 | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Insert copious amounts of POD documentation here for each | ||||||||||||||||||||||||||||
| 79 | # function... (test.pl will have to do for now) | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub show_old { | ||||||||||||||||||||||||||
|
Subroutine
show_old: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | join ", ", | ||||||||||||||||||||||||||||
| 83 | map { | ||||||||||||||||||||||||||||
| 84 | 0 | my $d = Data::Dumper->new([$_]); | |||||||||||||||||||||||||||
| 85 | 0 | $d->Indent(0)->Terse(1); | |||||||||||||||||||||||||||
| 86 | 0 | $d->Dump; | |||||||||||||||||||||||||||
| 87 | } @_; | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item show | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Show returns a string representation of an object. | ||||||||||||||||||||||||||||
| 94 | It does not like infinite lists. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 136 | ▶ | sub show { | ||||||||||||||||||||||||||
|
Subroutine
show: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 136 | return join ", ", map {show_aux($_)} @_; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 356 | ▶ | sub show_aux { | ||||||||||||||||||||||||||
|
Subroutine
show_aux: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 356 | my $x = shift; | |||||||||||||||||||||||||||
| 104 | 356 | ▶ | if (not defined $x) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 105 | 0 | return 'undef'; | |||||||||||||||||||||||||||
| 106 | } elsif ($x eq '') { | ||||||||||||||||||||||||||||
| 107 | 3 | return "''"; | |||||||||||||||||||||||||||
| 108 | } elsif (not ref $x) { | ||||||||||||||||||||||||||||
| 109 | 301 | ▶ | if ($x =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 110 | 293 | return "$x"; | |||||||||||||||||||||||||||
| 111 | } elsif ($x =~ /^.$/) { | ||||||||||||||||||||||||||||
| 112 | 3 | return "'$x'"; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 5 | $x =~ s|\n|\\n|g; | |||||||||||||||||||||||||||
| 115 | 5 | return '"' . $x . '"'; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } elsif (ref($x) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 118 | # Here we evaluate all values of the array. As this can | ||||||||||||||||||||||||||||
| 119 | # be lazy, and might resize the array, we have to do this | ||||||||||||||||||||||||||||
| 120 | # now. | ||||||||||||||||||||||||||||
| 121 | 52 | ▶ | map { $x->[$_] if $_ < scalar @{$x}} (0..scalar @{$x}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | # return "(Array of size " . scalar @{$x} . ", " . ref($x) . ")" . "[" . show(@{$x}) . "]"; | ||||||||||||||||||||||||||||
| 123 | 50 | return "[" . show(@{$x}) . "]"; | |||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||
| 125 | 0 | return "[ref $x]"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =item inc k | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Increases the value passed by 1. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | $x = inc 2; # 3 | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | In Haskell: | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | inc :: a -> a | ||||||||||||||||||||||||||||
| 139 | inc k = 1 + k | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 10 | ▶ | sub inc($) { | ||||||||||||||||||||||||||
|
Subroutine
inc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 10 | return shift() + 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item double k | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Doubles the passed value. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | $x = double 3; # 6 | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | In Haskell: | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | double :: a -> a | ||||||||||||||||||||||||||||
| 157 | double k = k * 2 | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 7 | ▶ | sub double($) { | ||||||||||||||||||||||||||
|
Subroutine
double: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 7 | return shift() * 2; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item square k | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Returns the square of the passed value. eg: | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | $x = square 3; # 9 | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | In Haskell: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | square :: a -> a | ||||||||||||||||||||||||||||
| 175 | square k = k * k | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 11 | ▶ | sub square($) { | ||||||||||||||||||||||||||
|
Subroutine
square: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 11 | return shift() ** 2; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 1 | ▶ | sub cons { | ||||||||||||||||||||||||||
|
Subroutine
cons: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | 1 | unshift @{$_[1]}, $_[0]; | |||||||||||||||||||||||||||
| 185 | 1 | return ($_[1]); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 6 | ▶ | sub min($$) { | ||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=8.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 6 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 190 | 6 | ▶ | return $x if $x < $y; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | return $y; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 9 | ▶ | sub max($$) { | ||||||||||||||||||||||||||
|
Subroutine
max: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 195 | 9 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 196 | 9 | ▶ | return $x if $x > $y; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 9 | return $y; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 8233 | ▶ | sub even($) { | ||||||||||||||||||||||||||
|
Subroutine
even: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 201 | 8233 | my $x = shift; | |||||||||||||||||||||||||||
| 202 | 8233 | return not $x % 2; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 8223 | ▶ | sub odd($) { | ||||||||||||||||||||||||||
|
Subroutine
odd: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 8223 | my $x = shift; | |||||||||||||||||||||||||||
| 207 | 8222 | return not even($x); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 6 | ▶ | sub rem($$) { | ||||||||||||||||||||||||||
|
Subroutine
rem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | 6 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 212 | 6 | return $x % $y; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 1 | ▶ | sub quot($$) { | ||||||||||||||||||||||||||
|
Subroutine
quot: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 216 | 1 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 217 | 1 | return int($x/$y); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =item gcd x y | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | Returns the greatest common denominator of two | ||||||||||||||||||||||||||||
| 224 | numbers. eg: | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | $x = gcd(144, 1024); # 16 | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | In Haskell: | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | gcd :: Integral a => a -> a -> a | ||||||||||||||||||||||||||||
| 231 | gcd 0 0 = error "gcd 0 0 is undefined" | ||||||||||||||||||||||||||||
| 232 | gcd x y = gcd' (abs x) (abs y) | ||||||||||||||||||||||||||||
| 233 | where gcd' x 0 = x | ||||||||||||||||||||||||||||
| 234 | gcd' x y = gcd' y (x `rem` y) | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 2 | ▶ | sub gcd($$) { | ||||||||||||||||||||||||||
|
Subroutine
gcd: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | 2 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 240 | 2 | ▶ | croak "gcd(0, 0) is undefined!" if ($x == 0 and $y == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$x == 0 and $y == 0
MC/DC0%0/2
$x == 0 and $y == 0
$x == 0
$y == 0
Truth table33%1/3
A and B
A= $x == 0B= $y == 0
| |||||||||||||||||||||||||||||
| 241 | 2 | return gcd_aux(abs $x, abs $y); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | sub gcd_aux($$); | ||||||||||||||||||||||||||||
| 245 | 8 | ▶ | sub gcd_aux($$) { | ||||||||||||||||||||||||||
|
Subroutine
gcd_aux: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 246 | 8 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 247 | 8 | ▶ | return $x if $y == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 6 | return gcd_aux($y, rem($x, $y)); | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =item lcm x y | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Returns the lowest common multiple of two numbers. | ||||||||||||||||||||||||||||
| 255 | eg: | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | $x = lcm(144, 1024); # 9216 | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | In Haskell: | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | lcm :: (Integral a) => a -> a -> a | ||||||||||||||||||||||||||||
| 262 | lcm _ 0 = 0 | ||||||||||||||||||||||||||||
| 263 | lcm 0 _ = 0 | ||||||||||||||||||||||||||||
| 264 | lcm x y = abs ((x `quot` gcd x y) * y) | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 1 | ▶ | sub lcm($$) { | ||||||||||||||||||||||||||
|
Subroutine
lcm: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 1 | my($x, $y) = @_; | |||||||||||||||||||||||||||
| 270 | 1 | ▶ | return 0 if $x == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 1 | ▶ | return 0 if $y == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 1 | return abs((quot($x,gcd($x, $y))) * $y); | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =item id x | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | The identity function - simply returns the argument. | ||||||||||||||||||||||||||||
| 279 | eg: | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | $x = id([1..6]); # [1, 2, 3, 4, 5, 6]. | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | In Haskell: | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | id :: a -> a | ||||||||||||||||||||||||||||
| 286 | id x = x | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 1 | ▶ | sub id { | ||||||||||||||||||||||||||
|
Subroutine
id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 1 | my @values = @_; | |||||||||||||||||||||||||||
| 292 | 1 | return @values; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =item const k _ | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Returns the first argument of 2 arguments. eg: | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | $x = const(4, 5); # 4 | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | In Haskell: | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | const :: a -> b -> a | ||||||||||||||||||||||||||||
| 305 | const k _ = k | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1 | ▶ | sub const { | ||||||||||||||||||||||||||
|
Subroutine
const: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 310 | 1 | my $x = shift; | |||||||||||||||||||||||||||
| 311 | 1 | return $x; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =item flip f | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | Given a function, flips the two arguments it is passed. | ||||||||||||||||||||||||||||
| 318 | Note that this returns a CODEREF, as currying does not yet | ||||||||||||||||||||||||||||
| 319 | happen. eg: flip(sub { $_[0] ** $_[1] })->(2, 3) = 9. | ||||||||||||||||||||||||||||
| 320 | In Haskell (ie this is what it should really do): | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | flip :: (a -> b -> c) -> b -> a -> c | ||||||||||||||||||||||||||||
| 323 | flip f x y = f y x | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =cut | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 1 | ▶ | sub flip { | ||||||||||||||||||||||||||
|
Subroutine
flip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 328 | 1 | my $f = shift; | |||||||||||||||||||||||||||
| 329 | 1 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 330 | 1 | $f->($_[1], $_[0]); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 1 | } | |||||||||||||||||||||||||||
| 333 | # flip f x y -> f y x can't be done as | ||||||||||||||||||||||||||||
| 334 | # this isn't yet lazy or curried! | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =item Until p f x | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Keep on applying f to x until p(x) is true, and | ||||||||||||||||||||||||||||
| 340 | then return x at that point. eg: | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | $x = Until { shift() % 10 == 0 } \&inc, 1; # 10 | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | In Haskell: | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | until :: (a -> Bool) -> (a -> a) -> a -> a | ||||||||||||||||||||||||||||
| 347 | until p f x = if p x then x else until p f (f x) | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | sub Until(&&$); | ||||||||||||||||||||||||||||
| 352 | 10 | ▶ | sub Until(&&$) { | ||||||||||||||||||||||||||
|
Subroutine
Until: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 353 | 10 | my($p, $f, $x) = @_; | |||||||||||||||||||||||||||
| 354 | 10 | ▶ | return $x if $p->($x); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 9 | return Until(\&$p, \&$f, $f->($x)); | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =item fst x:xs | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | Returns the first element in a tuple. eg: | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | $x = fst([1, 2]); # 1 | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | In Haskell: | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | fst :: (a,b) -> a | ||||||||||||||||||||||||||||
| 368 | fst (x,_) = x | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =cut | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 1 | ▶ | sub fst($) { | ||||||||||||||||||||||||||
|
Subroutine
fst: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 1 | my $x = shift; | |||||||||||||||||||||||||||
| 374 | 1 | return $x->[0]; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =item snd x:y:xs | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Returns the second element in a tuple. eg: | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | $x = snd([1, 2]); # 2 | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | In Haskell: | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | snd :: (a,b) -> a | ||||||||||||||||||||||||||||
| 387 | snd (_,y) = y | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =cut | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 1 | ▶ | sub snd($) { | ||||||||||||||||||||||||||
|
Subroutine
snd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 392 | 1 | my $x = shift; | |||||||||||||||||||||||||||
| 393 | 1 | return $x->[1]; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =item head xs | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | Returns the head (first element) of a list. eg: | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | $x = head([1..6]); # 1 | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | In Haskell: | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | head :: [a] -> a | ||||||||||||||||||||||||||||
| 406 | head (x:_) = x | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =cut | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 2 | ▶ | sub head($) { | ||||||||||||||||||||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 411 | 2 | my $xs = shift; | |||||||||||||||||||||||||||
| 412 | 2 | return $xs->[0]; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =item Last xs | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Returns the last element of a list. Note the capital L, to make it | ||||||||||||||||||||||||||||
| 419 | distinct from the Perl 'last' command. eg: | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | $x = Last([1..6]); # 6 | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | In Haskell: | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | last :: [a] -> a | ||||||||||||||||||||||||||||
| 426 | last [x] = x | ||||||||||||||||||||||||||||
| 427 | last (_:xs) = last xs | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =cut | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 2 | ▶ | sub Last($) { | ||||||||||||||||||||||||||
|
Subroutine
Last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 432 | 2 | my $xs = shift; | |||||||||||||||||||||||||||
| 433 | 2 | return $xs->[-1]; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =item tail xs | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Returns a list minus the first element (head). eg: | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | $x = tail([1..6]); # [2, 3, 4, 5, 6] | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | In Haskell: | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | tail :: [a] -> [a] | ||||||||||||||||||||||||||||
| 446 | tail (_:xs) = xs | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 3 | ▶ | sub tail($) { | ||||||||||||||||||||||||||
|
Subroutine
tail: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 451 | 3 | my $xs = shift; | |||||||||||||||||||||||||||
| 452 | 3 | my $len = scalar @{$xs}; | |||||||||||||||||||||||||||
| 453 | 3 | ▶ | $len = $len == $INFINITE ? $len : $len - 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | 8201 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 455 | 8201 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 456 | 8201 | return $xs->[$idx+1]; | |||||||||||||||||||||||||||
| 457 | 3 | }, $len; | |||||||||||||||||||||||||||
| 458 | 3 | return \@a; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =item init xs | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | Returns a list minus its last element. eg: | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | $x = init([1..6]); # [1, 2, 3, 4, 5] | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | In Haskell: | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | init :: [a] -> [a] | ||||||||||||||||||||||||||||
| 471 | init [x] = [] | ||||||||||||||||||||||||||||
| 472 | init (x:xs) = x : init xs | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =cut | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 2 | ▶ | sub init($) { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 477 | 2 | my $xs = shift; | |||||||||||||||||||||||||||
| 478 | 2 | pop(@{$xs}); | |||||||||||||||||||||||||||
| 479 | 1 | return $xs; | |||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =item null xs | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | Returns whether or not the list is empty. eg: | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | $x = null([1, 2]); # False | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | In Haskell: | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | null :: [a] -> Bool | ||||||||||||||||||||||||||||
| 492 | null [] = True | ||||||||||||||||||||||||||||
| 493 | null (_:_) = False | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =cut | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 3 | ▶ | sub null($) { | ||||||||||||||||||||||||||
|
Subroutine
null: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 498 | 3 | my $x = shift; | |||||||||||||||||||||||||||
| 499 | 3 | return not @{$x}; | |||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | =item Map f xs | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | Evaluates f for each element of the list xs and returns the list | ||||||||||||||||||||||||||||
| 506 | composed of the results of each such evaluation. It is very similar to | ||||||||||||||||||||||||||||
| 507 | the Perl command 'map', hence the capital M, but also copes with | ||||||||||||||||||||||||||||
| 508 | infinite lists. eg: | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | $x = Map { double(shift) } [1..6]; # [2, 4, 6, 8, 10, 12] | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | In Haskell: | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | map :: (a -> b) -> [a] -> [b] | ||||||||||||||||||||||||||||
| 515 | map f xs = [ f x | x <- xs ] | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =cut | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 2 | ▶ | sub Map(&$) { | ||||||||||||||||||||||||||
|
Subroutine
Map: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 520 | 2 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 521 | 16 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 522 | 16 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 523 | 16 | return $f->($xs->[$idx]); | |||||||||||||||||||||||||||
| 524 | 2 | }, scalar @{$xs}; | |||||||||||||||||||||||||||
| 525 | 2 | return \@a; | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | =item filter p xs | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | Returns the list of the elements in xs for which | ||||||||||||||||||||||||||||
| 532 | p(xs) returns true. It is similar to the Perl command | ||||||||||||||||||||||||||||
| 533 | 'grep', but it also copes with infinite lists. eg: | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | $x = filter(\&even, [1..6]); # [2, 4, 6] | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | In Haskell: | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | filter :: (a -> Bool) -> [a] -> [a] | ||||||||||||||||||||||||||||
| 540 | filter p xs = [ x | x <- xs, p x ] | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =cut | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | # Ha! Before infinite lists simply consisted of: | ||||||||||||||||||||||||||||
| 545 | # return [grep { $f->($_) } @{$xs}]; | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 5 | ▶ | sub filter(&$) { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.5
Pod
documented
| |||||||||||||||||||||||||||||
| 548 | 5 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 549 | 5 | my $pointer = -1; | |||||||||||||||||||||||||||
| 550 | 4125 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=33.7
| |||||||||||||||||||||||||||||
| 551 | 4125 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 552 | 4125 | my $debug = 0; | |||||||||||||||||||||||||||
| 553 | 4125 | ▶ | print "$idx: in (done $pointer)\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | 4125 | ▶ | if ($pointer eq $INFINITE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 555 | 0 | die "Fetching an infinite amount of values in filter()!\n"; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | 4125 | ▶ | if ($idx - 1 > $pointer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 558 | 0 | ▶ | print "$idx: doing $array->FETCH for $pointer..", $idx - 1, "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | 0 | ▶ | map { $array->FETCH($_) if $_ < $array->FETCHSIZE} ($pointer..$idx-1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | 4125 | ▶ | if ($idx > $array->FETCHSIZE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 562 | 0 | ▶ | print "$idx: in: silly, getting out\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 0 | return undef; | |||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||
| 565 | 4125 | while (1) { | |||||||||||||||||||||||||||
| 566 | 8254 | $pointer++; | |||||||||||||||||||||||||||
| 567 | 8254 | ▶ | print "$idx: loop: $idx (done $pointer/", $array->FETCHSIZE, ") = ", $f->($xs->[$pointer]), "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 568 | 8254 | ▶ | if ($pointer >= $array->FETCHSIZE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | 2 | ▶ | print "$idx: Size *was* ", $array->FETCHSIZE, "!\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 570 | 2 | $array->STORESIZE($idx); | |||||||||||||||||||||||||||
| 571 | 2 | ▶ | print "$idx: Set size to ", $array->FETCHSIZE, "!\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 572 | 2 | last; | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | 8252 | ▶ | if ($f->($xs->[$pointer])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 575 | 4122 | ▶ | print "$idx: oooh (elt $pointer: '", $xs->[$pointer], "' was true)\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | 4122 | last; | |||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | 4124 | ▶ | print "$idx: loop: out\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 4124 | return $xs->[$pointer]; | |||||||||||||||||||||||||||
| 582 | 5 | }, scalar @{$xs}; | |||||||||||||||||||||||||||
| 583 | 5 | return \@a; | |||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | =item concat | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | Concatenates lists together into one list. eg: | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | concat([[1..3], [4..6]]); # [1, 2, 3, 4, 5, 6] | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | In Haskell: | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | concat :: [[a]] -> [a] | ||||||||||||||||||||||||||||
| 596 | concat = foldr (++) [] | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | TODO: Make sure this works with infinite lists! | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | =cut | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 1 | ▶ | sub concat($) { | ||||||||||||||||||||||||||
|
Subroutine
concat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 603 | 1 | my($xxs) = shift; | |||||||||||||||||||||||||||
| 604 | 1 | ▶ | return foldr(sub { [@{shift()}, @{shift()}]; }, [], $xxs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | =item Length | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | Returns the length of a list - only do this with | ||||||||||||||||||||||||||||
| 611 | finite lists! eg: | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | $x = Length([1..6]); # 6 | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | In Haskell: | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | length :: [a] -> Int | ||||||||||||||||||||||||||||
| 618 | length = foldl' (\n _ -> n + 1) 0 | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | TODO Make sure this works! | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =cut #' | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 37 | ▶ | sub Length($) { | ||||||||||||||||||||||||||
|
Subroutine
Length: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 625 | 37 | my $xs = shift; | |||||||||||||||||||||||||||
| 626 | 37 | my $len = scalar @{$xs}; | |||||||||||||||||||||||||||
| 627 | 37 | ▶ | confess "Fetching the length of an infinite list!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 628 | if $len == $INFINITE; | ||||||||||||||||||||||||||||
| 629 | 36 | return $len; | |||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | =item foldl f z xs | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | Applies function f to the pairs (z, xs[0]), (f(z, xs[0], xs[1]), | ||||||||||||||||||||||||||||
| 636 | (f(f(z, xs[0], xs[1])), xs[2]) and so on. ie it folds from the left | ||||||||||||||||||||||||||||
| 637 | and returns the last value. Note that foldl should not be done to | ||||||||||||||||||||||||||||
| 638 | infinite lists. eg: the following returns the sum of 1..6: | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | $x = foldl { shift() + shift() } 0, [1..6]; # 21 | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | In Haskell: | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | foldl :: (a -> b -> a) -> a -> [b] -> a | ||||||||||||||||||||||||||||
| 645 | foldl f z [] = z | ||||||||||||||||||||||||||||
| 646 | foldl f z (x:xs) = foldl f (f z x) xs | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | =cut | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 8 | ▶ | sub foldl(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
foldl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 651 | 8 | my($f, $z, $xs) = @_; | |||||||||||||||||||||||||||
| 652 | 8 | map { $z = $f->($z, $_) } @{$xs}; | |||||||||||||||||||||||||||
| 653 | 8 | return $z; | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | =item foldl1 f xs | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | This is a variant of foldl where the first value of | ||||||||||||||||||||||||||||
| 660 | xs is taken as z. Applies function f to the pairs (xs[0], xs[1]), | ||||||||||||||||||||||||||||
| 661 | (f(xs[0], xs[1], xs[2]), (f(f(xs[0], xs[1], xs[2])), xs[3]) and | ||||||||||||||||||||||||||||
| 662 | so on. ie it folds from the left and returns the last value. | ||||||||||||||||||||||||||||
| 663 | Note that foldl should not be | ||||||||||||||||||||||||||||
| 664 | done to infinite lists. eg: the following returns the sum | ||||||||||||||||||||||||||||
| 665 | of 1..6: | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | $x = foldl1 { shift() + shift() } [1..6]; # 21 | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | In Haskell: | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | foldl1 :: (a -> a -> a) -> [a] -> a | ||||||||||||||||||||||||||||
| 672 | foldl1 f (x:xs) = foldl f x xs | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =cut | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | 6 | ▶ | sub foldl1(&$) { | ||||||||||||||||||||||||||
|
Subroutine
foldl1: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 677 | 6 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 678 | 6 | my $z = shift @{$xs}; | |||||||||||||||||||||||||||
| 679 | 4 | return foldl(\&$f, $z, $xs); | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =item scanl f q xs | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | Returns a list of all the intermedia values that foldl would compute. | ||||||||||||||||||||||||||||
| 686 | ie returns the list z, f(z, xs[0]), f(f(z, xs[0]), xs[1]), f(f(f(z, | ||||||||||||||||||||||||||||
| 687 | xs[0]), xs[1]), xs[2]) and so on. eg: | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | $x = scanl { shift() + shift() }, 0, [1..6]; # [0, 1, 3, 6, 10, 15, 21] | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | In Haskell: | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | scanl :: (a -> b -> a) -> a -> [b] -> [a] | ||||||||||||||||||||||||||||
| 694 | scanl f q xs = q : (case xs of | ||||||||||||||||||||||||||||
| 695 | [] -> [] | ||||||||||||||||||||||||||||
| 696 | x:xs -> scanl f (f q x) xs) | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | =cut | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | 3 | ▶ | sub scanl(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
scanl: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||||||||||||||||||||
| 701 | 3 | my($f, $q, $xs) = @_; | |||||||||||||||||||||||||||
| 702 | # Ha! Before infinite lists simply consisted of the elegant: | ||||||||||||||||||||||||||||
| 703 | # my @return = $q; | ||||||||||||||||||||||||||||
| 704 | # map { $q = $f->($q, $_); push @return, $q } @{$xs}; | ||||||||||||||||||||||||||||
| 705 | # return [@return]; | ||||||||||||||||||||||||||||
| 706 | 3 | my $pointer = -1; | |||||||||||||||||||||||||||
| 707 | 19 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=28.5
| |||||||||||||||||||||||||||||
| 708 | 19 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 709 | 19 | my $debug = 0; | |||||||||||||||||||||||||||
| 710 | 19 | ▶ | print "$idx: in (done $pointer)\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 711 | 19 | ▶ | if ($idx == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 712 | 3 | ▶ | print "$idx: zero, easy = $q!\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 713 | 3 | return $q; | |||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||
| 715 | 16 | ▶ | if ($pointer eq $INFINITE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 716 | 0 | die "Fetching an infinite amount of values in filter()!\n"; | |||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||
| 718 | 16 | ▶ | if ($idx - 1 > $pointer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 719 | 16 | ▶ | print "$idx: doing $array->FETCH for $pointer..", $idx - 1, "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 720 | 16 | ▶ | map { $array->FETCH($_) if $_ < $array->FETCHSIZE} ($pointer..$idx-1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||
| 722 | 16 | ▶ | if ($idx > $array->FETCHSIZE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 723 | 0 | ▶ | print "$idx: in: silly, getting out\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 724 | 0 | return undef; | |||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||
| 726 | 16 | $pointer++; | |||||||||||||||||||||||||||
| 727 | 16 | ▶ | print "$idx: getting f(idx $idx-1, ", $xs->[$idx-1], "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 728 | 16 | my $return = $f->($array->FETCH($idx-1), $xs->[$idx-1]); | |||||||||||||||||||||||||||
| 729 | 16 | ▶ | print "$idx: out with $return\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 730 | 16 | return $return; | |||||||||||||||||||||||||||
| 731 | 3 | }, scalar @{$xs} + 1; | |||||||||||||||||||||||||||
| 732 | 3 | return \@a; | |||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||
| 736 | =item scanl1 f xs | ||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | This is a variant of scanl where the first value of xs is taken as | ||||||||||||||||||||||||||||
| 739 | q. Returns a list of all the intermedia values that foldl would | ||||||||||||||||||||||||||||
| 740 | compute. ie returns the list f(xs[0], xs[1]), f(f(xs[0], xs[1]), | ||||||||||||||||||||||||||||
| 741 | xs[2]), f(f(f(xs[0], xs[1]), xs[2]), xs[3]) and so on. eg: | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | $x = scanl1 { shift() + shift() } [1..6]; # [1, 3, 6, 10, 15, 21] | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | In Haskell: | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | scanl1 :: (a -> a -> a) -> [a] -> [a] | ||||||||||||||||||||||||||||
| 748 | scanl1 f (x:xs) = scanl f x xs | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | =cut | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | 3 | ▶ | sub scanl1(&$) { | ||||||||||||||||||||||||||
|
Subroutine
scanl1: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 753 | 3 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 754 | 3 | my $z = shift @{$xs}; | |||||||||||||||||||||||||||
| 755 | 2 | return scanl(\&$f, $z, $xs); | |||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | =item foldr f z xs | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | This is similar to foldl but is folding from the right instead of the | ||||||||||||||||||||||||||||
| 762 | left. Note that foldr should not be done to infinite lists. eg: the | ||||||||||||||||||||||||||||
| 763 | following returns the sum of 1..6 | ||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||
| 765 | $x = foldr { shift() + shift() } 0, [1..6] ; # 21 | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | In Haskell: | ||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | foldr :: (a -> b -> b) -> b -> [a] -> b | ||||||||||||||||||||||||||||
| 770 | foldr f z [] = z | ||||||||||||||||||||||||||||
| 771 | foldr f z (x:xs) = f x (foldr f z xs) | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | =cut | ||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||
| 775 | 5 | ▶ | sub foldr(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
foldr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 776 | 5 | my($f, $z, $xs) = @_; | |||||||||||||||||||||||||||
| 777 | 5 | map { $z = $f->($_, $z) } reverse @{$xs}; | |||||||||||||||||||||||||||
| 778 | 5 | return $z; | |||||||||||||||||||||||||||
| 779 | } | ||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | =item foldr1 f xs | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | This is similar to foldr1 but is folding from the right instead of the | ||||||||||||||||||||||||||||
| 785 | left. Note that foldr1 should not be done on infinite lists. eg: | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | $x = foldr1 { shift() + shift() } [1..6]; # 21 | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | In Haskell: | ||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||
| 791 | foldr1 :: (a -> a -> a) -> [a] -> a | ||||||||||||||||||||||||||||
| 792 | foldr1 f [x] = x | ||||||||||||||||||||||||||||
| 793 | foldr1 f (x:xs) = f x (foldr1 f xs) | ||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | =cut | ||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | 3 | ▶ | sub foldr1(&$) { | ||||||||||||||||||||||||||
|
Subroutine
foldr1: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 798 | 3 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 799 | 3 | my $z = pop @{$xs}; | |||||||||||||||||||||||||||
| 800 | 3 | return foldr(\&$f, $z, $xs); | |||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||
| 804 | =item scanr f z xs | ||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||
| 806 | This is similar to scanl but is scanning and folding | ||||||||||||||||||||||||||||
| 807 | from the right instead of the left. Note that scanr should | ||||||||||||||||||||||||||||
| 808 | not be done on infinite lists. eg: | ||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||
| 810 | $x = scanr { shift() + shift() } 0, [1..6]; | ||||||||||||||||||||||||||||
| 811 | # [0, 6, 11, 15, 18, 20, 21] | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | In Haskell: | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | scanr :: (a -> b -> b) -> b -> [a] -> [b] | ||||||||||||||||||||||||||||
| 816 | scanr f q0 [] = [q0] | ||||||||||||||||||||||||||||
| 817 | scanr f q0 (x:xs) = f x q : qs | ||||||||||||||||||||||||||||
| 818 | where qs@(q:_) = scanr f q0 xs | ||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||
| 820 | =cut | ||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | 2 | ▶ | sub scanr(&$$) { | ||||||||||||||||||||||||||
|
Subroutine
scanr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 823 | 2 | my($f, $z, $xs) = @_; | |||||||||||||||||||||||||||
| 824 | 2 | my @return = $z; | |||||||||||||||||||||||||||
| 825 | 2 | map { $z = $f->($_, $z); push @return, $z; } reverse @{$xs}; | |||||||||||||||||||||||||||
| 826 | 2 | return [@return]; | |||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | =item scanr1 f xs | ||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||
| 832 | This is similar to scanl1 but is scanning and folding | ||||||||||||||||||||||||||||
| 833 | from the right instead of the left. Note that scanr1 should | ||||||||||||||||||||||||||||
| 834 | not be done on infinite lists. eg: | ||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||
| 836 | $x = scanr1 { shift() + shift() } [1..6]; | ||||||||||||||||||||||||||||
| 837 | # [6, 11, 15, 18, 20, 21] | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | In Haskell: | ||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||
| 841 | scanr1 :: (a -> a -> a) -> [a] -> [a] | ||||||||||||||||||||||||||||
| 842 | scanr1 f [x] = [x] | ||||||||||||||||||||||||||||
| 843 | scanr1 f (x:xs) = f x q : qs | ||||||||||||||||||||||||||||
| 844 | where qs@(q:_) = scanr1 f xs | ||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||
| 846 | =cut | ||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||
| 848 | 1 | ▶ | sub scanr1(&$) { | ||||||||||||||||||||||||||
|
Subroutine
scanr1: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 849 | 1 | my($f, $xs) = @_; | |||||||||||||||||||||||||||
| 850 | 1 | my $z = pop @{$xs}; | |||||||||||||||||||||||||||
| 851 | 1 | return scanr(\&$f, $z, $xs); | |||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||
| 855 | =item iterate f x | ||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||
| 857 | This returns the infinite list (x, f(x), f(f(x)), f(f(f(x)))...) and | ||||||||||||||||||||||||||||
| 858 | so on. eg: | ||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||
| 860 | $x = take(8, iterate { shift() * 2 } 1); | ||||||||||||||||||||||||||||
| 861 | # [1, 2, 4, 8, 16, 32, 64, 128] | ||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||
| 863 | In Haskell: | ||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||
| 865 | iterate :: (a -> a) -> a -> [a] | ||||||||||||||||||||||||||||
| 866 | iterate f x = x : iterate f (f x) | ||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | =cut | ||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||
| 870 | 27 | ▶ | sub iterate(&$) { | ||||||||||||||||||||||||||
|
Subroutine
iterate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 871 | 27 | my($f, $x) = @_; | |||||||||||||||||||||||||||
| 872 | 24709 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 873 | 24709 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 874 | 24709 | ▶ | return $x if $idx == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 875 | 24688 | return $f->($array->FETCH($idx-1)); | |||||||||||||||||||||||||||
| 876 | 27 | }; | |||||||||||||||||||||||||||
| 877 | 27 | return \@a; | |||||||||||||||||||||||||||
| 878 | } | ||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | =item repeat x | ||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||
| 883 | This returns the infinite list where all | ||||||||||||||||||||||||||||
| 884 | elements are x. eg: | ||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||
| 886 | $x = take(4, repeat(42)); # [42, 42, 42, 42]. | ||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||
| 888 | In Haskell: | ||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||
| 890 | repeat :: a -> [a] | ||||||||||||||||||||||||||||
| 891 | repeat x = xs where xs = x:xs | ||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||
| 893 | =cut | ||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||
| 895 | 2 | ▶ | sub repeat($) { | ||||||||||||||||||||||||||
|
Subroutine
repeat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 896 | 2 | my $x = shift; | |||||||||||||||||||||||||||
| 897 | 9 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 898 | 9 | return $x; | |||||||||||||||||||||||||||
| 899 | 2 | }; | |||||||||||||||||||||||||||
| 900 | 2 | return \@a; | |||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||
| 904 | =item replicate n x | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | Returns a list containing n times the element x. eg: | ||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||
| 908 | $x = replicate(5, 1); # [1, 1, 1, 1, 1] | ||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||
| 910 | In Haskell: | ||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||
| 912 | replicate :: Int -> a -> [a] | ||||||||||||||||||||||||||||
| 913 | replicate n x = take n (repeat x) | ||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||
| 915 | =cut | ||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | 1 | ▶ | sub replicate($$) { | ||||||||||||||||||||||||||
|
Subroutine
replicate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 918 | 1 | my($n, $x) = @_; | |||||||||||||||||||||||||||
| 919 | 1 | return take($n, repeat($x)); | |||||||||||||||||||||||||||
| 920 | } | ||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||
| 922 | # TODO | ||||||||||||||||||||||||||||
| 923 | # cycle :: [a] -> [a] | ||||||||||||||||||||||||||||
| 924 | # cycle [] = error "Prelude.cycle: empty list" | ||||||||||||||||||||||||||||
| 925 | # cycle xs = xs' where xs'=xs++xs' | ||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||
| 928 | =item take n xs | ||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||
| 930 | Returns a list containing the first n elements from the list xs. eg: | ||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||
| 932 | $x = take(2, [1..6]); # [1, 2] | ||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||
| 934 | In Haskell: | ||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||
| 936 | take :: Int -> [a] -> [a] | ||||||||||||||||||||||||||||
| 937 | take 0 _ = [] | ||||||||||||||||||||||||||||
| 938 | take _ [] = [] | ||||||||||||||||||||||||||||
| 939 | take n (x:xs) | n>0 = x : take (n-1) xs | ||||||||||||||||||||||||||||
| 940 | take _ _ = error "Prelude.take: negative argument" | ||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||
| 942 | =cut | ||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||
| 944 | 16 | ▶ | sub take($$) { | ||||||||||||||||||||||||||
|
Subroutine
take: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 945 | 16 | my($n, $xs) = @_; | |||||||||||||||||||||||||||
| 946 | 16 | my @return; | |||||||||||||||||||||||||||
| 947 | 16 | foreach my $i (0..$n-1) { | |||||||||||||||||||||||||||
| 948 | 114 | push @return, $xs->[$i]; | |||||||||||||||||||||||||||
| 949 | } | ||||||||||||||||||||||||||||
| 950 | 16 | return \@return; | |||||||||||||||||||||||||||
| 951 | } | ||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||
| 953 | |||||||||||||||||||||||||||||
| 954 | =item drop n xs | ||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||
| 956 | Returns a list containing xs with the first n elements missing. eg: | ||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||
| 958 | $x = drop(2, [1..6]); # [3, 4, 5, 6] | ||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | In Haskell: | ||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||
| 962 | drop :: Int -> [a] -> [a] | ||||||||||||||||||||||||||||
| 963 | drop 0 xs = xs | ||||||||||||||||||||||||||||
| 964 | drop _ [] = [] | ||||||||||||||||||||||||||||
| 965 | drop n (_:xs) | n>0 = drop (n-1) xs | ||||||||||||||||||||||||||||
| 966 | drop _ _ = error "Prelude.drop: negative argument" | ||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||
| 968 | =cut | ||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||
| 970 | 2 | ▶ | sub drop($$) { | ||||||||||||||||||||||||||
|
Subroutine
drop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 971 | 2 | my($n, $xs) = @_; | |||||||||||||||||||||||||||
| 972 | # Ha! Before infinite lists simply consisted of: | ||||||||||||||||||||||||||||
| 973 | # return [splice @{$xs}, $n]; | ||||||||||||||||||||||||||||
| 974 | 2 | my $len = scalar @{$xs}; | |||||||||||||||||||||||||||
| 975 | 2 | ▶ | $len = $len == $INFINITE ? $len : $len - $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 976 | 8 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 977 | 8 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 978 | 8 | return $xs->[$idx+$n]; | |||||||||||||||||||||||||||
| 979 | 2 | }, $len; | |||||||||||||||||||||||||||
| 980 | 2 | return \@a; | |||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||
| 984 | =item splitAt n xs | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | Splits the list xs into two lists at element n. eg: | ||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||
| 988 | $x = splitAt(2, [1..6]);# [[1, 2], [3, 4, 5, 6]] | ||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||
| 990 | In Haskell: | ||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||
| 992 | splitAt :: Int -> [a] -> ([a], [a]) | ||||||||||||||||||||||||||||
| 993 | splitAt 0 xs = ([],xs) | ||||||||||||||||||||||||||||
| 994 | splitAt _ [] = ([],[]) | ||||||||||||||||||||||||||||
| 995 | splitAt n (x:xs) | n>0 = (x:xs',xs'') where (xs',xs'') = splitAt (n-1) xs | ||||||||||||||||||||||||||||
| 996 | splitAt _ _ = error "Prelude.splitAt: negative argument" | ||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||
| 998 | =cut | ||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||
| 1000 | 1 | ▶ | sub splitAt($$) { | ||||||||||||||||||||||||||
|
Subroutine
splitAt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1001 | 1 | my($n, $xs) = @_; | |||||||||||||||||||||||||||
| 1002 | 1 | return [take($n, $xs), drop($n, $xs)]; | |||||||||||||||||||||||||||
| 1003 | } | ||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||
| 1005 | |||||||||||||||||||||||||||||
| 1006 | =item takeWhile p xs | ||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||
| 1008 | Takes elements from xs while p(that element) is | ||||||||||||||||||||||||||||
| 1009 | true. Returns the list. eg: | ||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||
| 1011 | $x = takeWhile { shift() <= 4 } [1..6]; # [1, 2, 3, 4] | ||||||||||||||||||||||||||||
| 1012 | |||||||||||||||||||||||||||||
| 1013 | In Haskell: | ||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||
| 1015 | takeWhile :: (a -> Bool) -> [a] -> [a] | ||||||||||||||||||||||||||||
| 1016 | takeWhile p [] = [] | ||||||||||||||||||||||||||||
| 1017 | takeWhile p (x:xs) | ||||||||||||||||||||||||||||
| 1018 | | p x = x : takeWhile p xs | ||||||||||||||||||||||||||||
| 1019 | | otherwise = [] | ||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||
| 1021 | =cut | ||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||
| 1023 | 4 | ▶ | sub takeWhile(&$) { | ||||||||||||||||||||||||||
|
Subroutine
takeWhile: called CC=1 SCAR=0.7
Pod
documented
| |||||||||||||||||||||||||||||
| 1024 | 4 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1025 | # Ha! Before infinite lists simply consisted of: | ||||||||||||||||||||||||||||
| 1026 | # my @return; | ||||||||||||||||||||||||||||
| 1027 | # push @return, $_ while($_ = shift @{$xs} and $p->($_)); | ||||||||||||||||||||||||||||
| 1028 | # return [@return]; | ||||||||||||||||||||||||||||
| 1029 | 4 | my $pointer = -1; | |||||||||||||||||||||||||||
| 1030 | 19 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=31.3
| |||||||||||||||||||||||||||||
| 1031 | 19 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 1032 | 19 | my $debug = 0; | |||||||||||||||||||||||||||
| 1033 | 19 | ▶ | print "$idx: in (done $pointer)\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1034 | 19 | ▶ | if ($pointer eq $INFINITE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1035 | 0 | die "Fetching an infinite amount of values in filter()!\n"; | |||||||||||||||||||||||||||
| 1036 | } | ||||||||||||||||||||||||||||
| 1037 | 19 | ▶ | if ($idx - 1 > $pointer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1038 | 0 | ▶ | print "$idx: doing $array->FETCH for $pointer..", $idx - 1, "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1039 | 0 | ▶ | map { $array->FETCH($_) if $_ < $array->FETCHSIZE} ($pointer..$idx-1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1040 | } | ||||||||||||||||||||||||||||
| 1041 | 19 | ▶ | if ($idx > $array->FETCHSIZE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1042 | 0 | ▶ | print "$idx: in: silly, getting out\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1043 | 0 | return undef; | |||||||||||||||||||||||||||
| 1044 | } | ||||||||||||||||||||||||||||
| 1045 | 19 | $pointer++; | |||||||||||||||||||||||||||
| 1046 | 19 | ▶ | if ($p->($xs->[$pointer])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1047 | 15 | ▶ | print "$idx: p true for index $pointer\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1048 | 15 | return $xs->[$pointer]; | |||||||||||||||||||||||||||
| 1049 | } else { | ||||||||||||||||||||||||||||
| 1050 | 4 | ▶ | print "$idx: p NOT true for index - resizing to $pointer\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1051 | 4 | $array->STORESIZE($pointer); | |||||||||||||||||||||||||||
| 1052 | 4 | return undef; | |||||||||||||||||||||||||||
| 1053 | } | ||||||||||||||||||||||||||||
| 1054 | 4 | }, scalar @{$xs}; | |||||||||||||||||||||||||||
| 1055 | 4 | return \@a; | |||||||||||||||||||||||||||
| 1056 | } | ||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||
| 1059 | =item dropWhile p xs | ||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||
| 1061 | Drops elements from the head of xs while p(that element) is | ||||||||||||||||||||||||||||
| 1062 | true. Returns the list. eg: | ||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||
| 1064 | $x = dropWhile { shift() <= 4 } [1..6]; # [5, 6] | ||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||
| 1066 | In Haskell: | ||||||||||||||||||||||||||||
| 1067 | |||||||||||||||||||||||||||||
| 1068 | dropWhile :: (a -> Bool) -> [a] -> [a] | ||||||||||||||||||||||||||||
| 1069 | dropWhile p [] = [] | ||||||||||||||||||||||||||||
| 1070 | dropWhile p xs@(x:xs') | ||||||||||||||||||||||||||||
| 1071 | | p x = dropWhile p xs' | ||||||||||||||||||||||||||||
| 1072 | | otherwise = xs | ||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||
| 1074 | =cut | ||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||
| 1076 | 4 | ▶ | sub dropWhile(&$) { | ||||||||||||||||||||||||||
|
Subroutine
dropWhile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1077 | 4 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1078 | # Ha! Before infinite lists simply consisted of: | ||||||||||||||||||||||||||||
| 1079 | # shift @{$xs} while($_ = @{$xs}[0] and $p->($_)); | ||||||||||||||||||||||||||||
| 1080 | 4 | my $pointer = 0; | |||||||||||||||||||||||||||
| 1081 | 4 | while (1) { | |||||||||||||||||||||||||||
| 1082 | 19 | ▶ | last unless $p->($xs->[$pointer]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1083 | 15 | $pointer++; | |||||||||||||||||||||||||||
| 1084 | } | ||||||||||||||||||||||||||||
| 1085 | 4 | print "Pointer = $pointer\n" if 0; | |||||||||||||||||||||||||||
| 1086 | 4 | my $len = scalar @{$xs}; | |||||||||||||||||||||||||||
| 1087 | 4 | ▶ | $len = $len == $INFINITE ? $len : $len - $pointer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1088 | 11 | ▶ | tie my @a, 'InfiniteList', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1089 | 11 | my($array, $idx) = @_; | |||||||||||||||||||||||||||
| 1090 | 11 | return $xs->[$idx + $pointer]; | |||||||||||||||||||||||||||
| 1091 | 4 | }, $len; | |||||||||||||||||||||||||||
| 1092 | 4 | return \@a; | |||||||||||||||||||||||||||
| 1093 | } | ||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||
| 1096 | =item span p xs | ||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||
| 1098 | Splits xs into two lists, the first containing the first few elements | ||||||||||||||||||||||||||||
| 1099 | for which p(that element) is true. eg: | ||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||
| 1101 | $x = span { shift() <= 4 }, [1..6]; | ||||||||||||||||||||||||||||
| 1102 | # [[1, 2, 3, 4], [5, 6]] | ||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||
| 1104 | In Haskell: | ||||||||||||||||||||||||||||
| 1105 | |||||||||||||||||||||||||||||
| 1106 | span :: (a -> Bool) -> [a] -> ([a],[a]) | ||||||||||||||||||||||||||||
| 1107 | span p [] = ([],[]) | ||||||||||||||||||||||||||||
| 1108 | span p xs@(x:xs') | ||||||||||||||||||||||||||||
| 1109 | | p x = (x:ys, zs) | ||||||||||||||||||||||||||||
| 1110 | | otherwise = ([],xs) | ||||||||||||||||||||||||||||
| 1111 | where (ys,zs) = span p xs' | ||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||
| 1113 | =cut | ||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||
| 1115 | 2 | ▶ | sub span(&$) { | ||||||||||||||||||||||||||
|
Subroutine
span: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1116 | 2 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1117 | 2 | my @xs = @{$xs}; | |||||||||||||||||||||||||||
| 1118 | 2 | return [takeWhile(\&$p, $xs), dropWhile(\&$p, \@xs)]; | |||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | |||||||||||||||||||||||||||||
| 1122 | =item break p xs | ||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||
| 1124 | Splits xs into two lists, the first containing the first few elements | ||||||||||||||||||||||||||||
| 1125 | for which p(that element) is false. eg: | ||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||
| 1127 | $x = break { shift() >= 4 }, [1..6]; # [[1, 2, 3], [4, 5, 6]] | ||||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||
| 1129 | In Haskell: | ||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||
| 1131 | break :: (a -> Bool) -> [a] -> ([a],[a]) | ||||||||||||||||||||||||||||
| 1132 | break p = span (not . p) | ||||||||||||||||||||||||||||
| 1133 | |||||||||||||||||||||||||||||
| 1134 | =cut | ||||||||||||||||||||||||||||
| 1135 | |||||||||||||||||||||||||||||
| 1136 | 1 | ▶ | sub break(&$) { | ||||||||||||||||||||||||||
|
Subroutine
break: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1137 | 1 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1138 | 1 | ▶ | return span(sub { not $p->(@_) }, $xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1139 | } | ||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||
| 1142 | =item lines s | ||||||||||||||||||||||||||||
| 1143 | |||||||||||||||||||||||||||||
| 1144 | Breaks the string s into multiple strings, split at line | ||||||||||||||||||||||||||||
| 1145 | boundaries. eg: | ||||||||||||||||||||||||||||
| 1146 | |||||||||||||||||||||||||||||
| 1147 | $x = lines("A\nB\nC"); # ['A', 'B', 'C'] | ||||||||||||||||||||||||||||
| 1148 | |||||||||||||||||||||||||||||
| 1149 | In Haskell: | ||||||||||||||||||||||||||||
| 1150 | |||||||||||||||||||||||||||||
| 1151 | lines :: String -> [String] | ||||||||||||||||||||||||||||
| 1152 | lines "" = [] | ||||||||||||||||||||||||||||
| 1153 | lines s = let (l,s') = break ('\n'==) s | ||||||||||||||||||||||||||||
| 1154 | in l : case s' of [] -> [] | ||||||||||||||||||||||||||||
| 1155 | (_:s'') -> lines s'' | ||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||
| 1157 | =cut | ||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||
| 1159 | 1 | ▶ | sub lines($) { | ||||||||||||||||||||||||||
|
Subroutine
lines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1160 | 1 | my $s = shift; | |||||||||||||||||||||||||||
| 1161 | 1 | return [split /\n/, $s]; | |||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||
| 1165 | =item words s | ||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||
| 1167 | Breaks the string s into multiple strings, split at whitespace | ||||||||||||||||||||||||||||
| 1168 | boundaries. eg: | ||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||
| 1170 | $x = words("hey how random"); # ['hey', 'how', 'random'] | ||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||
| 1172 | In Haskell: | ||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||
| 1174 | words :: String -> [String] | ||||||||||||||||||||||||||||
| 1175 | words s = case dropWhile isSpace s of | ||||||||||||||||||||||||||||
| 1176 | "" -> [] | ||||||||||||||||||||||||||||
| 1177 | s' -> w : words s'' | ||||||||||||||||||||||||||||
| 1178 | where (w,s'') = break isSpace s' | ||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||
| 1180 | =cut | ||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||
| 1182 | 1 | ▶ | sub words($) { | ||||||||||||||||||||||||||
|
Subroutine
words: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1183 | 1 | my $s = shift; | |||||||||||||||||||||||||||
| 1184 | 1 | return [split /\s+/, $s]; | |||||||||||||||||||||||||||
| 1185 | } | ||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||
| 1188 | =item unlines xs | ||||||||||||||||||||||||||||
| 1189 | |||||||||||||||||||||||||||||
| 1190 | Does the opposite of unlines, that is: joins multiple | ||||||||||||||||||||||||||||
| 1191 | strings into one, joined by newlines. eg: | ||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||
| 1193 | $x = unlines(['A', 'B', 'C']); # "A\nB\nC"; | ||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||
| 1195 | In Haskell: | ||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||
| 1197 | unlines :: [String] -> String | ||||||||||||||||||||||||||||
| 1198 | unlines = concatMap (\l -> l ++ "\n") | ||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||
| 1200 | (note that strings in Perl are not lists of characters, | ||||||||||||||||||||||||||||
| 1201 | so this approach will not actually work...) | ||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||
| 1203 | =cut | ||||||||||||||||||||||||||||
| 1204 | |||||||||||||||||||||||||||||
| 1205 | 1 | ▶ | sub unlines($) { | ||||||||||||||||||||||||||
|
Subroutine
unlines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1206 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1207 | # return concatMap(sub { return $_[0] . "\n"; }, $xs); | ||||||||||||||||||||||||||||
| 1208 | 1 | ▶ | return foldr1(sub { return $_[0] . "\n" . $_[1]; }, $xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1209 | } | ||||||||||||||||||||||||||||
| 1210 | |||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||
| 1212 | =item unwords ws | ||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||
| 1214 | Does the opposite of unwords, that is: joins multiple strings into | ||||||||||||||||||||||||||||
| 1215 | one, joined by a space. eg: | ||||||||||||||||||||||||||||
| 1216 | |||||||||||||||||||||||||||||
| 1217 | $x = unwords(["hey","how","random"]); # 'hey how random' | ||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||
| 1219 | In Haskell: | ||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||
| 1221 | unwords :: [String] -> String | ||||||||||||||||||||||||||||
| 1222 | unwords [] = [] | ||||||||||||||||||||||||||||
| 1223 | unwords ws = foldr1 (\w s -> w ++ ' ':s) ws | ||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||
| 1225 | =cut | ||||||||||||||||||||||||||||
| 1226 | |||||||||||||||||||||||||||||
| 1227 | 1 | ▶ | sub unwords($) { | ||||||||||||||||||||||||||
|
Subroutine
unwords: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1228 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1229 | 1 | ▶ | return foldr1(sub { return $_[0] . ' ' . $_[1]; }, $xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1230 | } | ||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||
| 1233 | =item Reverse xs | ||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||
| 1235 | Returns a list containing the elements of xs in reverse order. Note | ||||||||||||||||||||||||||||
| 1236 | the capital R, so as not to clash with the Perl command 'reverse'. | ||||||||||||||||||||||||||||
| 1237 | You should not try to Reverse an infinite list. eg: | ||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||
| 1239 | $x = Reverse([1..6]); # [6, 5, 4, 3, 2, 1] | ||||||||||||||||||||||||||||
| 1240 | |||||||||||||||||||||||||||||
| 1241 | In Haskell: | ||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||
| 1243 | reverse :: [a] -> [a] | ||||||||||||||||||||||||||||
| 1244 | reverse = foldl (flip (:)) [] | ||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||
| 1246 | =cut | ||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||
| 1248 | 3 | ▶ | sub Reverse($) { | ||||||||||||||||||||||||||
|
Subroutine
Reverse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1249 | 3 | my $xs = shift; | |||||||||||||||||||||||||||
| 1250 | 3 | return [reverse @{$xs}]; | |||||||||||||||||||||||||||
| 1251 | } | ||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||
| 1254 | =item And xs | ||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||
| 1256 | Returns true if all the elements in xs are true. Returns false | ||||||||||||||||||||||||||||
| 1257 | otherwise. Note the capital A, so as not to clash with the Perl | ||||||||||||||||||||||||||||
| 1258 | command 'and'. You should not try to And an infinite list (unless you | ||||||||||||||||||||||||||||
| 1259 | expect it to fail, as it will short-circuit). eg: | ||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||
| 1261 | $x = And([1, 1, 1]); # 1 | ||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||
| 1263 | In Haskell: | ||||||||||||||||||||||||||||
| 1264 | |||||||||||||||||||||||||||||
| 1265 | and :: [Bool] -> Bool | ||||||||||||||||||||||||||||
| 1266 | and = foldr (&&) True | ||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||
| 1268 | =cut | ||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||
| 1270 | 2 | ▶ | sub And($) { | ||||||||||||||||||||||||||
|
Subroutine
And: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1271 | 2 | my $xs = shift; | |||||||||||||||||||||||||||
| 1272 | map { | ||||||||||||||||||||||||||||
| 1273 | 8195 | ▶ | return 0 if not $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1274 | 2 | } @{$xs}; | |||||||||||||||||||||||||||
| 1275 | 1 | return 1; | |||||||||||||||||||||||||||
| 1276 | } | ||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||
| 1279 | =item Or xs | ||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||
| 1281 | Returns true if one of the elements in xs is true. Returns | ||||||||||||||||||||||||||||
| 1282 | false otherwise. Note the capital O, so as not to clash with | ||||||||||||||||||||||||||||
| 1283 | the Perl command 'or'. You may try to Or an infinite list | ||||||||||||||||||||||||||||
| 1284 | as it will short-circuit (unless you expect it to fail, that | ||||||||||||||||||||||||||||
| 1285 | is). eg: | ||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||
| 1287 | $x = Or([0, 0, 1]); # 1 | ||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||
| 1289 | In Haskell: | ||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||
| 1291 | or :: [Bool] -> Bool | ||||||||||||||||||||||||||||
| 1292 | or = foldr (||) False | ||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||
| 1294 | =cut | ||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||
| 1296 | 1 | ▶ | sub Or($) { | ||||||||||||||||||||||||||
|
Subroutine
Or: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1297 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1298 | map { | ||||||||||||||||||||||||||||
| 1299 | 3 | ▶ | return 1 if $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1300 | 1 | } @{$xs}; | |||||||||||||||||||||||||||
| 1301 | 0 | return 0; | |||||||||||||||||||||||||||
| 1302 | } | ||||||||||||||||||||||||||||
| 1303 | |||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||
| 1305 | =item any p xs | ||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||
| 1307 | Returns true if one of p(each element of xs) are true. Returns | ||||||||||||||||||||||||||||
| 1308 | false otherwise. You should not try to And an infinite | ||||||||||||||||||||||||||||
| 1309 | list (unless you expect it to fail, as it will short-circuit). | ||||||||||||||||||||||||||||
| 1310 | eg: | ||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||
| 1312 | $x = any { even(shift) } [1, 2, 3]; # 1 | ||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||
| 1314 | In Haskell: | ||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||
| 1316 | any :: (a -> Bool) -> [a] -> Bool | ||||||||||||||||||||||||||||
| 1317 | any p = or . map p | ||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||
| 1319 | =cut | ||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||
| 1321 | 3 | ▶ | sub any(&$) { | ||||||||||||||||||||||||||
|
Subroutine
any: called CC=5 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||
| 1322 | 3 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1323 | 3 | my $n = 0; | |||||||||||||||||||||||||||
| 1324 | 3 | my $size = $#{$xs}; | |||||||||||||||||||||||||||
| 1325 | 3 | ▶ | while ($n <= $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1326 | 6 | ▶ | return 1 if $p->($xs->[$n]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1327 | 3 | $n++; | |||||||||||||||||||||||||||
| 1328 | } | ||||||||||||||||||||||||||||
| 1329 | 0 | ▶ | if ($size == $Language::Functional::INFINITE | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$size == $Language::Functional::INFINITE or $size == $Language::Functional::INFINITE - 1
MC/DC0%0/2
$size == $Language::Functional::INFINITE or $size == $Language::Functional::INFINITE - 1
$size == $Language::Functional::INFINITE
$size == $Language::Functional::INFINITE - 1
Truth table0%0/3
A or B
A= $size == $Language::Functional::INFINITEB= $size == $Language::Functional::INFINITE - 1
| |||||||||||||||||||||||||||||
| 1330 | or $size == $Language::Functional::INFINITE - 1 | ||||||||||||||||||||||||||||
| 1331 | ) { | ||||||||||||||||||||||||||||
| 1332 | 0 | confess "Evaluating predicate on inifinite number of elements " . | |||||||||||||||||||||||||||
| 1333 | "would never end!"; | ||||||||||||||||||||||||||||
| 1334 | } | ||||||||||||||||||||||||||||
| 1335 | 0 | return 0; | |||||||||||||||||||||||||||
| 1336 | } | ||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||
| 1339 | =item all p xs | ||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||
| 1341 | Returns true if all of the p(each element of xs) is true. Returns | ||||||||||||||||||||||||||||
| 1342 | false otherwise. You may try to Or an infinite list | ||||||||||||||||||||||||||||
| 1343 | as it will short-circuit (unless you expect it to fail, that | ||||||||||||||||||||||||||||
| 1344 | is). eg: | ||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||
| 1346 | $x = all { odd(shift) } [1, 1, 3]; # 1 | ||||||||||||||||||||||||||||
| 1347 | |||||||||||||||||||||||||||||
| 1348 | In Haskell: | ||||||||||||||||||||||||||||
| 1349 | |||||||||||||||||||||||||||||
| 1350 | all :: (a -> Bool) -> [a] -> Bool | ||||||||||||||||||||||||||||
| 1351 | all p = and . map p | ||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||
| 1353 | =cut | ||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||
| 1355 | 4 | ▶ | sub all(&$) { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 1356 | 4 | my($p, $xs) = @_; | |||||||||||||||||||||||||||
| 1357 | 4 | my $n = 0; | |||||||||||||||||||||||||||
| 1358 | 4 | my $size = $#{$xs}; | |||||||||||||||||||||||||||
| 1359 | 4 | ▶ | while ($n <= $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1360 | 8200 | ▶ | return 0 if not $p->($xs->[$n]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1361 | 8199 | $n++; | |||||||||||||||||||||||||||
| 1362 | } | ||||||||||||||||||||||||||||
| 1363 | 3 | ▶ | if ($size == $Language::Functional::INFINITE | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$size == $Language::Functional::INFINITE or $size == $Language::Functional::INFINITE - 1
MC/DC50%1/2
$size == $Language::Functional::INFINITE or $size == $Language::Functional::INFINITE - 1
$size == $Language::Functional::INFINITE
$size == $Language::Functional::INFINITE - 1
Truth table66%2/3
A or B
A= $size == $Language::Functional::INFINITEB= $size == $Language::Functional::INFINITE - 1
| |||||||||||||||||||||||||||||
| 1364 | or $size == $Language::Functional::INFINITE - 1 | ||||||||||||||||||||||||||||
| 1365 | ) { | ||||||||||||||||||||||||||||
| 1366 | 1 | confess "Evaluating predicate on inifinite number of elements " . | |||||||||||||||||||||||||||
| 1367 | "would never end!"; | ||||||||||||||||||||||||||||
| 1368 | } | ||||||||||||||||||||||||||||
| 1369 | 2 | return 1; | |||||||||||||||||||||||||||
| 1370 | } | ||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||
| 1373 | =item elem x xs | ||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||
| 1375 | Returns true is x is present in xs. | ||||||||||||||||||||||||||||
| 1376 | You probably should not do this with infinite lists. | ||||||||||||||||||||||||||||
| 1377 | Note that this assumes x and xs are numbers. | ||||||||||||||||||||||||||||
| 1378 | eg: | ||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||
| 1380 | $x = elem(2, [1, 2, 3]); # 1 | ||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||
| 1382 | In Haskell: | ||||||||||||||||||||||||||||
| 1383 | |||||||||||||||||||||||||||||
| 1384 | elem :: Eq a => a -> [a] -> Bool | ||||||||||||||||||||||||||||
| 1385 | elem = any . (==) | ||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||
| 1387 | =cut | ||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||
| 1389 | 1 | ▶ | sub elem($$) { | ||||||||||||||||||||||||||
|
Subroutine
elem: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1390 | 1 | my($x, $xs) = @_; | |||||||||||||||||||||||||||
| 1391 | 1 | ▶ | return any(sub { $_[0] == $x }, $xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1392 | } | ||||||||||||||||||||||||||||
| 1393 | |||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||
| 1395 | =item notElem x xs | ||||||||||||||||||||||||||||
| 1396 | |||||||||||||||||||||||||||||
| 1397 | Returns true if x is not present in x. You should not do this with | ||||||||||||||||||||||||||||
| 1398 | infinite lists. Note that this assumes that x and xs are numbers. eg: | ||||||||||||||||||||||||||||
| 1399 | |||||||||||||||||||||||||||||
| 1400 | $x = notElem(2, [1, 1, 3]); # 1 | ||||||||||||||||||||||||||||
| 1401 | |||||||||||||||||||||||||||||
| 1402 | In Haskell: | ||||||||||||||||||||||||||||
| 1403 | |||||||||||||||||||||||||||||
| 1404 | notElem :: Eq a => a -> [a] -> Bool | ||||||||||||||||||||||||||||
| 1405 | notElem = all . (/=) | ||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||
| 1407 | =cut | ||||||||||||||||||||||||||||
| 1408 | |||||||||||||||||||||||||||||
| 1409 | 1 | ▶ | sub notElem($$) { | ||||||||||||||||||||||||||
|
Subroutine
notElem: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1410 | 1 | my($x, $xs) = @_; | |||||||||||||||||||||||||||
| 1411 | 1 | ▶ | return all { shift() != $x } $xs; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1412 | } | ||||||||||||||||||||||||||||
| 1413 | |||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||
| 1415 | =item lookup key xys | ||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||
| 1417 | This returns the value of the key in xys, where xys is a list of key, | ||||||||||||||||||||||||||||
| 1418 | value pairs. It returns undef if the key was not found. You should not | ||||||||||||||||||||||||||||
| 1419 | do this with infinite lists. Note that this assumes that the keys are | ||||||||||||||||||||||||||||
| 1420 | strings. eg: | ||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||
| 1422 | $x = lookup(3, [1..6]); # 4 | ||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||
| 1424 | In Haskell: | ||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||
| 1426 | lookup :: Eq a => a -> [(a,b)] -> Maybe b | ||||||||||||||||||||||||||||
| 1427 | lookup k [] = Nothing | ||||||||||||||||||||||||||||
| 1428 | lookup k ((x,y):xys) | ||||||||||||||||||||||||||||
| 1429 | | k==x = Just y | ||||||||||||||||||||||||||||
| 1430 | | otherwise = lookup k xys | ||||||||||||||||||||||||||||
| 1431 | |||||||||||||||||||||||||||||
| 1432 | TODO: Make sure this works with infinite lists | ||||||||||||||||||||||||||||
| 1433 | |||||||||||||||||||||||||||||
| 1434 | =cut | ||||||||||||||||||||||||||||
| 1435 | |||||||||||||||||||||||||||||
| 1436 | 1 | ▶ | sub lookup($$) { | ||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 1437 | 1 | my($key, $xys) = @_; | |||||||||||||||||||||||||||
| 1438 | 1 | my %hash = @{$xys}; | |||||||||||||||||||||||||||
| 1439 | 1 | ▶ | return $hash{$key} if defined $hash{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1440 | 0 | return undef; | |||||||||||||||||||||||||||
| 1441 | } | ||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||
| 1444 | =item minimum xs | ||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||
| 1446 | Returns the minimum value in xs. | ||||||||||||||||||||||||||||
| 1447 | You should not do this with a infinite list. | ||||||||||||||||||||||||||||
| 1448 | eg: | ||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||
| 1450 | $x = minimum([1..6]); # 1 | ||||||||||||||||||||||||||||
| 1451 | |||||||||||||||||||||||||||||
| 1452 | In Haskell: | ||||||||||||||||||||||||||||
| 1453 | |||||||||||||||||||||||||||||
| 1454 | minimum :: Ord a => [a] -> a | ||||||||||||||||||||||||||||
| 1455 | minimum = foldl1 min | ||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||
| 1457 | =cut | ||||||||||||||||||||||||||||
| 1458 | |||||||||||||||||||||||||||||
| 1459 | 1 | ▶ | sub minimum($) { | ||||||||||||||||||||||||||
|
Subroutine
minimum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1460 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1461 | 1 | return foldl1(\&min, $xs); | |||||||||||||||||||||||||||
| 1462 | } | ||||||||||||||||||||||||||||
| 1463 | |||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||
| 1465 | =item maximum xs | ||||||||||||||||||||||||||||
| 1466 | |||||||||||||||||||||||||||||
| 1467 | Returns the maximum value in xs. | ||||||||||||||||||||||||||||
| 1468 | You should not do this with an infinite list. | ||||||||||||||||||||||||||||
| 1469 | eg: maximum([1..6]) = 6. In Haskell: | ||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||
| 1471 | maximum :: Ord a => [a] -> a | ||||||||||||||||||||||||||||
| 1472 | maximum = foldl1 max | ||||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||
| 1474 | =cut | ||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||
| 1476 | 3 | ▶ | sub maximum($) { | ||||||||||||||||||||||||||
|
Subroutine
maximum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1477 | 3 | my $xs = shift; | |||||||||||||||||||||||||||
| 1478 | 3 | return foldl1(\&max, $xs); | |||||||||||||||||||||||||||
| 1479 | } | ||||||||||||||||||||||||||||
| 1480 | |||||||||||||||||||||||||||||
| 1481 | |||||||||||||||||||||||||||||
| 1482 | =item sum xs | ||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||
| 1484 | Returns the sum of the elements of xs. | ||||||||||||||||||||||||||||
| 1485 | You should not do this with an infinite list. | ||||||||||||||||||||||||||||
| 1486 | eg: sum([1..6]) = 21. In Haskell: | ||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||
| 1488 | sum :: Num a => [a] -> a | ||||||||||||||||||||||||||||
| 1489 | sum = foldl' (+) 0 | ||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||
| 1491 | =cut #' | ||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||
| 1493 | 1 | ▶ | sub sum($) { | ||||||||||||||||||||||||||
|
Subroutine
sum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1494 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1495 | 1 | ▶ | return foldl(sub { $_[0] + $_[1] }, 0, $xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1496 | } | ||||||||||||||||||||||||||||
| 1497 | |||||||||||||||||||||||||||||
| 1498 | |||||||||||||||||||||||||||||
| 1499 | =item product xs | ||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||
| 1501 | Returns the products of the elements of xs. | ||||||||||||||||||||||||||||
| 1502 | You should not do this with an infinite list. | ||||||||||||||||||||||||||||
| 1503 | eg: product([1..6]) = 720. In Haskell: | ||||||||||||||||||||||||||||
| 1504 | |||||||||||||||||||||||||||||
| 1505 | product :: Num a => [a] -> a | ||||||||||||||||||||||||||||
| 1506 | product = foldl' (*) 1 | ||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||
| 1508 | =cut #' | ||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||
| 1510 | 1 | ▶ | sub product($) { | ||||||||||||||||||||||||||
|
Subroutine
product: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1511 | 1 | my $xs = shift; | |||||||||||||||||||||||||||
| 1512 | 1 | ▶ | return foldl(sub { $_[0] * $_[1] }, 1,$xs); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1513 | } | ||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||
| 1516 | =item zip as bs | ||||||||||||||||||||||||||||
| 1517 | |||||||||||||||||||||||||||||
| 1518 | Zips together two lists into one list. Should | ||||||||||||||||||||||||||||
| 1519 | not be done with infinite lists. | ||||||||||||||||||||||||||||
| 1520 | eg: zip([1..6], [7..12]) = [1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 6, 12]. | ||||||||||||||||||||||||||||
| 1521 | In Haskell: | ||||||||||||||||||||||||||||
| 1522 | |||||||||||||||||||||||||||||
| 1523 | zip :: [a] -> [b] -> [(a,b)] | ||||||||||||||||||||||||||||
| 1524 | zip = zipWith (\a b -> (a,b)) | ||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||
| 1526 | zipWith :: (a->b->c) -> [a]->[b]->[c] | ||||||||||||||||||||||||||||
| 1527 | zipWith z (a:as) (b:bs) = z a b : zipWith z as bs | ||||||||||||||||||||||||||||
| 1528 | zipWith _ _ _ = [] | ||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||
| 1530 | =cut | ||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||
| 1532 | 1 | ▶ | sub zip($$) { | ||||||||||||||||||||||||||
|
Subroutine
zip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1533 | 1 | my($as, $bs) = @_; | |||||||||||||||||||||||||||
| 1534 | 1 | my @result; | |||||||||||||||||||||||||||
| 1535 | 1 | foreach (1..max(Length($as), Length($bs))) { | |||||||||||||||||||||||||||
| 1536 | 6 | push @result, shift @{$as}; | |||||||||||||||||||||||||||
| 1537 | 6 | push @result, shift @{$bs}; | |||||||||||||||||||||||||||
| 1538 | } | ||||||||||||||||||||||||||||
| 1539 | 1 | return [@result]; | |||||||||||||||||||||||||||
| 1540 | } | ||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||
| 1542 | |||||||||||||||||||||||||||||
| 1543 | =item zip3 as bs cs | ||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||
| 1545 | Zips together three lists into one. Should not be | ||||||||||||||||||||||||||||
| 1546 | done with infinite lists. | ||||||||||||||||||||||||||||
| 1547 | eg: zip3([1..2], [3..4], [5..6]) = [1, 3, 5, 2, 4, 6]. | ||||||||||||||||||||||||||||
| 1548 | In Haskell: | ||||||||||||||||||||||||||||
| 1549 | |||||||||||||||||||||||||||||
| 1550 | zip3 :: [a] -> [b] -> [c] -> [(a,b,c)] | ||||||||||||||||||||||||||||
| 1551 | zip3 = zipWith3 (\a b c -> (a,b,c)) | ||||||||||||||||||||||||||||
| 1552 | |||||||||||||||||||||||||||||
| 1553 | zipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->[d] | ||||||||||||||||||||||||||||
| 1554 | zipWith3 z (a:as) (b:bs) (c:cs) | ||||||||||||||||||||||||||||
| 1555 | = z a b c : zipWith3 z as bs cs | ||||||||||||||||||||||||||||
| 1556 | zipWith3 _ _ _ _ = [] | ||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||
| 1558 | =cut | ||||||||||||||||||||||||||||
| 1559 | |||||||||||||||||||||||||||||
| 1560 | 1 | ▶ | sub zip3($$$) { | ||||||||||||||||||||||||||
|
Subroutine
zip3: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1561 | 1 | my($as, $bs, $cs) = @_; | |||||||||||||||||||||||||||
| 1562 | 1 | my @result; | |||||||||||||||||||||||||||
| 1563 | 1 | foreach (1..maximum([Length($as), Length($bs), Length($cs)])) { | |||||||||||||||||||||||||||
| 1564 | 2 | push @result, shift @{$as}; | |||||||||||||||||||||||||||
| 1565 | 2 | push @result, shift @{$bs}; | |||||||||||||||||||||||||||
| 1566 | 2 | push @result, shift @{$cs}; | |||||||||||||||||||||||||||
| 1567 | } | ||||||||||||||||||||||||||||
| 1568 | 1 | return [@result]; | |||||||||||||||||||||||||||
| 1569 | } | ||||||||||||||||||||||||||||
| 1570 | |||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||
| 1572 | =item unzip abs | ||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||
| 1574 | Unzips one list into two. Should not be done with infinite lists. | ||||||||||||||||||||||||||||
| 1575 | eg: unzip([1,7,2,8,3,9,4,10,5,11,6,12]) = ([1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]). | ||||||||||||||||||||||||||||
| 1576 | |||||||||||||||||||||||||||||
| 1577 | unzip :: [(a,b)] -> ([a],[b]) | ||||||||||||||||||||||||||||
| 1578 | unzip = foldr (\(a,b) ~(as,bs) -> (a:as, b:bs)) ([], []) | ||||||||||||||||||||||||||||
| 1579 | |||||||||||||||||||||||||||||
| 1580 | =cut | ||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||
| 1582 | 1 | ▶ | sub unzip($) { | ||||||||||||||||||||||||||
|
Subroutine
unzip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1583 | 1 | my $abs = shift; | |||||||||||||||||||||||||||
| 1584 | 1 | my(@as, @bs); | |||||||||||||||||||||||||||
| 1585 | 1 | ▶ | while (@{$abs}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1586 | 6 | push @as, shift @{$abs}; | |||||||||||||||||||||||||||
| 1587 | 6 | push @bs, shift @{$abs}; | |||||||||||||||||||||||||||
| 1588 | } | ||||||||||||||||||||||||||||
| 1589 | 1 | return [@as], [@bs]; | |||||||||||||||||||||||||||
| 1590 | } | ||||||||||||||||||||||||||||
| 1591 | |||||||||||||||||||||||||||||
| 1592 | |||||||||||||||||||||||||||||
| 1593 | =item unzip abcs | ||||||||||||||||||||||||||||
| 1594 | |||||||||||||||||||||||||||||
| 1595 | Unzips one list into three. Should not be done with infinite lists. | ||||||||||||||||||||||||||||
| 1596 | eg: unzip3([1,3,5,2,4,6]) = ([1, 2], [3, 4], [5, 6]). | ||||||||||||||||||||||||||||
| 1597 | In Haskell: | ||||||||||||||||||||||||||||
| 1598 | |||||||||||||||||||||||||||||
| 1599 | unzip3 :: [(a,b,c)] -> ([a],[b],[c]) | ||||||||||||||||||||||||||||
| 1600 | unzip3 = foldr (\(a,b,c) ~(as,bs,cs) -> (a:as,b:bs,c:cs)) | ||||||||||||||||||||||||||||
| 1601 | ([],[],[]) | ||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||
| 1603 | =cut | ||||||||||||||||||||||||||||
| 1604 | |||||||||||||||||||||||||||||
| 1605 | 1 | ▶ | sub unzip3($) { | ||||||||||||||||||||||||||
|
Subroutine
unzip3: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1606 | 1 | my $abcs = shift; | |||||||||||||||||||||||||||
| 1607 | 1 | my(@as, @bs, @cs); | |||||||||||||||||||||||||||
| 1608 | 1 | ▶ | while (@{$abcs}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1609 | 2 | push @as, shift @{$abcs}; | |||||||||||||||||||||||||||
| 1610 | 2 | push @bs, shift @{$abcs}; | |||||||||||||||||||||||||||
| 1611 | 2 | push @cs, shift @{$abcs}; | |||||||||||||||||||||||||||
| 1612 | } | ||||||||||||||||||||||||||||
| 1613 | 1 | return [@as], [@bs], [@cs]; | |||||||||||||||||||||||||||
| 1614 | } | ||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||
| 1616 | |||||||||||||||||||||||||||||
| 1617 | =item integers | ||||||||||||||||||||||||||||
| 1618 | |||||||||||||||||||||||||||||
| 1619 | A useful function that returns an infinite list containing | ||||||||||||||||||||||||||||
| 1620 | all the integers. eg: integers = (1, 2, 3, 4, 5, ...). | ||||||||||||||||||||||||||||
| 1621 | |||||||||||||||||||||||||||||
| 1622 | =cut | ||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||
| 1624 | 26 | ▶ | sub integers() { | ||||||||||||||||||||||||||
|
Subroutine
integers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1625 | 26 | ▶ | return iterate { shift() +1 } 1; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1626 | } | ||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||
| 1628 | |||||||||||||||||||||||||||||
| 1629 | =item factors x | ||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||
| 1631 | A useful function that returns the factors of x. | ||||||||||||||||||||||||||||
| 1632 | eg: factors(100) = [1, 2, 4, 5, 10, 20, 25, 50, 100]. | ||||||||||||||||||||||||||||
| 1633 | In Haskell: | ||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||
| 1635 | factors x = [n | n <- [1..x], x `mod` n == 0] | ||||||||||||||||||||||||||||
| 1636 | |||||||||||||||||||||||||||||
| 1637 | =cut | ||||||||||||||||||||||||||||
| 1638 | |||||||||||||||||||||||||||||
| 1639 | 30 | ▶ | sub factors($) { | ||||||||||||||||||||||||||
|
Subroutine
factors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1640 | 30 | my $x = shift; | |||||||||||||||||||||||||||
| 1641 | 30 | return [grep { $x % $_ == 0 } (1..$x)]; | |||||||||||||||||||||||||||
| 1642 | } | ||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||
| 1644 | |||||||||||||||||||||||||||||
| 1645 | =item prime x | ||||||||||||||||||||||||||||
| 1646 | |||||||||||||||||||||||||||||
| 1647 | A useful function that returns, rather unefficiently, | ||||||||||||||||||||||||||||
| 1648 | if x is a prime number or not. It is rather useful while | ||||||||||||||||||||||||||||
| 1649 | used as a filter, | ||||||||||||||||||||||||||||
| 1650 | eg: take(10, filter("prime", integers)) = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]. | ||||||||||||||||||||||||||||
| 1651 | In Haskell: | ||||||||||||||||||||||||||||
| 1652 | |||||||||||||||||||||||||||||
| 1653 | primes = [n | n <- [2..], length (factors n) == 2] | ||||||||||||||||||||||||||||
| 1654 | |||||||||||||||||||||||||||||
| 1655 | =cut | ||||||||||||||||||||||||||||
| 1656 | |||||||||||||||||||||||||||||
| 1657 | 29 | ▶ | sub prime($) { | ||||||||||||||||||||||||||
|
Subroutine
prime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1658 | 29 | my $x = shift; | |||||||||||||||||||||||||||
| 1659 | 29 | return Length(factors($x)) == 2; | |||||||||||||||||||||||||||
| 1660 | } | ||||||||||||||||||||||||||||
| 1661 | |||||||||||||||||||||||||||||
| 1662 | =back | ||||||||||||||||||||||||||||
| 1663 | |||||||||||||||||||||||||||||
| 1664 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 1665 | |||||||||||||||||||||||||||||
| 1666 | Leon Brocard E<lt>F<acme@astray.com>E<gt> | ||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||
| 1668 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||
| 1670 | Copyright (C) 1999-2008, Leon Brocard | ||||||||||||||||||||||||||||
| 1671 | |||||||||||||||||||||||||||||
| 1672 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 1673 | |||||||||||||||||||||||||||||
| 1674 | This module is free software; you can redistribute it or modify it | ||||||||||||||||||||||||||||
| 1675 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||
| 1677 | =cut | ||||||||||||||||||||||||||||
| 1678 | |||||||||||||||||||||||||||||
| 1679 | |||||||||||||||||||||||||||||
| 1680 | |||||||||||||||||||||||||||||
| 1681 | |||||||||||||||||||||||||||||
| 1682 | package InfiniteList; | ||||||||||||||||||||||||||||
| 1683 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1684 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1685 | 1 | ▶ | use Tie::Array; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1686 | 1 | ▶ | use vars qw(@ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1687 | 1 | @ISA = ('Tie::Array'); | |||||||||||||||||||||||||||
| 1688 | |||||||||||||||||||||||||||||
| 1689 | 52 | ▶ | sub TIEARRAY { | ||||||||||||||||||||||||||
|
Subroutine
TIEARRAY: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 1690 | 52 | my $class = shift; | |||||||||||||||||||||||||||
| 1691 | 52 | my $closure = shift; | |||||||||||||||||||||||||||
| 1692 | 52 | ▶ | my $size = shift || $Language::Functional::INFINITE; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $Language::Functional::INFINITE
MC/DC0%0/2
shift() || $Language::Functional::INFINITE
shift()
$Language::Functional::INFINITE
Truth table66%2/3
A || B
A= shift()B= $Language::Functional::INFINITE
| |||||||||||||||||||||||||||||
| 1693 | 52 | ▶ | confess "usage: tie(\@ary, 'InfiniteList', &closure)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ or ref $closure ne "CODE"
MC/DC0%0/2
@_ or ref $closure ne "CODE"
@_
ref $closure ne "CODE"
Truth table33%1/3
A or B
A= @_B= ref $closure ne "CODE"
| |||||||||||||||||||||||||||||
| 1694 | if @_ || ref($closure) ne 'CODE'; | ||||||||||||||||||||||||||||
| 1695 | 52 | return bless { | |||||||||||||||||||||||||||
| 1696 | CLOSURE => $closure, | ||||||||||||||||||||||||||||
| 1697 | ARRAY => [], | ||||||||||||||||||||||||||||
| 1698 | SIZE => $size, | ||||||||||||||||||||||||||||
| 1699 | }, $class; | ||||||||||||||||||||||||||||
| 1700 | } | ||||||||||||||||||||||||||||
| 1701 | |||||||||||||||||||||||||||||
| 1702 | 66038 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||
| 1703 | 66038 | my($self,$idx) = @_; | |||||||||||||||||||||||||||
| 1704 | 66038 | my $debug = 0; | |||||||||||||||||||||||||||
| 1705 | 66038 | ▶ | print ":fetch $idx... " if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1706 | 66038 | ▶ | if ($idx == $Language::Functional::INFINITE or $idx == $Language::Functional::INFINITE-1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$idx == $Language::Functional::INFINITE or $idx == $Language::Functional::INFINITE - 1
MC/DC50%1/2
$idx == $Language::Functional::INFINITE or $idx == $Language::Functional::INFINITE - 1
$idx == $Language::Functional::INFINITE
$idx == $Language::Functional::INFINITE - 1
Truth table66%2/3
A or B
A= $idx == $Language::Functional::INFINITEB= $idx == $Language::Functional::INFINITE - 1
| |||||||||||||||||||||||||||||
| 1707 | 6 | confess "Fetching an infinite amount of values!"; | |||||||||||||||||||||||||||
| 1708 | } | ||||||||||||||||||||||||||||
| 1709 | 66032 | ▶ | if (not defined $self->{ARRAY}[$idx]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1710 | 37117 | ▶ | print "MISS\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1711 | 37117 | $self->{ARRAY}[$idx] = $self->{CLOSURE}->($self, $idx); | |||||||||||||||||||||||||||
| 1712 | } else { | ||||||||||||||||||||||||||||
| 1713 | 28915 | ▶ | print "HIT\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1714 | } | ||||||||||||||||||||||||||||
| 1715 | 66030 | ▶ | print ":so $idx = ", $self->{ARRAY}[$idx], "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1716 | 66030 | return $self->{ARRAY}[$idx]; | |||||||||||||||||||||||||||
| 1717 | } | ||||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||
| 1719 | 33065 | ▶ | sub FETCHSIZE { | ||||||||||||||||||||||||||
|
Subroutine
FETCHSIZE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1720 | 33065 | my $self = shift; | |||||||||||||||||||||||||||
| 1721 | 33065 | return $self->{SIZE}; | |||||||||||||||||||||||||||
| 1722 | } | ||||||||||||||||||||||||||||
| 1723 | |||||||||||||||||||||||||||||
| 1724 | 3 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1725 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 1726 | 3 | confess "Storing, this should never happen to an infinite list!"; | |||||||||||||||||||||||||||
| 1727 | } | ||||||||||||||||||||||||||||
| 1728 | |||||||||||||||||||||||||||||
| 1729 | 6 | ▶ | sub STORESIZE { | ||||||||||||||||||||||||||
|
Subroutine
STORESIZE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1730 | 6 | my($self, $size) = @_; | |||||||||||||||||||||||||||
| 1731 | 6 | $self->{SIZE} = $size; | |||||||||||||||||||||||||||
| 1732 | } | ||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||