home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7520 < prev    next >
Encoding:
Text File  |  1992-12-21  |  1.8 KB  |  56 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!mcsun!sun4nl!ruuinf!henkp
  3. From: henkp@cs.ruu.nl (Henk Penning)
  4. Subject: Re: In 5.0, are we gonna get...
  5. Sender: network-news@cs.ruu.nl
  6. Message-ID: <1992Dec18.140112.18192@cs.ruu.nl>
  7. Date: Fri, 18 Dec 1992 14:01:12 GMT
  8. References: <1992Dec14.043835.23009@reed.edu> <1992Dec14.192007.7534@wdl.loral.com> <2B2D2498.874E@tct.com> <7246288358-314904@dec4.wu-wien.ac.at>
  9. Organization: Utrecht University, Dept. of Computer Science
  10. Lines: 44
  11.  
  12. In <7246288358-314904@dec4.wu-wien.ac.at> neumann@dec4.wu-wien.ac.at (Gustaf Neumann) writes:
  13.  
  14. > [ ... ] how about 
  15. >
  16. >        $sum = eval join('+ ',1,34,$x,45,-8,0.1);
  17. >
  18. >ok, the string conversion could be avoided, but you can do it now...
  19.  
  20. >Gustaf Neumann          neumann@dec4.wu-wien.ac.at, neumann@awiwuw11.bitnet
  21.  
  22.   I would like to write:
  23.  
  24.     $max = reduce { $a >  $b ? $a : $b } @row
  25.   
  26. -- Another idea would be to have a reduce in array-context,
  27.    for example to squeeze out adjacent equal elements:
  28.  
  29.      @uniq = reduce { ($a eq $b) ? ($a) : ($a,$b) } @row
  30.    
  31.    Here the ab-sub is evaluated in array-context. The first time
  32.    $a is the first element of @row, later on the last element
  33.    of the partial result.
  34.  
  35. -- Computing the difference of adjacent elements diff[i]=row[i+1]-row[i]
  36.  
  37.      @diff = reduce { ($b-$a,$b) } @row ; pop @diff ;
  38.  
  39. -- Range compression: 1,3,4,5,7,22 -> 1,3..5,7,22
  40.  
  41.    sub compress
  42.      { if ( $a =~ /^(\d+)\.\.(\d+)$/ )
  43.      { ( $b == $2 + 1 ) ? ("$1..$b") : ($a,$b) ; }
  44.        else
  45.      { ( $b == $a + 1 ) ? ("$a..$b") : ($a,$b) ; }
  46.      }
  47.  
  48.    @compress = reduce compress @row ;
  49.  
  50. -- The examples have not been tested (;-) but the code looks nice.
  51.    The scalar-reduce is much more important to have than the
  52.    array-reduce, but I think there is a case for the latter too.
  53.  
  54.                 ===  HenkP  ===
  55.  
  56.