Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f30b6f52

Von Sven Schöling vor mehr als 15 Jahren hinzugefügt

  • ID f30b6f52b1684388cebc0878b73c39c6b1040749
  • Vorgänger 17b87579
  • Nachfolger ecd0ca84

cross von common.pl nach MoreCommon verschoben.

Unterschiede anzeigen:

bin/mozilla/common.pl
580 580
  $lxdebug->leave_sub();
581 581
}
582 582

  
583
=item cross BLOCK ARRAY ARRAY
584

  
585
Evaluates BLOCK for each combination of elements in ARRAY1 and ARRAY2
586
and returns a new list consisting of BLOCK's return values.
587
The two elements are set to $a and $b.
588
Note that those two are aliases to the original value so changing them
589
will modify the input arrays.
590

  
591
  # append each to each
592
  @a = qw/a b c/;
593
  @b = qw/1 2 3/;
594
  @x = pairwise { "$a$b" } @a, @b;
595
  # returns a1, a2, a3, b1, b2, b3, c1, c2, c3
596

  
597
As cross expects an array but returns a list it is not directly chainable
598
at the moment. This will be corrected in the future.
599

  
600
=cut
601
sub cross(&\@\@) {
602
  my $op = shift;
603
  use vars qw/@A @B/;
604
  local (*A, *B) = @_;    # syms for caller's input arrays
605

  
606
  # Localise $a, $b
607
  my ($caller_a, $caller_b) = do {
608
    my $pkg = caller();
609
    no strict 'refs';
610
    \*{$pkg.'::a'}, \*{$pkg.'::b'};
611
  };
612

  
613
  local(*$caller_a, *$caller_b);
614

  
615
  # This map expression is also the return value.
616
  map { my $a_index = $_;
617
    map { my $b_index = $_;
618
      # assign to $a, $b as refs to caller's array elements
619
      (*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]);
620
      $op->();    # perform the transformation
621
    }  0 .. $#B;
622
  }  0 .. $#A;
623
}
624

  
625 583
1;

Auch abrufbar als: Unified diff