home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Database / CLIPR503.W96 / ITERATOR.PR_ / ITERATOR.PR
Text File  |  1995-06-20  |  2KB  |  76 lines

  1. /***
  2. *
  3. *  Iterator.prg
  4. *
  5. *  Sample array iteration functions
  6. *
  7. *  Copyright (c) 1993-1995, Computer Associates International Inc.
  8. *  All rights reserved.
  9. *
  10. *  NOTE: compile with /a /m /n /w
  11. *
  12. */
  13.  
  14.  
  15. /***
  16. *
  17. *  IEval( <nCount>, <bBlock> ) --> xValueLast
  18. *
  19. *  Evaluate bBlock nCount times, passing it the current iteration
  20. *  (starting at 1)
  21. *
  22. *  Return the value the last iteration returns
  23. *
  24. */
  25. FUNCTION IEval( nCount, bBlock )
  26.  
  27.    LOCAL i              // Counter variable
  28.    LOCAL xValResult     // Return value
  29.  
  30.    FOR i := 1 TO nCount
  31.       xValResult := EVAL( bBlock, i )
  32.    NEXT
  33.  
  34.    RETURN( xValResult )
  35.  
  36.  
  37.  
  38. /***
  39. *
  40. *  Collect( <aArray>, <bBlock> ) --> aResults
  41. *
  42. *  Evaluate bBlock on each element of aArray and store the results in
  43. *  aResults
  44. *
  45. */
  46. FUNCTION Collect( aArray, bBlock )
  47.  
  48.    LOCAL aResults[ LEN( aArray ) ]
  49.    LOCAL nCount := 1
  50.  
  51.    AEVAL( aArray, { |element| aResults[nCount++] := EVAL( bBlock, element) } )
  52.  
  53.    RETURN( aResults )
  54.  
  55.  
  56.  
  57. /***
  58. *
  59. *  Extract( <aArray>, <bMatch> ) --> aMatches
  60. *
  61. *  Returns the elements in aArray that match specified criteria.  bMatch
  62. *  is passed on element at a time and should return .T. if element matches
  63. *  criteria, false otherwise.
  64. *
  65. */
  66. FUNCTION Extract( aArray, bMatch )
  67.  
  68.    LOCAL aResults := {}
  69.  
  70.    AEVAL( aArray, { |element| IIF( EVAL( bMatch, element),   ;
  71.                                    AADD( aResults, element), ;
  72.                                    NIL ) }                   ;
  73.         )
  74.  
  75.    RETURN ( aResults )
  76.