| 1 | package Seq; | ||
| 2 | 9 | ▶ | use 5.036; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | our $VERSION = '0.005'; | ||
| 4 | 9 | ▶ | use subs 'bind', 'join', 'select', 'last', 'sort'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use Scalar::Util qw(reftype); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use List::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 9 | ▶ | use Sub::Exporter -setup => { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | exports => [ | ||
| 10 | qw(id fst snd key assign), | ||
| 11 | ], | ||
| 12 | 9 | }; | |
| 13 | 0 | ▶ | use DDP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | # TODO: contains?, firstIndex?, mapX, sort, cache | ||
| 16 | # interspers, slice, zip, unzip, foldBack, any, | ||
| 17 | # forall, none, average, average_by, | ||
| 18 | # pairwise, windowed, transpose, item, chunk_by_size, | ||
| 19 | # one, minmax, minmax_by, | ||
| 20 | # regex_match, regex_replace | ||
| 21 | # | ||
| 22 | # + ways to do regexes on strings | ||
| 23 | # + using this module to scan file-system | ||
| 24 | # ? how about a data checker tool | ||
| 25 | # + more ways to transform data, especially complex structures | ||
| 26 | # ? A data transformation/selection language | ||
| 27 | # o Error checking through another module that adds signature type check through AOP | ||
| 28 | # o Maybe DU, Record fist-class support when i implement them. | ||
| 29 | # o good way to also implement async with it? | ||
| 30 | |||
| 31 | # Important functions used in FP code. So adding them. | ||
| 32 | sub id :prototype($) { return $_[0] } | ||
| 33 | sub fst :prototype($) { return $_[0][0] } | ||
| 34 | sub snd :prototype($) { return $_[0][1] } | ||
| 35 | |||
| 36 | # a helper to create a function for selecting a hash-key | ||
| 37 | sub key :prototype($) { | ||
| 38 | my $name = $_[0]; | ||
| 39 | return sub($hash) { return $hash->{$name} }; | ||
| 40 | } | ||
| 41 | |||
| 42 | # allows writing a code block to create a value | ||
| 43 | sub assign :prototype(&) { | ||
| 44 | return $_[0]->(); | ||
| 45 | } | ||
| 46 | |||
| 47 | #- Constructurs | ||
| 48 | # Those are functions that create Seq types | ||
| 49 | |||
| 50 | # creates a sequence from a subroutine | ||
| 51 | sub from_sub($class, $f) { | ||
| 52 | return bless(sub { | ||
| 53 | my $abort = 0; | ||
| 54 | my $it = $f->(); | ||
| 55 | return sub { | ||
| 56 | return undef if $abort; | ||
| 57 | if ( defined(my $x = $it->()) ) { | ||
| 58 | return $x; | ||
| 59 | } | ||
| 60 | $abort = 1; | ||
| 61 | undef $it; | ||
| 62 | } | ||
| 63 | }, 'Seq'); | ||
| 64 | } | ||
| 65 | |||
| 66 | # always an empty sequence | ||
| 67 | sub empty($class) { | ||
| 68 | return from_sub('Seq', sub { | ||
| 69 | return sub { undef }; | ||
| 70 | }); | ||
| 71 | } | ||
| 72 | |||
| 73 | # TODO: When $state is a reference. Same handling as in fold? | ||
| 74 | sub unfold($class, $state, $f) { | ||
| 75 | from_sub('Seq', sub { | ||
| 76 | # IMPORTANT: Perl signatures are aliases. As we assign | ||
| 77 | # to $state later, we need to make a copy here. | ||
| 78 | # Removing this lines causes bugs. | ||
| 79 | my $state = $state; | ||
| 80 | my $abort = 0; | ||
| 81 | my $x; | ||
| 82 | return sub { | ||
| 83 | return undef if $abort; | ||
| 84 | |||
| 85 | ($x, $state) = $f->($state); | ||
| 86 | $abort = 1 if not defined $x; | ||
| 87 | return $x; | ||
| 88 | } | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub init($class, $count, $f) { | ||
| 93 | return unfold('Seq', 0, sub($index) { | ||
| 94 | return $f->($index), $index+1 if $index < $count; | ||
| 95 | return undef; | ||
| 96 | }); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub range_step($class, $start, $step, $stop) { | ||
| 100 | croak '$step is 0. Will run forever.' if $step == 0; | ||
| 101 | |||
| 102 | # Ascending order | ||
| 103 | if ( $start <= $stop ) { | ||
| 104 | return unfold('Seq', $start, sub($current) { | ||
| 105 | return $current, $current+$step if $current <= $stop; | ||
| 106 | return undef; | ||
| 107 | }); | ||
| 108 | } | ||
| 109 | # Descending | ||
| 110 | else { | ||
| 111 | return unfold('Seq', $start, sub($current) { | ||
| 112 | return $current, $current-$step if $current >= $stop; | ||
| 113 | return undef; | ||
| 114 | }); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub range($class, $start, $stop) { | ||
| 119 | return range_step('Seq', $start, 1, $stop); | ||
| 120 | } | ||
| 121 | |||
| 122 | # turns all arguments into an sequence | ||
| 123 | sub wrap($class, @xs) { | ||
| 124 | my $last = $#xs; | ||
| 125 | return unfold('Seq', 0, sub($idx) { | ||
| 126 | return $xs[$idx], $idx+1 if $idx <= $last; | ||
| 127 | return undef; | ||
| 128 | }); | ||
| 129 | } | ||
| 130 | |||
| 131 | # turns a list into a Seq - alias to wrap | ||
| 132 | sub from_list($class, @xs) { | ||
| 133 | return wrap(Seq => @xs); | ||
| 134 | } | ||
| 135 | |||
| 136 | # turns an arrayref into a seq | ||
| 137 | sub from_array($class, $xs) { | ||
| 138 | return unfold('Seq', 0, sub($idx) { | ||
| 139 | return $xs->[$idx], $idx+1 if $idx <= $xs->$#*; | ||
| 140 | return undef; | ||
| 141 | }); | ||
| 142 | } | ||
| 143 | |||
| 144 | # Pass it a hashref. The function $f is executed for every | ||
| 145 | # (key, value) pair. The result is used as a single item in | ||
| 146 | # the sequence. | ||
| 147 | # | ||
| 148 | # Seq->from_hash : Hash<'Key, 'Value> -> ('Key -> 'Value -> 'a) -> Seq<'a> | ||
| 149 | sub from_hash($class, $hashref, $f) { | ||
| 150 | from_sub('Seq', sub { | ||
| 151 | my $idx = 0; | ||
| 152 | my @keys = keys %$hashref; | ||
| 153 | my $last = $#keys; | ||
| 154 | return sub { | ||
| 155 | return undef if $idx > $last; | ||
| 156 | my $key = $keys[$idx++]; | ||
| 157 | return $f->($key, $hashref->{$key}); | ||
| 158 | } | ||
| 159 | }); | ||
| 160 | } | ||
| 161 | |||
| 162 | # Concatenates a list of Seq into a single Seq | ||
| 163 | sub concat($class, @seqs) { | ||
| 164 | my $count = @seqs; | ||
| 165 | |||
| 166 | # with no values to concat, return an empty iterator | ||
| 167 | return empty('Seq') if $count == 0; | ||
| 168 | # one element can be returned as-is | ||
| 169 | return $seqs[0] if $count == 1; | ||
| 170 | # at least two items | ||
| 171 | return List::Util::reduce { append($a, $b) } @seqs; | ||
| 172 | } | ||
| 173 | |||
| 174 | #- Methods | ||
| 175 | # functions operating on Seq and returning another Seq | ||
| 176 | |||
| 177 | sub append($seqA, $seqB) { | ||
| 178 | from_sub('Seq', sub { | ||
| 179 | my $exhaustedA = 0; | ||
| 180 | my $itA = $seqA->(); | ||
| 181 | my $itB = $seqB->(); | ||
| 182 | |||
| 183 | return sub { | ||
| 184 | REDO: | ||
| 185 | if ( $exhaustedA ) { | ||
| 186 | return $itB->(); | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | if ( defined(my $x = $itA->()) ) { | ||
| 190 | return $x; | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $exhaustedA = 1; | ||
| 194 | goto REDO; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | }; | ||
| 198 | }); | ||
| 199 | } | ||
| 200 | |||
| 201 | # map : Seq<'a> -> ('a -> 'b) -> Seq<'b'> | ||
| 202 | sub map($seq, $f) { | ||
| 203 | from_sub('Seq', sub { | ||
| 204 | my $it = $seq->(); | ||
| 205 | return sub { | ||
| 206 | if ( defined(my $x = $it->()) ) { | ||
| 207 | return $f->($x); | ||
| 208 | } | ||
| 209 | return undef; | ||
| 210 | } | ||
| 211 | }); | ||
| 212 | } | ||
| 213 | |||
| 214 | # bind : Seq<'a> -> ('a -> Seq<'b>) -> Seq<'b> | ||
| 215 | sub bind($seq, $f) { | ||
| 216 | from_sub('Seq', sub { | ||
| 217 | my $it = $seq->(); | ||
| 218 | my $seqB = undef; | ||
| 219 | |||
| 220 | return sub { | ||
| 221 | REDO: | ||
| 222 | # when $seqB is defined. an entry from the seq is returned | ||
| 223 | if ( defined $seqB ) { | ||
| 224 | if ( defined(my $b = $seqB->()) ) { | ||
| 225 | return $b; | ||
| 226 | } | ||
| 227 | # as soon an undef is returned, $seqB finished | ||
| 228 | $seqB = undef; | ||
| 229 | } | ||
| 230 | # when $seqB is undef, we request/create a new $seqB | ||
| 231 | if ( defined(my $a = $it->()) ) { | ||
| 232 | $seqB = $f->($a)->(); | ||
| 233 | goto REDO; | ||
| 234 | } | ||
| 235 | # when $seqB is not defined and $it does not return new values | ||
| 236 | return undef; | ||
| 237 | } | ||
| 238 | }); | ||
| 239 | } | ||
| 240 | |||
| 241 | # flatten : Seq<Seq<'a>> -> Seq<'a> | ||
| 242 | sub flatten($iter) { | ||
| 243 | return bind($iter, \&id); | ||
| 244 | } | ||
| 245 | |||
| 246 | # flatten_array : Seq<Array<'a>> -> Seq<'a> | ||
| 247 | sub flatten_array($seq) { | ||
| 248 | return bind($seq, sub($array) { | ||
| 249 | return from_array('Seq', $array); | ||
| 250 | }); | ||
| 251 | } | ||
| 252 | |||
| 253 | # cartesian : Seq<'a> -> Seq<'b> -> Seq<'a * 'b> | ||
| 254 | sub cartesian($seqA, $seqB) { | ||
| 255 | bind($seqA, sub($a) { | ||
| 256 | bind($seqB, sub($b) { | ||
| 257 | wrap('Seq', [$a, $b]); | ||
| 258 | })}); | ||
| 259 | } | ||
| 260 | |||
| 261 | # join creates the cartesian product, but only for those elements | ||
| 262 | # $predicate returns true. | ||
| 263 | # join : Seq<'a> -> Seq<'b> -> ('a -> 'b -> bool) -> Seq<'a * 'b> | ||
| 264 | sub join($seqA, $seqB, $predicate) { | ||
| 265 | bind($seqA, sub($a) { | ||
| 266 | bind($seqB, sub($b) { | ||
| 267 | return wrap(Seq => [$a, $b]) if $predicate->($a, $b); | ||
| 268 | return empty('Seq'); | ||
| 269 | })}); | ||
| 270 | } | ||
| 271 | |||
| 272 | # Expects a sequence of tuples. For example what join returns. | ||
| 273 | # Provides a merging function to combine 'a and 'b into something new 'c | ||
| 274 | # merge : Seq<'a * 'b> -> ('a -> 'b -> 'c) -> Seq<'c> | ||
| 275 | sub merge($iter, $merge) { | ||
| 276 | bind($iter, sub($tuple) { | ||
| 277 | return wrap(Seq => $merge->($tuple->[0], $tuple->[1])); | ||
| 278 | }); | ||
| 279 | } | ||
| 280 | |||
| 281 | # Merges a sequence that contains tuples with hashes. Like: [{...}, {...}] | ||
| 282 | # $mapA contains the selection from the first element of the tuple | ||
| 283 | # $mapB contains the selection from the second element of the tuple | ||
| 284 | # | ||
| 285 | # Selection can either be a hashref mapping a hashnames to a | ||
| 286 | # new hashname { id => 'some_id' } | ||
| 287 | # | ||
| 288 | # or an array of names that should be picked: [qw/id name/] | ||
| 289 | sub select($iter, $mapA, $mapB) { | ||
| 290 | # Transforms the different inputs a user can give into a | ||
| 291 | # hash and an array containing the keys | ||
| 292 | state $gen_input = sub($mapping) { | ||
| 293 | my $hash; | ||
| 294 | my $keys; | ||
| 295 | if ( not defined reftype $mapping) { | ||
| 296 | if ( $mapping =~ m/\Aall\z/i ) { | ||
| 297 | return ['ALL']; | ||
| 298 | } | ||
| 299 | elsif ( $mapping =~ m/\Anone\z/i ) { | ||
| 300 | return ['NONE']; | ||
| 301 | } | ||
| 302 | else { | ||
| 303 | croak "When not arrayref or hashref must be either 'ALL' or 'NONE'"; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | elsif ( reftype $mapping eq 'HASH' ) { | ||
| 307 | $hash = $mapping; | ||
| 308 | $keys = [ keys $mapping->%* ]; | ||
| 309 | } | ||
| 310 | elsif ( reftype $mapping eq 'ARRAY' ) { | ||
| 311 | $hash = { map { $_ => $_ } @$mapping }; | ||
| 312 | $keys = $mapping; | ||
| 313 | } | ||
| 314 | else { | ||
| 315 | croak '$mappings must be tuple and either contain hashref or arrayref'; | ||
| 316 | } | ||
| 317 | |||
| 318 | # Returns a discriminated union with three cases | ||
| 319 | # ['ALL'] | ||
| 320 | # ['NONE'] | ||
| 321 | # ['SELECTION', $mapping, $keys] | ||
| 322 | return [SELECTION => $hash, $keys]; | ||
| 323 | }; | ||
| 324 | |||
| 325 | my $caseA = $gen_input->($mapA); | ||
| 326 | my $caseB = $gen_input->($mapB); | ||
| 327 | |||
| 328 | merge($iter, sub($a, $b) { | ||
| 329 | my %new_hash; | ||
| 330 | |||
| 331 | # Merge keys from $seqA | ||
| 332 | if ( $caseA->[0] eq 'ALL' ) { | ||
| 333 | # Copies hash | ||
| 334 | %new_hash = %$a; | ||
| 335 | } | ||
| 336 | elsif ( $caseA->[0] eq 'NONE' ) { | ||
| 337 | # do nothing here ... | ||
| 338 | } | ||
| 339 | else { | ||
| 340 | my ($mapping, $keys) = $caseA->@[1,2]; | ||
| 341 | for my $key ( @$keys ) { | ||
| 342 | $new_hash{$mapping->{$key}} = $a->{$key}; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | # Merge keys from $seqB | ||
| 347 | if ( $caseB->[0] eq 'ALL' ) { | ||
| 348 | # add keys from $b to new hash | ||
| 349 | for my $key ( keys %$b ) { | ||
| 350 | $new_hash{$key} = $b->{$key}; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | elsif ( $caseB->[0] eq 'NONE' ) { | ||
| 354 | # do nothing here ... | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | my ($mapping, $keys) = $caseB->@[1,2]; | ||
| 358 | for my $key ( @$keys ) { | ||
| 359 | $new_hash{$mapping->{$key}} = $b->{$key}; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | return \%new_hash; | ||
| 364 | }); | ||
| 365 | } | ||
| 366 | |||
| 367 | # combines map and filter | ||
| 368 | sub choose($iter, $chooser) { | ||
| 369 | from_sub('Seq', sub { | ||
| 370 | my $it = $iter->(); | ||
| 371 | return sub { | ||
| 372 | while ( defined(my $x = $it->()) ) { | ||
| 373 | my $optional = $chooser->($x); | ||
| 374 | return $optional if defined $optional; | ||
| 375 | } | ||
| 376 | return undef; | ||
| 377 | } | ||
| 378 | }); | ||
| 379 | } | ||
| 380 | |||
| 381 | sub mapi($iter, $f) { | ||
| 382 | return indexed($iter)->map($f); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub filter($iter, $predicate) { | ||
| 386 | from_sub('Seq', sub { | ||
| 387 | my $it = $iter->(); | ||
| 388 | return sub { | ||
| 389 | while ( defined(my $x = $it->()) ) { | ||
| 390 | return $x if $predicate->($x); | ||
| 391 | } | ||
| 392 | return undef; | ||
| 393 | } | ||
| 394 | }); | ||
| 395 | } | ||
| 396 | |||
| 397 | sub take($iter, $amount) { | ||
| 398 | from_sub('Seq', sub { | ||
| 399 | my $i = $iter->(); | ||
| 400 | my $returnedSoFar = 0; | ||
| 401 | return sub { | ||
| 402 | if ( $returnedSoFar < $amount ) { | ||
| 403 | $returnedSoFar++; | ||
| 404 | if ( defined(my $x = $i->()) ) { | ||
| 405 | return $x; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | return; | ||
| 409 | } | ||
| 410 | }); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub skip($iter, $amount) { | ||
| 414 | from_sub('Seq', sub { | ||
| 415 | my $it = $iter->(); | ||
| 416 | my $count = 0; | ||
| 417 | return sub { | ||
| 418 | while ( $count++ < $amount ) { | ||
| 419 | $it->(); | ||
| 420 | } | ||
| 421 | return $it->(); | ||
| 422 | } | ||
| 423 | }); | ||
| 424 | } | ||
| 425 | |||
| 426 | sub indexed($iter) { | ||
| 427 | my $index = 0; | ||
| 428 | return $iter->map(sub($x) { | ||
| 429 | return [$index++, $x]; | ||
| 430 | }); | ||
| 431 | } | ||
| 432 | |||
| 433 | sub distinct_by($iter, $f) { | ||
| 434 | from_sub('Seq', sub { | ||
| 435 | my $it = $iter->(); | ||
| 436 | my %seen; | ||
| 437 | return sub { | ||
| 438 | SKIP: | ||
| 439 | if ( defined(my $x = $it->()) ) { | ||
| 440 | my $key = $f->($x); | ||
| 441 | goto SKIP if exists $seen{$key}; | ||
| 442 | $seen{$key} = 1; | ||
| 443 | return $x; | ||
| 444 | } | ||
| 445 | return undef; | ||
| 446 | } | ||
| 447 | }); | ||
| 448 | } | ||
| 449 | |||
| 450 | # remove duplicates - it uses a hash to remember seen items | ||
| 451 | # so it only works good when Seq contains Strings or Numbers. | ||
| 452 | # Use distinct_by for other data. | ||
| 453 | sub distinct($iter) { | ||
| 454 | return distinct_by($iter, \&id); | ||
| 455 | } | ||
| 456 | |||
| 457 | # TODO: Instead of fsts and snds provide a function to pick the index of an array. | ||
| 458 | # Maybe even a function to pick and re-order multiple elements | ||
| 459 | # Like: ->pick([3,1,5]) | ||
| 460 | |||
| 461 | sub fsts($seq) { | ||
| 462 | return $seq->map(sub ($x) { $x->[0] }); | ||
| 463 | } | ||
| 464 | |||
| 465 | sub snds($seq) { | ||
| 466 | return $seq->map(sub ($x) { $x->[1] }); | ||
| 467 | } | ||
| 468 | |||
| 469 | # TODO: zip can handle a list of sequences | ||
| 470 | sub zip($seqA, $seqB) { | ||
| 471 | from_sub('Seq', sub { | ||
| 472 | my $itA = $seqA->(); | ||
| 473 | my $itB = $seqB->(); | ||
| 474 | |||
| 475 | return sub { | ||
| 476 | if (defined (my $a = $itA->())) { | ||
| 477 | if (defined (my $b = $itB->())) { | ||
| 478 | return [$a,$b]; | ||
| 479 | }} | ||
| 480 | return undef; | ||
| 481 | } | ||
| 482 | }); | ||
| 483 | } | ||
| 484 | |||
| 485 | #- Side-Effects | ||
| 486 | # functions that have side-effects or produce side-effects. Those are | ||
| 487 | # immediately executed, usually consuming all elements of Seq at once. | ||
| 488 | |||
| 489 | sub iter($iter, $f) { | ||
| 490 | my $it = $iter->(); | ||
| 491 | while ( defined(my $x = $it->()) ) { | ||
| 492 | $f->($x); | ||
| 493 | } | ||
| 494 | return; | ||
| 495 | } | ||
| 496 | |||
| 497 | # Similar to iter(). But returns the $seq as-is. | ||
| 498 | # Useful for doing something between a chain. For example printing | ||
| 499 | # all elements of a sequence. | ||
| 500 | # | ||
| 501 | # $seq->do(sub($x) { print Dumper($x) })->... | ||
| 502 | sub do($iter, $f) { | ||
| 503 | my $it = $iter->(); | ||
| 504 | while ( defined(my $x = $it->()) ) { | ||
| 505 | $f->($x); | ||
| 506 | } | ||
| 507 | return $iter; | ||
| 508 | } | ||
| 509 | |||
| 510 | sub rev($iter) { | ||
| 511 | from_sub('Seq', sub { | ||
| 512 | my @list = to_list($iter); | ||
| 513 | return sub { | ||
| 514 | if ( defined(my $x = pop @list) ) { | ||
| 515 | return $x; | ||
| 516 | } | ||
| 517 | return undef; | ||
| 518 | }; | ||
| 519 | }); | ||
| 520 | } | ||
| 521 | |||
| 522 | sub sort($seq, $comparer) { | ||
| 523 | from_sub('Seq', sub { | ||
| 524 | local ($a, $b); | ||
| 525 | my @array = CORE::sort { $comparer->($a, $b) } to_list($seq); | ||
| 526 | my $idx = 0; | ||
| 527 | my $count = @array; | ||
| 528 | |||
| 529 | return sub { | ||
| 530 | if ( $idx < $count ) { | ||
| 531 | my $x = $array[$idx]; | ||
| 532 | $idx++; | ||
| 533 | return $x; | ||
| 534 | } | ||
| 535 | return undef; | ||
| 536 | }; | ||
| 537 | }); | ||
| 538 | } | ||
| 539 | |||
| 540 | # group_by : Seq<'a> -> ('a -> 'Key) -> Seq<Seq<'a>> | ||
| 541 | sub group_by($seq, $get_key) { | ||
| 542 | my %group; | ||
| 543 | iter($seq, sub($a) { | ||
| 544 | my $key = $get_key->($a); | ||
| 545 | push $group{$key}->@*, $a; | ||
| 546 | }); | ||
| 547 | |||
| 548 | return from_hash('Seq', \%group, sub($key, $value) { | ||
| 549 | return from_array('Seq', $value); | ||
| 550 | }); | ||
| 551 | } | ||
| 552 | |||
| 553 | # group_fold : | ||
| 554 | # Seq<'a> | ||
| 555 | # -> (unit -> 'State) | ||
| 556 | # -> ('a -> 'Key) | ||
| 557 | # -> ('State -> 'a -> 'State) | ||
| 558 | # -> Seq<'State> | ||
| 559 | sub group_fold($seq, $get_state, $get_key, $folder) { | ||
| 560 | my %group; | ||
| 561 | iter($seq, sub($a) { | ||
| 562 | my $key = $get_key->($a); | ||
| 563 | push $group{$key}->@*, $a; | ||
| 564 | }); | ||
| 565 | |||
| 566 | return from_hash('Seq', \%group, sub($key, $array) { | ||
| 567 | my $state = $get_state->(); | ||
| 568 | for my $a ( @$array ) { | ||
| 569 | $state = $folder->($state, $a); | ||
| 570 | } | ||
| 571 | return $state; | ||
| 572 | }); | ||
| 573 | } | ||
| 574 | |||
| 575 | #- Converter | ||
| 576 | # Those are functions converting Seq to none Seq types | ||
| 577 | |||
| 578 | # fold is like a foreach-loop. You iterate through all items generating | ||
| 579 | # a new 'State. The $folder is passed the latest | ||
| 580 | # 'State and one 'a from the sequence. You return | ||
| 581 | # the next 'State that should be used. Once all elements of 'a | ||
| 582 | # sequence are processed. The last 'State is returned. | ||
| 583 | # | ||
| 584 | # fold : Seq<'a> -> 'State -> ('State -> 'a -> 'State) -> 'State | ||
| 585 | sub fold($seq, $state, $folder) { | ||
| 586 | iter($seq, sub($x) { | ||
| 587 | $state = $folder->($state, $x); | ||
| 588 | }); | ||
| 589 | return $state; | ||
| 590 | } | ||
| 591 | |||
| 592 | # Same as fold. But when you mutate 'State and return 'State from | ||
| 593 | # the lambda. You can use this function instead. | ||
| 594 | # | ||
| 595 | # fold : Seq<'a> -> 'State -> ('State -> 'a -> 'State) -> 'State | ||
| 596 | sub fold_mut($seq, $state, $folder) { | ||
| 597 | iter($seq, sub($x) { | ||
| 598 | $folder->($state, $x); | ||
| 599 | }); | ||
| 600 | return $state; | ||
| 601 | } | ||
| 602 | |||
| 603 | # Like fold, but without an initial state. When sequence is empty | ||
| 604 | # it returns an undef. Otherwise combines two elements from | ||
| 605 | # left to right to produce an output. Needs a sequence | ||
| 606 | # with one item to work properly. It is encouraged to use `fold` | ||
| 607 | # instead. | ||
| 608 | # reduce: Seq<'a> -> ('a -> 'a -> 'a) -> 'a | ||
| 609 | sub reduce($seq, $reducer, $default) { | ||
| 610 | return fold(skip($seq, 1), first($seq, $default), $reducer); | ||
| 611 | } | ||
| 612 | |||
| 613 | # first : Seq<'a> -> 'a -> 'a | ||
| 614 | sub first($seq, $default) { | ||
| 615 | my $first = $seq->()(); | ||
| 616 | return $first if defined $first; | ||
| 617 | return $default; | ||
| 618 | } | ||
| 619 | |||
| 620 | # last : Seq<'a> -> 'a -> 'a | ||
| 621 | sub last($seq, $default) { | ||
| 622 | my $last; | ||
| 623 | iter($seq, sub($x) { | ||
| 624 | $last = $x; | ||
| 625 | }); | ||
| 626 | return $last if defined $last; | ||
| 627 | return $default; | ||
| 628 | } | ||
| 629 | |||
| 630 | # to_array : Seq<'a> -> Array<'a> | ||
| 631 | sub to_array($seq) { | ||
| 632 | state $folder = sub($array, $x) { push @$array, $x }; | ||
| 633 | return fold_mut($seq, [], $folder); | ||
| 634 | } | ||
| 635 | |||
| 636 | sub to_list($seq) { | ||
| 637 | return @{ to_array($seq) }; | ||
| 638 | } | ||
| 639 | |||
| 640 | # count : Seq<'a> -> int | ||
| 641 | sub count($seq) { | ||
| 642 | state $folder = sub($count, $x) { $count+1 }; | ||
| 643 | return fold($seq, 0, $folder); | ||
| 644 | } | ||
| 645 | |||
| 646 | # sum : Seq<'a> -> float | ||
| 647 | sub sum($seq) { | ||
| 648 | state $folder = sub($sum, $x) { $sum + $x }; | ||
| 649 | return fold($seq, 0, $folder); | ||
| 650 | } | ||
| 651 | |||
| 652 | # sum_by : Seq<'a> -> (float -> 'a -> float) -> float | ||
| 653 | sub sum_by($seq, $f) { | ||
| 654 | return fold($seq, 0, sub($sum, $x) { | ||
| 655 | return $sum + $f->($x); | ||
| 656 | }); | ||
| 657 | } | ||
| 658 | |||
| 659 | # returns the min value or undef on empty sequence | ||
| 660 | # min value is compared with numerical < | ||
| 661 | sub min($seq, $default) { | ||
| 662 | min_by($seq, \&id, $default); | ||
| 663 | } | ||
| 664 | |||
| 665 | # returns the min value or undef on empty sequence | ||
| 666 | # min value is compared using lt | ||
| 667 | sub min_str($seq, $default) { | ||
| 668 | min_by_str($seq, \&id, $default); | ||
| 669 | } | ||
| 670 | |||
| 671 | sub min_by($seq, $key, $default) { | ||
| 672 | return fold($seq, undef, sub($min, $x) { | ||
| 673 | my $value = $key->($x); | ||
| 674 | defined $min | ||
| 675 | ? ($value < $min) ? $value : $min | ||
| 676 | : $value; | ||
| 677 | }) // $default; | ||
| 678 | } | ||
| 679 | |||
| 680 | sub min_by_str($seq, $key, $default) { | ||
| 681 | return fold($seq, undef, sub($min, $x) { | ||
| 682 | my $value = $key->($x); | ||
| 683 | defined $min | ||
| 684 | ? ($value lt $min) ? $value : $min | ||
| 685 | : $value; | ||
| 686 | }) // $default; | ||
| 687 | } | ||
| 688 | |||
| 689 | # returns the max value or undef when sequence is empty | ||
| 690 | sub max($seq, $default) { | ||
| 691 | max_by($seq, \&id, $default); | ||
| 692 | } | ||
| 693 | |||
| 694 | sub max_str($seq, $default) { | ||
| 695 | max_by_str($seq, \&id, $default); | ||
| 696 | } | ||
| 697 | |||
| 698 | sub max_by($seq, $key, $default) { | ||
| 699 | return fold($seq, undef, sub($max, $x) { | ||
| 700 | my $value = $key->($x); | ||
| 701 | defined $max | ||
| 702 | ? ($value > $max) ? $value : $max | ||
| 703 | : $value; | ||
| 704 | }) // $default; | ||
| 705 | } | ||
| 706 | |||
| 707 | sub max_by_str($seq, $key, $default) { | ||
| 708 | return fold($seq, undef, sub($max, $x) { | ||
| 709 | my $value = $key->($x); | ||
| 710 | defined $max | ||
| 711 | ? ($value gt $max) ? $value : $max | ||
| 712 | : $value; | ||
| 713 | }) // $default; | ||
| 714 | } | ||
| 715 | |||
| 716 | sub str_join($iter, $sep) { | ||
| 717 | return CORE::join($sep, to_list($iter)); | ||
| 718 | } | ||
| 719 | |||
| 720 | # Build a hash by providing a keying function. Later elements | ||
| 721 | # in the sequence overwrite previous one. | ||
| 722 | sub to_hash($seq, $get_key) { | ||
| 723 | my %hash; | ||
| 724 | iter($seq, sub($x) { | ||
| 725 | $hash{$get_key->($x)} = $x; | ||
| 726 | }); | ||
| 727 | return \%hash; | ||
| 728 | } | ||
| 729 | |||
| 730 | # Build a hash by providing a keying function. Values | ||
| 731 | # are put into arrays to allow key with multiple values. | ||
| 732 | # | ||
| 733 | # to_hash_of_array: Seq<'a> -> ('a -> 'Key) -> Hash<'Key, Array<'a>> | ||
| 734 | sub to_hash_of_array($seq, $get_key) { | ||
| 735 | my %hash; | ||
| 736 | iter($seq, sub($x) { | ||
| 737 | push @{ $hash{$get_key->($x)} }, $x; | ||
| 738 | }); | ||
| 739 | return \%hash; | ||
| 740 | } | ||
| 741 | |||
| 742 | # to_array_of_array : Seq<Seq<'a>> -> Array<Array<'a>> | ||
| 743 | sub to_array_of_array($seq) { | ||
| 744 | my @outer; | ||
| 745 | iter($seq, sub($inner) { | ||
| 746 | push @outer, to_array($inner); | ||
| 747 | }); | ||
| 748 | return \@outer; | ||
| 749 | } | ||
| 750 | |||
| 751 | # returns first element for which the given $predicate returns true | ||
| 752 | # | ||
| 753 | # find : Seq<'a> -> ('a -> bool) -> 'a | ||
| 754 | sub find($seq, $default, $predicate) { | ||
| 755 | my $it = $seq->(); | ||
| 756 | while ( defined(my $x = $it->()) ) { | ||
| 757 | return $x if $predicate->($x); | ||
| 758 | } | ||
| 759 | return $default; | ||
| 760 | } | ||
| 761 | |||
| 762 | 1; | ||