home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a009 / 6.ddi / SAMPLE.LIF / ITERATOR.PRG < prev    next >
Encoding:
Text File  |  1991-04-14  |  1.2 KB  |  56 lines

  1. /***
  2. *
  3. *  Iterator.prg
  4. *  Sample array iterator functions
  5. *  Copyright, Nantucket Corporation, 1990
  6. *
  7. *  NOTE: compile with /n/w/a/m
  8. */
  9.  
  10.  
  11.  
  12. /***
  13. *  IEval( <nCount>, <bBlock> ) --> valueLast
  14. *  Evaluate bBlock nCount times, passing it the current iteration (starting
  15. *  at 1).  Return the value the last iteration returns.
  16. */
  17. FUNCTION IEval( nCount, bBlock )
  18.    LOCAL i, valResult
  19.  
  20.    FOR i := 1 TO nCount
  21.       valResult := EVAL( bBlock, i )
  22.    NEXT
  23.  
  24.    RETURN( valResult )
  25.  
  26.  
  27.  
  28. /***
  29. *  Collect( <aArray>, <bBlock> ) --> aResults
  30. *  Evaluate bBlock on each element of aArray and store the results in 
  31. *  aResults
  32. */
  33. FUNCTION Collect( aArray, bBlock )
  34.    LOCAL aResults[ LEN( aArray ) ]
  35.    LOCAL nCount := 1
  36.  
  37.    AEVAL( aArray, { |element| aResults[nCount++] := EVAL( bBlock, element) } )
  38.  
  39.    RETURN( aResults )
  40.  
  41.  
  42.  
  43. /***
  44. *  Extract( <aArray>, <bMatch> ) --> aMatches
  45. *  Returns the elements in aArray that match specified criteria.  bMatch 
  46. *  is passed on element at a time and should return .T. if element matches
  47. *  criteria, false otherwise. 
  48. */
  49. FUNCTION Extract( aArray, bMatch )
  50.    LOCAL aResults := {}
  51.  
  52.    AEVAL( aArray, {|element| IIF( EVAL( bMatch, element), AADD( aResults, element), NIL )} )
  53.  
  54.    RETURN( aResults )
  55.  
  56.