| 1 | package FAST::List::Gen::Haskell; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use lib '../../'; | ||
| 5 | use FAST::List::Gen::Lazy '*'; | ||
| 6 | use Scalar::Util 'weaken'; | ||
| 7 | sub DEBUG () {0} | ||
| 8 | our @EXPORT = qw (lazy L lazyx Lx fn now seq map_ unzip unzipn x_xs); | ||
| 9 | our @EXPORT_OK; | ||
| 10 | my $setup_export = sub { | ||
| 11 | @EXPORT_OK = keys %{{map {$_ => 1} @EXPORT, @FAST::List::Gen::EXPORT_OK}} | ||
| 12 | }; | ||
| 13 | our %EXPORT_TAGS = (base => \@EXPORT, all => \@EXPORT_OK); | ||
| 14 | BEGIN { | ||
| 15 | no strict 'refs'; | ||
| 16 | *import = *FAST::List::Gen::import; | ||
| 17 | *{uc $_} = *{'FAST::List::Gen::'.uc} for 'version' | ||
| 18 | } | ||
| 19 | our ($a, $b); | ||
| 20 | no if ($] > 5.012), warnings => 'illegalproto'; | ||
| 21 | |||
| 22 | |||
| 23 | =head1 NAME | ||
| 24 | |||
| 25 | FAST::List::Gen::Haskell - the haskell prelude in perl5 | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | this module provides most of the functions in the haskell prelude that pertain | ||
| 30 | to working with lists. | ||
| 31 | |||
| 32 | # haskell: fibs = 0 : 1 : zipWith (+) fibs (tail fibs) | ||
| 33 | |||
| 34 | use FAST::List::Gen::Haskell; | ||
| 35 | |||
| 36 | $fibs = lazy 0, 1, zipWith {&sum} $fibs, tail $fibs; | ||
| 37 | |||
| 38 | print "@$fibs[0 .. 10]\n"; # prints '0 1 1 2 3 5 8 13 21 34 55' | ||
| 39 | |||
| 40 | C< lazy > provides the generic behavior of haskell's lazy list concatenation | ||
| 41 | (the C< : > and C< ++ > operators). none of its elements are touched until they | ||
| 42 | are needed. in general, all the functions in this package defer their execution | ||
| 43 | until something is required of them. they also only touch their arguments at | ||
| 44 | the latest possible time, which is how the co-recursion above works. | ||
| 45 | |||
| 46 | the functions in this module are a bit more flexible (perlish) than those in | ||
| 47 | haskell. in most cases where a function expects a single generator, a list of | ||
| 48 | values and/or generators can be provided, which will be preprocessed by | ||
| 49 | C< lazy > into a single generator. | ||
| 50 | |||
| 51 | when loaded, most of the functions in this package become methods for all | ||
| 52 | generators. if a method of the same name already exists, the method from this | ||
| 53 | package will be prefixed with C< hs_ >. so C< filter > is a method named | ||
| 54 | C<< ->hs_filter(...) >> | ||
| 55 | |||
| 56 | this library currently does not have the best performance due to over-caching | ||
| 57 | of many internal generators. a future update will address this by replacing | ||
| 58 | those generators with cache-less generator streams. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub let { | ||
| 63 | my ($name, $code) = splice @_, 0, 2; | ||
| 64 | |||
| 65 | $_ = &fn (ref eq 'ARRAY' ? @$_ : $_, @_) for $code; | ||
| 66 | |||
| 67 | if (DEBUG) { | ||
| 68 | my $next = $code; | ||
| 69 | $code = sub {Carp::cluck "$name @_"; goto &$next} | ||
| 70 | } | ||
| 71 | no strict 'refs'; | ||
| 72 | *$name = *{ucfirst $name} = $code; | ||
| 73 | |||
| 74 | *{'FAST::List::Gen::erator::' | ||
| 75 | .('hs_' x defined &{'FAST::List::Gen::erator::'.$name}) | ||
| 76 | .$name} = sub { | ||
| 77 | push @_, shift; goto &$code | ||
| 78 | }; | ||
| 79 | push @EXPORT, $name, ucfirst $name; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $pred = sub { | ||
| 83 | local *_ = (@_ > 1 ? \$_[1] : \undef); | ||
| 84 | $_[0]->($_) | ||
| 85 | }; | ||
| 86 | |||
| 87 | sub map_ (&) {&map(@_, $_)} | ||
| 88 | |||
| 89 | |||
| 90 | =head1 FUNCTIONS | ||
| 91 | |||
| 92 | all of these functions are available with a ucfirst name, since many clash with | ||
| 93 | perl builtin names. | ||
| 94 | |||
| 95 | =head2 Utility Operations | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item x_xs C< [GENERATOR] > | ||
| 100 | |||
| 101 | C< x_xs > is a convenience function that returns the head and tail of a passed | ||
| 102 | in generator. C< x_xs > uses C< $_ > without an argument. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub x_xs (;$) {(@_, $_)[0]->x_xs} | ||
| 107 | |||
| 108 | |||
| 109 | =item seq C< LIST > | ||
| 110 | |||
| 111 | forces immediate evaluation of the elements in C< LIST > and returns the list | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | BEGIN {*seq = \&now} | ||
| 116 | |||
| 117 | |||
| 118 | =item flip C< {CODE} > | ||
| 119 | |||
| 120 | C< flip > converts C< CODE > into a function that takes it's arguments reversed | ||
| 121 | |||
| 122 | my $idx = \&head . flip \&drop; | ||
| 123 | |||
| 124 | $idx->($gen, 5) == $gen->$idx(5) == $gen->get(5) | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | let flip => sub (&) {goto &FAST::List::Gen::Function::flip}; | ||
| 129 | |||
| 130 | |||
| 131 | =back | ||
| 132 | |||
| 133 | =head2 List operations | ||
| 134 | |||
| 135 | =over 4 | ||
| 136 | |||
| 137 | =item Map C< {CODE} LIST > | ||
| 138 | |||
| 139 | Map f xs is the list obtained by applying f to each element of xs, i.e., | ||
| 140 | |||
| 141 | map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] | ||
| 142 | map f [x1, x2, ...] == [f x1, f x2, ...] | ||
| 143 | |||
| 144 | $x = &map(sub {$_**2}, $gen); | ||
| 145 | $x = Map {$_**2} $gen; | ||
| 146 | |||
| 147 | that usage is the same as C< FAST::List::Gen::gen >, but this is something that | ||
| 148 | C< gen > can't do: | ||
| 149 | |||
| 150 | my $pow_2 = Map {$_**2}; # partial application, needs at least 1 more | ||
| 151 | # argument to evaluate, but can be passed a list | ||
| 152 | my $ints = <0..>; | ||
| 153 | |||
| 154 | my $squares = $ints->$pow_2; | ||
| 155 | |||
| 156 | say "@$squares[0 .. 10]"; # 0 1 4 9 16 25 36 49 64 81 100 | ||
| 157 | |||
| 158 | and this: | ||
| 159 | |||
| 160 | my $src; | ||
| 161 | my $square_of_src = Map {$_ ** 2} $src; | ||
| 162 | |||
| 163 | $src = <1.. by 2>; | ||
| 164 | |||
| 165 | say "@$square_of_src[0 .. 4]"; # 1 9 25 49 81 | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | let map => sub (&@) {&gen(shift, &lazy)}; | ||
| 170 | |||
| 171 | |||
| 172 | =item filter C< {CODE} LIST > | ||
| 173 | |||
| 174 | filter, applied to a predicate and a list, returns the list of those elements | ||
| 175 | that satisfy the predicate; i.e., | ||
| 176 | |||
| 177 | filter p xs = [ x | x <- xs, p x] | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | let filter => sub (&@) {&FAST::List::Gen::filter(shift, &lazy)}; | ||
| 182 | |||
| 183 | |||
| 184 | =item head C< GENERATOR > | ||
| 185 | |||
| 186 | Extract the first element of a list, which must be non-empty. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | let head => sub {$_[0]->head}; | ||
| 191 | |||
| 192 | |||
| 193 | =item Last C< GENERATOR > | ||
| 194 | |||
| 195 | Extract the last element of a list, which must be finite and non-empty. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | let last => sub {$_[0]->last}; | ||
| 200 | |||
| 201 | |||
| 202 | =item tail C< GENERATOR > | ||
| 203 | |||
| 204 | Extract the elements after the head of a list, which must be non-empty. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | let tail => sub {$_[0]->tail}; | ||
| 209 | |||
| 210 | |||
| 211 | =item init C< GENERATOR > | ||
| 212 | |||
| 213 | Return all the elements of a list except the last one. The list must be | ||
| 214 | non-empty. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | let init => sub { | ||
| 219 | my $xs = shift; | ||
| 220 | take($xs->apply->size - 1, $xs) | ||
| 221 | }; | ||
| 222 | |||
| 223 | |||
| 224 | =item null C< GENERATOR > | ||
| 225 | |||
| 226 | Test whether a list is empty. | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | let null => sub {$_[0]->apply->size == 0}; | ||
| 231 | |||
| 232 | |||
| 233 | =item Length C< GENERATOR > | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | let length => sub {$_[0]->apply->size}; | ||
| 238 | |||
| 239 | |||
| 240 | =item Reverse C< LIST > | ||
| 241 | |||
| 242 | reverse xs returns the elements of xs in reverse order. xs must be finite. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | let reverse => sub {&list->reverse}; | ||
| 247 | |||
| 248 | |||
| 249 | =back | ||
| 250 | |||
| 251 | =head2 Reducing lists (folds) | ||
| 252 | |||
| 253 | =over 4 | ||
| 254 | |||
| 255 | =item foldl C< {CODE} ITEM LIST > | ||
| 256 | |||
| 257 | foldl, applied to a binary operator, a starting value (typically the | ||
| 258 | left-identity of the operator), and a list, reduces the list using the binary | ||
| 259 | operator, from left to right: | ||
| 260 | |||
| 261 | foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn | ||
| 262 | |||
| 263 | The list must be finite. | ||
| 264 | |||
| 265 | =item foldl1 C< {CODE} LIST > | ||
| 266 | |||
| 267 | C< foldl1 > is a variant of C< foldl > that has no starting value argument, and | ||
| 268 | thus must be applied to non-empty lists. | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | let foldl => sub (&@@) { | ||
| 273 | my ($code, $z) = splice @_, 0, 2; | ||
| 274 | &lazy->do(sub { | ||
| 275 | $z = $code->(now $z, $_) | ||
| 276 | }); | ||
| 277 | $z | ||
| 278 | }; | ||
| 279 | |||
| 280 | let foldl1 => sub (&@) { | ||
| 281 | my $code = shift; | ||
| 282 | my $xs = &lazy; | ||
| 283 | my $init = 1; | ||
| 284 | my $x; | ||
| 285 | $xs->do(sub { | ||
| 286 | if ($init) { | ||
| 287 | undef $init; | ||
| 288 | $x = now $_; | ||
| 289 | } else { | ||
| 290 | $x = $code->(now $x, $_) | ||
| 291 | } | ||
| 292 | }); | ||
| 293 | $x | ||
| 294 | }; | ||
| 295 | |||
| 296 | |||
| 297 | =item foldr C< {CODE} ITEM LIST > | ||
| 298 | |||
| 299 | foldr, applied to a binary operator, a starting value (typically the | ||
| 300 | right-identity of the operator), and a list, reduces the list using the binary | ||
| 301 | operator, from right to left: | ||
| 302 | |||
| 303 | foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...) | ||
| 304 | |||
| 305 | =item foldr1 C< {CODE} LIST > | ||
| 306 | |||
| 307 | C< foldr1 > is a variant of C< foldr > that has no starting value argument, and | ||
| 308 | thus must be applied to non-empty lists. | ||
| 309 | |||
| 310 | =cut | ||
| 311 | |||
| 312 | let foldr => sub (&@@) { | ||
| 313 | my ($code, $z) = splice @_, 0, 2; | ||
| 314 | &lazy->reverse->do(sub { | ||
| 315 | $z = $code->(now $_, $z) | ||
| 316 | }); | ||
| 317 | $z | ||
| 318 | }; | ||
| 319 | |||
| 320 | let foldr1 => sub (&@) { | ||
| 321 | my $code = shift; | ||
| 322 | my $xs = &lazy; | ||
| 323 | $xs->apply; | ||
| 324 | my $i = $xs->size - 1; | ||
| 325 | my $x = $xs->get($i--); | ||
| 326 | while ($i >= 0) { | ||
| 327 | $x = $code->(now $xs->get($i--), $x) | ||
| 328 | } | ||
| 329 | $x | ||
| 330 | }; | ||
| 331 | |||
| 332 | |||
| 333 | =back | ||
| 334 | |||
| 335 | =head3 Special folds | ||
| 336 | |||
| 337 | =over 4 | ||
| 338 | |||
| 339 | =item And C< LIST > | ||
| 340 | |||
| 341 | and returns the conjunction of a Boolean list. For the result to be True, the | ||
| 342 | list must be finite; False, however, results from a False value at a finite | ||
| 343 | index of a finite or infinite list. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | let and => sub { | ||
| 348 | not &lazy->do(sub {$_ or done 1}) | ||
| 349 | }; | ||
| 350 | |||
| 351 | |||
| 352 | =item Or C< LIST > | ||
| 353 | |||
| 354 | or returns the disjunction of a Boolean list. For the result to be False, the | ||
| 355 | list must be finite; True, however, results from a True value at a finite index | ||
| 356 | of a finite or infinite list. | ||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | let or => sub { | ||
| 361 | &lazy->do(sub {$_ and done 1}); | ||
| 362 | }; | ||
| 363 | |||
| 364 | |||
| 365 | =item any C< {CODE} LIST > | ||
| 366 | |||
| 367 | Applied to a predicate and a list, any determines if any element of the list | ||
| 368 | satisfies the predicate. | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | let any => sub (&@) { | ||
| 373 | my $p = shift; | ||
| 374 | &lazy->do(sub {$p->($_) and done 1}) | ||
| 375 | }; | ||
| 376 | |||
| 377 | |||
| 378 | =item all C< {CODE} LIST > | ||
| 379 | |||
| 380 | Applied to a predicate and a list, all determines if all elements of the list | ||
| 381 | satisfy the predicate. | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | let all => sub (&@) { | ||
| 386 | my $p = shift; | ||
| 387 | not &lazy->do(sub {$p->($_) or done 1}) | ||
| 388 | }; | ||
| 389 | |||
| 390 | |||
| 391 | =item sum C< LIST > | ||
| 392 | |||
| 393 | The sum function computes the sum of a finite list of numbers. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | let sum => sub {&lazy->sum}; | ||
| 398 | |||
| 399 | |||
| 400 | =item product C< LIST > | ||
| 401 | |||
| 402 | The product function computes the product of a finite list of numbers. | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | let product => sub {&lazy->product}; | ||
| 407 | |||
| 408 | |||
| 409 | =item concat C< GENERATOR > | ||
| 410 | |||
| 411 | Concatenate a list of lists. | ||
| 412 | |||
| 413 | =cut | ||
| 414 | |||
| 415 | let concat => sub { | ||
| 416 | my $xs = shift; | ||
| 417 | my $xp = 0; | ||
| 418 | my $self; | ||
| 419 | my $at; | ||
| 420 | my $sequence; | ||
| 421 | my $ret = mutable gen {$at = $_; $sequence->size > $_ or done; $sequence->get($_)}; | ||
| 422 | $sequence = sequence mutable gen { | ||
| 423 | $xp < @$xs or done; | ||
| 424 | my $seq = $self->sequence; | ||
| 425 | splice @$seq, $#$seq, 0, map {FAST::List::Gen::isagen or &makegen(\@$_)} $$xs[$xp++]; | ||
| 426 | $self->rebuild; | ||
| 427 | $sequence->get($at) | ||
| 428 | } 1; | ||
| 429 | $self = tied @$sequence; | ||
| 430 | $ret | ||
| 431 | }; | ||
| 432 | |||
| 433 | |||
| 434 | =item concatMap C< {CODE} LIST > | ||
| 435 | |||
| 436 | Map a function over a list and concatenate the results. | ||
| 437 | |||
| 438 | =cut | ||
| 439 | |||
| 440 | let concatMap => sub (&@) {concat(&gen(shift, &lazy))}; | ||
| 441 | |||
| 442 | |||
| 443 | =item maximum C< LIST > | ||
| 444 | |||
| 445 | maximum returns the maximum value from a list, which must be non-empty, finite, | ||
| 446 | and of an ordered type. It is a special case of maximumBy, which allows the | ||
| 447 | programmer to supply their own comparison function. | ||
| 448 | |||
| 449 | =cut | ||
| 450 | |||
| 451 | let maximum => sub {&lazy->max}; | ||
| 452 | |||
| 453 | |||
| 454 | =item minimum C< LIST > | ||
| 455 | |||
| 456 | minimum returns the minimum value from a list, which must be non-empty, finite, | ||
| 457 | and of an ordered type. It is a special case of minimumBy, which allows the | ||
| 458 | programmer to supply their own comparison function. | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | let minimum => sub {&lazy->min}; | ||
| 463 | |||
| 464 | |||
| 465 | =back | ||
| 466 | |||
| 467 | =head2 Building lists | ||
| 468 | |||
| 469 | =head3 Scans | ||
| 470 | |||
| 471 | =over 4 | ||
| 472 | |||
| 473 | =item scanl C< {CODE} LIST > | ||
| 474 | |||
| 475 | scanl is similar to foldl, but returns a list of successive reduced values from | ||
| 476 | the left: | ||
| 477 | |||
| 478 | scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] | ||
| 479 | |||
| 480 | Note that | ||
| 481 | |||
| 482 | last (scanl f z xs) == foldl f z xs. | ||
| 483 | |||
| 484 | =cut | ||
| 485 | |||
| 486 | let scanl => sub (&@) { | ||
| 487 | my $code = shift; | ||
| 488 | my $gen = &lazy; | ||
| 489 | iterate_multi { | ||
| 490 | my $ret = $_ == 0 ? $gen->get(0) : $code->(self($_ - 1), $gen->get($_)); | ||
| 491 | done $ret if $_ + 1 >= $gen->size; | ||
| 492 | $ret | ||
| 493 | }->recursive | ||
| 494 | }; | ||
| 495 | |||
| 496 | |||
| 497 | =item scanr C< {CODE} LIST > | ||
| 498 | |||
| 499 | scanr is the right-to-left dual of scanl. Note that | ||
| 500 | |||
| 501 | head (scanr f z xs) == foldr f z xs. | ||
| 502 | |||
| 503 | =cut | ||
| 504 | |||
| 505 | let scanr => sub (&@) { | ||
| 506 | my $code = shift; | ||
| 507 | my $gen = &lazy; | ||
| 508 | iterate_multi { | ||
| 509 | my $ret = $_ == 0 ? $gen->get($gen->size - 1) : $code->($gen->get($gen->size - ($_+1)), self($_ - 1)); | ||
| 510 | done $ret if $_ + 1 >= $gen->size; | ||
| 511 | $ret | ||
| 512 | }->recursive | ||
| 513 | }; | ||
| 514 | |||
| 515 | |||
| 516 | =back | ||
| 517 | |||
| 518 | =head3 Infinite lists | ||
| 519 | |||
| 520 | =over 4 | ||
| 521 | |||
| 522 | =item iterate C< {CODE} ITEM > | ||
| 523 | |||
| 524 | iterate f x returns an infinite list of repeated applications of f to x: | ||
| 525 | |||
| 526 | iterate f x == [x, f x, f (f x), ...] | ||
| 527 | |||
| 528 | =cut | ||
| 529 | |||
| 530 | let iterate => sub (&@) { | ||
| 531 | my ($code, $x) = @_; | ||
| 532 | &FAST::List::Gen::iterate(sub {$code->($_)})->from($x)->scalar; | ||
| 533 | }; | ||
| 534 | |||
| 535 | |||
| 536 | =item repeat C< ITEM > | ||
| 537 | |||
| 538 | repeat x is an infinite list, with x the value of every element. | ||
| 539 | |||
| 540 | =cut | ||
| 541 | |||
| 542 | let repeat => sub { | ||
| 543 | my $x = $_[0]; | ||
| 544 | gen {$x} | ||
| 545 | }; | ||
| 546 | |||
| 547 | |||
| 548 | =item hs_repeat C< ITEM > | ||
| 549 | |||
| 550 | my $repeat; $repeat = lazy $x, $repeat; | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | let hs_repeat => sub { | ||
| 555 | my ($x, $xs) = shift; | ||
| 556 | $xs = lazy $x, $xs | ||
| 557 | }; | ||
| 558 | |||
| 559 | |||
| 560 | =item replicate C< NUM ITEM > | ||
| 561 | |||
| 562 | replicate n x is a list of length n with x the value of every element. | ||
| 563 | |||
| 564 | =cut | ||
| 565 | |||
| 566 | let hs_replicate => sub ($@) { | ||
| 567 | take(shift, &hs_repeat) | ||
| 568 | }; | ||
| 569 | |||
| 570 | let replicate => sub ($@) { | ||
| 571 | my ($n, $x) = @_; | ||
| 572 | gen {$x} $n | ||
| 573 | }; | ||
| 574 | |||
| 575 | |||
| 576 | =item cycle C< LIST > | ||
| 577 | |||
| 578 | cycle ties a finite list into a circular one, or equivalently, the infinite | ||
| 579 | repetition of the original list. It is the identity on infinite lists. | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | let cycle => sub {&lazy->cycle}; | ||
| 584 | |||
| 585 | |||
| 586 | =item hs_cycle C< LIST > | ||
| 587 | |||
| 588 | hs_cycle ties a finite list into a circular one, or equivalently, the infinite | ||
| 589 | repetition of the original list. It is the identity on infinite lists. | ||
| 590 | |||
| 591 | it is defined in perl as: | ||
| 592 | |||
| 593 | my $cycle; $cycle = lazy $xs, $cycle; | ||
| 594 | |||
| 595 | =cut | ||
| 596 | |||
| 597 | let hs_cycle => sub { | ||
| 598 | my ($xs, $cycle) = &lazy; | ||
| 599 | $cycle = lazy $xs, $cycle | ||
| 600 | }; | ||
| 601 | |||
| 602 | |||
| 603 | =back | ||
| 604 | |||
| 605 | =head3 Sublists | ||
| 606 | |||
| 607 | =over 4 | ||
| 608 | |||
| 609 | =item take C< NUM LIST > | ||
| 610 | |||
| 611 | take n, applied to a list xs, returns the prefix of xs of length n, or xs itself | ||
| 612 | if n > length xs: | ||
| 613 | |||
| 614 | take 3 [1,2,3,4,5] == [1,2,3] | ||
| 615 | take 3 [1,2] == [1,2] | ||
| 616 | take 3 [] == [] | ||
| 617 | take (-1) [1,2] == [] | ||
| 618 | take 0 [1,2] == [] | ||
| 619 | |||
| 620 | =cut | ||
| 621 | |||
| 622 | let take => sub (@@) { | ||
| 623 | my $n = shift; | ||
| 624 | &lazy->take($n) | ||
| 625 | }; | ||
| 626 | |||
| 627 | |||
| 628 | =item drop C< NUM LIST > | ||
| 629 | |||
| 630 | drop n xs returns the suffix of xs after the first n elements, or [] if n > | ||
| 631 | length xs: | ||
| 632 | |||
| 633 | drop 3 [1,2,3,4,5] == [4,5] | ||
| 634 | drop 3 [1,2] == [] | ||
| 635 | drop 3 [] == [] | ||
| 636 | drop (-1) [1,2] == [1,2] | ||
| 637 | drop 0 [1,2] == [1,2] | ||
| 638 | |||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | let drop => sub (@@) { | ||
| 643 | my $n = shift; | ||
| 644 | &lazy->drop($n) | ||
| 645 | }; | ||
| 646 | |||
| 647 | |||
| 648 | =item splitAt C< NUM LIST > | ||
| 649 | |||
| 650 | splitAt n xs returns a tuple where first element is xs prefix of length n and | ||
| 651 | second element is the remainder of the list: | ||
| 652 | |||
| 653 | splitAt 3 [1,2,3,4,5] == ([1,2,3],[4,5]) | ||
| 654 | splitAt 1 [1,2,3] == ([1],[2,3]) | ||
| 655 | splitAt 3 [1,2,3] == ([1,2,3],[]) | ||
| 656 | splitAt 4 [1,2,3] == ([1,2,3],[]) | ||
| 657 | splitAt 0 [1,2,3] == ([],[1,2,3]) | ||
| 658 | splitAt (-1) [1,2,3] == ([],[1,2,3]) | ||
| 659 | |||
| 660 | It is equivalent to (take n xs, drop n xs). | ||
| 661 | |||
| 662 | =cut | ||
| 663 | |||
| 664 | let splitAt => sub {take(@_), drop(@_)}, 2, 2; | ||
| 665 | |||
| 666 | |||
| 667 | =item takeWhile C< {CODE} LIST > | ||
| 668 | |||
| 669 | takeWhile, applied to a predicate p and a list xs, returns the longest prefix | ||
| 670 | (possibly empty) of xs of elements that satisfy p: | ||
| 671 | |||
| 672 | take_while (< 3) [1,2,3,4,1,2,3,4] == [1,2] | ||
| 673 | take_while (< 9) [1,2,3] == [1,2,3] | ||
| 674 | take_while (< 0) [1,2,3] == [] | ||
| 675 | |||
| 676 | =cut | ||
| 677 | |||
| 678 | let takeWhile => sub (&@) { | ||
| 679 | my ($p, $xs) = (shift, &lazy); | ||
| 680 | $xs->take_while(sub {$p->($_)}) | ||
| 681 | }; | ||
| 682 | |||
| 683 | |||
| 684 | =item dropWhile C< {CODE} LIST > | ||
| 685 | |||
| 686 | dropWhile p xs returns the suffix remaining after take_while p xs: | ||
| 687 | |||
| 688 | dropWhile (< 3) [1,2,3,4,5,1,2,3] == [3,4,5,1,2,3] | ||
| 689 | dropWhile (< 9) [1,2,3] == [] | ||
| 690 | dropWhile (< 0) [1,2,3] == [1,2,3] | ||
| 691 | |||
| 692 | =cut | ||
| 693 | |||
| 694 | let dropWhile => sub (&@) { | ||
| 695 | my ($p, $xs) = (shift, &lazy); | ||
| 696 | $xs->drop_while(sub {$p->($_)}) | ||
| 697 | }; | ||
| 698 | |||
| 699 | |||
| 700 | =item span C< {CODE} LIST > | ||
| 701 | |||
| 702 | span, applied to a predicate p and a list xs, returns a tuple where first | ||
| 703 | element is longest prefix (possibly empty) of xs of elements that satisfy p and | ||
| 704 | second element is the remainder of the list: | ||
| 705 | |||
| 706 | span (< 3) [1,2,3,4,1,2,3,4] == ([1,2],[3,4,1,2,3,4]) | ||
| 707 | span (< 9) [1,2,3] == ([1,2,3],[]) | ||
| 708 | span (< 0) [1,2,3] == ([],[1,2,3]) | ||
| 709 | |||
| 710 | span p xs is equivalent to (takeWhile p xs, dropWhile p xs) | ||
| 711 | |||
| 712 | =cut | ||
| 713 | |||
| 714 | let span => sub (&@) { | ||
| 715 | my $p = shift; | ||
| 716 | &lazy->span(sub {$p->($_)}) | ||
| 717 | }, 2, 2; | ||
| 718 | |||
| 719 | |||
| 720 | =item break C< {CODE} LIST > | ||
| 721 | |||
| 722 | break, applied to a predicate p and a list xs, returns a tuple where first | ||
| 723 | element is longest prefix (possibly empty) of xs of elements that do not satisfy | ||
| 724 | p and second element is the remainder of the list: | ||
| 725 | |||
| 726 | break (> 3) [1,2,3,4,1,2,3,4] == ([1,2,3],[4,1,2,3,4]) | ||
| 727 | break (< 9) [1,2,3] == ([],[1,2,3]) | ||
| 728 | break (> 9) [1,2,3] == ([1,2,3],[]) | ||
| 729 | |||
| 730 | break p is equivalent to span (not . p) | ||
| 731 | |||
| 732 | =cut | ||
| 733 | |||
| 734 | let break => sub (&@) { | ||
| 735 | my $p = shift; | ||
| 736 | &lazy->span(sub {not $p->($_)}) | ||
| 737 | }, 2, 2; | ||
| 738 | |||
| 739 | |||
| 740 | =back | ||
| 741 | |||
| 742 | =head3 Searching lists | ||
| 743 | |||
| 744 | =over 4 | ||
| 745 | |||
| 746 | =item elem C< ITEM LIST > | ||
| 747 | |||
| 748 | elem is the list membership predicate, usually written in infix form, e.g., | ||
| 749 | x `elem` xs. | ||
| 750 | |||
| 751 | =cut | ||
| 752 | |||
| 753 | let elem => sub (@@) { | ||
| 754 | my $x = shift; | ||
| 755 | $x eq $_ and return 1 for @{&lazy}; | ||
| 756 | return () | ||
| 757 | }; | ||
| 758 | |||
| 759 | |||
| 760 | =item notElem C< ITEM LIST > | ||
| 761 | |||
| 762 | notElem is the negation of elem. | ||
| 763 | |||
| 764 | =cut | ||
| 765 | |||
| 766 | let notElem => sub (@@) { | ||
| 767 | my $x = shift; | ||
| 768 | my $xs = &lazy; | ||
| 769 | $x eq $_ and return () for @$xs; | ||
| 770 | return 1 | ||
| 771 | }; | ||
| 772 | |||
| 773 | |||
| 774 | =back | ||
| 775 | |||
| 776 | =head3 Zipping and unzipping lists | ||
| 777 | |||
| 778 | =over 4 | ||
| 779 | |||
| 780 | =item zip C< LIST > | ||
| 781 | |||
| 782 | zip takes 2+ lists and returns a single interleaved list. If one input list is | ||
| 783 | short, excess elements of the longer lists are discarded. unlike the haskell | ||
| 784 | version, the zip returns a flat generator. | ||
| 785 | |||
| 786 | C<zip> is the same as C<zipWith {\@_}> | ||
| 787 | |||
| 788 | =cut | ||
| 789 | |||
| 790 | let zip => sub (@@) { | ||
| 791 | FAST::List::Gen::zipwith {\@_} @_ | ||
| 792 | }; | ||
| 793 | |||
| 794 | |||
| 795 | =item zipWith C< {CODE} LIST > | ||
| 796 | |||
| 797 | zipWith generalizes zip by zipping with the function given as the first | ||
| 798 | argument, instead of a tupling function. For example, zipWith (+) is applied to | ||
| 799 | two lists to produce the list of corresponding sums. | ||
| 800 | |||
| 801 | =cut | ||
| 802 | |||
| 803 | let zipWith => \&FAST::List::Gen::zipwith, 3; | ||
| 804 | |||
| 805 | |||
| 806 | =item zipWithAB C< {$a * $b} $gen1, $gen2 > | ||
| 807 | |||
| 808 | The zipWithAB function takes a function which uses C< $a > and C< $b >, as well | ||
| 809 | as two lists and returns a list analogous to zipWith. | ||
| 810 | |||
| 811 | =cut | ||
| 812 | |||
| 813 | let zipWithAB => \&FAST::List::Gen::zipWithAB, 3; | ||
| 814 | |||
| 815 | |||
| 816 | =item unzip C< GENERATOR > | ||
| 817 | |||
| 818 | unzip transforms a list into two lists of the even and odd elements. | ||
| 819 | |||
| 820 | zs = zip xs, ys | ||
| 821 | (xs, ys) == unzip zs | ||
| 822 | |||
| 823 | =cut | ||
| 824 | |||
| 825 | BEGIN {undef $_ for *unzip, *unzipn} | ||
| 826 | {no warnings 'once'; | ||
| 827 | *unzip = &unzipn(2); | ||
| 828 | } | ||
| 829 | |||
| 830 | |||
| 831 | =item unzipn C< NUM GENERATOR > | ||
| 832 | |||
| 833 | The unzipn function is the n-dimentional precursor to C< unzip > | ||
| 834 | |||
| 835 | unzip xs = unzipn 2, xs | ||
| 836 | |||
| 837 | =cut | ||
| 838 | |||
| 839 | sub unzipn { | ||
| 840 | return \&unzipn unless @_; | ||
| 841 | my $n = shift; | ||
| 842 | return bless sub {&unzipn($n, @_)} => 'FAST::List::Gen::Bare::Function' unless @_; | ||
| 843 | my $xs = shift; | ||
| 844 | map { | ||
| 845 | my $x = $_; | ||
| 846 | gen {$$_[$x]} $xs | ||
| 847 | } 0 .. $n - 1; | ||
| 848 | } | ||
| 849 | bless \&unzipn => 'FAST::List::Gen::Bare::Function'; | ||
| 850 | |||
| 851 | |||
| 852 | =back | ||
| 853 | |||
| 854 | =head3 Functions on strings | ||
| 855 | |||
| 856 | =over 4 | ||
| 857 | |||
| 858 | =item lines C< STRING > | ||
| 859 | |||
| 860 | lines breaks a string up into a list of strings at newline characters. The | ||
| 861 | resulting strings do not contain newlines. the newline sequence is taken from | ||
| 862 | the value of the input record separator C< $/ > | ||
| 863 | |||
| 864 | =cut | ||
| 865 | |||
| 866 | let lines => sub { | ||
| 867 | my $str = &lazy->join($/); | ||
| 868 | my $nl = quotemeta $/; | ||
| 869 | iterate_multi { | ||
| 870 | if ($str =~ s{\A(.*?)(?:$nl|\Z)}{}o) { | ||
| 871 | length $str ? $1 : done $1 | ||
| 872 | } else {done} | ||
| 873 | } | ||
| 874 | }; | ||
| 875 | |||
| 876 | |||
| 877 | =item words C< STRING > | ||
| 878 | |||
| 879 | words breaks a string up into a list of words, which were delimited by white | ||
| 880 | space. | ||
| 881 | |||
| 882 | =cut | ||
| 883 | |||
| 884 | let words => sub { | ||
| 885 | my $str = &lazy->str; | ||
| 886 | iterate_multi { | ||
| 887 | if ($str =~ s/\A\s*(\S*)(?:\s+|\Z)//) { | ||
| 888 | length $str ? $1 : done $1 | ||
| 889 | } else {done} | ||
| 890 | } | ||
| 891 | }; | ||
| 892 | |||
| 893 | |||
| 894 | =item unlines C< LIST > | ||
| 895 | |||
| 896 | unlines is an inverse operation to lines. It joins lines, after appending a | ||
| 897 | terminating newline to each. the newline sequence is taken from the value of the | ||
| 898 | input record separator C< $/ > | ||
| 899 | |||
| 900 | =cut | ||
| 901 | |||
| 902 | let unlines => sub {&foldl1(sub {join $/ => @_}, &lazy)}; | ||
| 903 | |||
| 904 | |||
| 905 | =item unwords C< LIST > | ||
| 906 | |||
| 907 | unwords is an inverse operation to words. It joins words with separating spaces. | ||
| 908 | |||
| 909 | =cut | ||
| 910 | |||
| 911 | let unwords => sub {&foldl1(sub {join ' ' => @_}, &lazy)}; | ||
| 912 | |||
| 913 | |||
| 914 | =back | ||
| 915 | |||
| 916 | =head1 ACKNOWLEGEMENTS | ||
| 917 | |||
| 918 | most of the documentation here started out at | ||
| 919 | L<http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Prelude.html> | ||
| 920 | and was subsequently edited to account for implementation differences. | ||
| 921 | |||
| 922 | =head1 AUTHOR | ||
| 923 | |||
| 924 | Eric Strom, C<< <asg at cpan.org> >> | ||
| 925 | |||
| 926 | =head1 BUGS | ||
| 927 | |||
| 928 | there are certainly bugs in code this complex. send in reports, tests, patches. | ||
| 929 | |||
| 930 | report any bugs / feature requests to C<bug-list-gen at rt.cpan.org>, or through | ||
| 931 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Gen>. | ||
| 932 | |||
| 933 | =head1 COPYRIGHT & LICENSE | ||
| 934 | |||
| 935 | copyright 2009-2011 Eric Strom. | ||
| 936 | |||
| 937 | this program is free software; you can redistribute it and/or modify it under | ||
| 938 | the terms of either: the GNU General Public License as published by the Free | ||
| 939 | Software Foundation; or the Artistic License. | ||
| 940 | |||
| 941 | see http://dev.perl.org/licenses/ for more information. | ||
| 942 | |||
| 943 | =cut | ||
| 944 | |||
| 945 | $setup_export->(); | ||
| 946 | delete $FAST::List::Gen::Haskell::{let}; | ||
| 947 | |||
| 948 | __PACKAGE__ if 'first require'; |