[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Iternating over multiple lists



A little bit ago, John Carter said:
: On Tue, 11 Apr 2000, Bernie Cosell wrote:
: > What perl needed to make the solution to this problem trivial (and 
: > elegant) is "multiple map" (or, if you rather "parallel map").  That 
: > is, imagine if you could do:
: > 
: >    mmap {BLOCK} list1 list2 list3 list4 ....
: > 
: 
: I agree with the objection to the name mmap = memory map to me.

Right. So I just whipped up the not-called-mmap function as an
academic exercise (i.e., trying to look busy). Is this function
something people actually want? If so, what should it be called?
As it stands now:

  multimap  {BLOCK} LISTREF, LISTREF, ...
  multigrep {BLOCK} LISTREF, LISTREF, ...

each loop through the lists in parallel, analgous to map() and grep().
Each successive set of values is passed to BLOCK in @_, and modifying
those values @_ changes the corresponding LIST value. To wit:

  use MultiMap qw/multimap multigrep/;
  @char=('a'..'f');
  $vowels = multigrep {
      my $is_vowel = $_[0] =~ /[aeiou]/;
      $_[1] = $is_vowel ? 'vowel' : 'consonant';
      $is_vowel;
      } \@char, \@type;
  print "$vowels vowels:\n",(multimap {"$_[0] $_[1]\n"} \@char, \@type);

gives:

  2 vowels:
  a vowel
  b consonant
  c consonant
  d consonant
  e vowel
  f consonant

dan

-- 
Daniel Macks
dmacks@a.chem.upenn.edu
dmacks@netspace.org
http://www.netspace.org/~dmacks


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe